I am working on a WPF application and want to set the cursor position to the corner of the screen.
I tried using the WinForms approach:
System.Windows.Forms.Cursor.Position = new System.Drawing.Point(0, 0);
and the user32.dll approach:
[System.Runtime.InteropServices.DllImport("User32.dll")]
private static extern bool SetCursorPos(int X, int Y);
The WinForms approach worked sometimes, but I don't understand why it worked and didn't work sometimes. I made sure the code is actually being called. The user32.dll approach didn't work for me at all.
Simple solution is make your application enough big and move mouse inside of it.
http://www.aspdotnet-pools.com/2017/10/move-mouse-cursor-automatically-c.html
To move mouse outside your application you need global mouse hook.
Global mouse event handler
Related
private void MoveCursor()
{
// Set the Current cursor, move the cursor's Position,
// and set its clipping rectangle to the form.
this.Cursor = new Cursor(Cursor.Current.Handle);
Cursor.Position = new Point(Cursor.Position.X - 50, Cursor.Position.Y - 50);
Cursor.Clip = new Rectangle(this.Location, this.Size);
}
I am using the above code to Restrict the movement but still I am able to move the mouse outside the form?
Can I restrict the mouse movement to a specified area in the form? Please advise...
Updated answer:
ClipCursor is the API function you require. You will need to supply screen-based coordinates.
BOOL WINAPI ClipCursor(RECT *lpRect);
take a look at this link for the Win32 API code, and this one for pinvoke from C#.
There is pair of Win32 API functions called SetCapture/ReleaseCapture that will restrict the mouse to a certain window bounds.
You will need to use PInvoke to use them, but this will work.
[DllImport("user32.dll")]
static extern IntPtr SetCapture(long hWnd);
SetCapture(Control.Handle);
One thing to bear in mind, is that if used incorrectly, it's possible that the user will not be able to click the [X] to shut down your application because the mouse will not be able to get to the title bar.
I'm looking for an api that can cause a windows select/highlight event to occur on a windows desktop, without actually causing the mouse cursor to move.. I can cause the mouse cursor to move with :
public static extern bool SetCursorPos(int X, int Y);
But that moves the actual cursor to that point... I'm looking for a way to highlight only as one might do by using the tab and arrow keys to move around the windows desktop. Any suggestions are appreciated..
regards, rob
I think you may be looking for SetFocus. You can get a control's handle with Control.Handle or FindWindow , and p/invoke SetFocus (use IntPtr as the argument type).
Im trying to move my cursor according to my hand point in kinect, I can get the real coordinates I mean I can move an image on screen but I want real cursor to be settled according to my hand coordinates. I tried Console.SetCursor(x,y) but it gives exception I also tried to download windows forms dll but I cant find the version 4.00 . Is there any simple way to set cursor in a desired position? (which is working by the way and as I said Console.SetcursorPosition is not wodking?)
You didn't provide very much information about you app but I suspect that you just need to assign to Cursor.Position from System.Windows.Forms. You may need to add a reference to System.Windows.Forms in order to gain access to this, depending on exactly what type of project you have.
If you want to keep it lightweight and avoid taking a reference to WinForms then you could just pinvoke to SetCursorPos.
[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);
Just use
Cursor.Position = new Point();
You can find more information's here
Thank you for question and answer.
I found strange and unobvious behavior.
Let you use multi-monitor (two or three monitor) configuration, and use two windows in your application.
One window is active, and you set cursor position for second windows.
And this two windows located on different monitors.
Then, you need call SetCursorPos TWICE!
TWICE, who would have thought?
Firs call move cursor to virtual borderline between monitors.
And only second call move cursor to required position second window!
Example of code (after 6 hours of experiments):
SetForegroundWindow(this.Handle); // set second window active
SendMessage(this.Handle, 0x20, this.Handle, (IntPtr)1); // Send WM_SETCURSOR
SetCursorPos(400, 600);
Thread.Sleep(50);
SetCursorPos(400, 600);
i need to implement a cursor with some very specific features:
it has to be animated
because after n seconds it automatically clicks - so the animation is feedback for the user when the click will happen
it has to snap to some of our controls
it has to work outside of our application
the approaches so far:
render my WPF-control into a bitmap, make a cursor-struct out of it and use user32.dll/SetSystemCursor to set it
PRO
the cursor has no delay after the mouse since it's a real cursor
CON
snapping is quite hard, especially since we have absolute and relative inputdevices and i would have to reset the mouseposition all the time or use user32.dll/ClipCursor (System.Windows.Forms.Cursor.Clip does the same) but the snapped cursor is always shaking around the snapped position (tries to escape, get's reset again....)
the code i use throws strange exceptions after some random time - so my current code seems quite unstable
render my own cursor into a maximized, topmost, allowtransparent, windowstyle=none, invisible window and manually move the cursor after the mouse (like Canvas.SetLeft(cursor, MousePosition.X))
PRO
snapping can be (easily) done
CON
when the mouse clicks and hit's the cursor the cursor get's clicked and not the window beyond
polling the mouseposition in a dispatcher-background-loop all the time doesn't seem very beautiful to me
to solve the second approach my cursor would have to have at least one transparent pixel
in the hotspot, so that the mouse can click through... that doesn't seem like a real solution to me...
any idea's anyone?
EDIT:
some example-source to show the problems...:
example app & source to show the problem with snapping the mouse to a fixed position: ClipIt.rar
example app & source that fails after random time - setting a self-drawn cursor: TryOwnCur.rar
can be found under: http://sourcemonk.com/Cursor
thanks to http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/a3cb7db6-5014-430f-a5c2-c9746b077d4f
i can click through my self-drawn cursor which follows the mouse-position by
setting the window style:none, and allowtransparent as i already did and
then
public const int WS_EX_TRANSPARENT = 0x00000020;
public const int GWL_EXSTYLE = (-20);
[DllImport("user32.dll")]
public static extern int GetWindowLong(IntPtr hwnd,
int index);
[DllImport("user32.dll")]
public static extern int SetWindowLong(IntPtr hwnd,
int index, int newStyle);
public static void makeTransparent(IntPtr hwnd) {
int extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_TRANSPARENT);
}
and call makeTransparent from OnSourceInitialized...
I'm writing a toy application that plays with the mouse cursor, and I'm trying to move it programmticly. Using either Cursor.Position = ... or Win32 interop calls work fine on a normal machine, but I'm having difficulties getting it to work under VMWare.
Does anyone have any suggestions?
EDIT
To clarify:
I've got a small windows forms application I've made running inside the VM, it has one button on it, when clicked it is supposed to move the mouse cursor inside the VM. I've used both the Cursor.Position method and the approach that Wolf5 has suggested.
Try this instead:
[DllImport("user32", SetLastError = true)]
private static extern int SetCursorPos(int x, int y);
public static void SetMousePos(Point p) {
SetMousePos(p.X, p.Y);
}
public static void SetMousePos(int x, int y) {
SetCursorPos(x, y);
}
Of course you will have to make sure VMWARE has focus in the first place since it cannot set the mouse position of your mouse outside VMWARE.
Don't focus the VM with your real mouse. Or uninstall the VMWare mouse driver so the VM doesn't get the focus unless you click inside it.
I have resolved the issue.
In a desperate attempt to try anything i finally gave up and un-installed the mouse driver from the VM. After a reboot, my toy application works.
The device was listed as a VMWare Pointing device, after the reboot it's coming up as an "unknown device", but the mouse still works. Albeit I a little on the nippy side.