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.
Related
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
I could not find anything on this particular problem, so here it goes!
I am interested in changing the position of the mouse cursor programmatically.
Cursor.Position = new Point(x, y);
works as expected using a single monitor. However, when I plug in a second one and choose to extend the desktop, changing the cursor's coordinates only works relative to the main screen.
So, does anyone know how to tell the cursor that I want to move to a different screen?
Get the width and height of your required screen and move the cursor relative to it
int width=Screen.AllScreens[1].Bounds.Width;
int height=Screen.AllScreens[1].Bounds.Height;
Cursor.Position=new Point(width-x,height-y);
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 know how to stimulate clicks using User32 SendInput method and what I need is a similar User32 method but to obtain the current mouse button state.
Something similar to:
public static extern bool GetCursorPos(ref System.Drawing.Point lpPoint);
Function GetCursorPos gives me the current cursor position. What I need is the left button state (if it's clicked or not). Is there such a function?
Use GetAsyncKeyState, To Quote MSDN:
The GetAsyncKeyState function works
with mouse buttons. However, it checks
on the state of the physical mouse
buttons, not on the logical mouse
buttons that the physical buttons are
mapped to. For example, the call
GetAsyncKeyState(VK_LBUTTON) always
returns the state of the left physical
mouse button, regardless of whether it
is mapped to the left or right logical
mouse button. You can determine the
system's current mapping of physical
mouse buttons to logical mouse buttons
by calling
GetSystemMetrics(SM_SWAPBUTTON).
There's a method called GetAsyncKeyState. The method signature looks like this:
[DllImport("user32.dll")]
public static extern short GetAsyncKeyState(UInt16 virtualKeyCode);
Then you simply call it passing the left mouse key code (VK_LBUTTON = 0x01) and off you go.
More information directly from MSDN.