How to move mouse cursor using C #?

I want to simulate a mouse movement every x seconds. To do this, I use a timer (x seconds), and when the timer is ticking, I will make a mouse movement.

But how can I move the mouse cursor using C #?

+55
c #
Nov 08 '11 at 13:05
source share
2 answers

Take a look at the Cursor.Position Property . He must start you.

 private void MoveCursor() { // Set the Current cursor, move the cursor 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); } 
+49
Nov 08 2018-11-11T00:
source share

First Add Class (Win32.cs)

 public class Win32 { [DllImport("User32.Dll")] public static extern long SetCursorPos(int x, int y); [DllImport("User32.Dll")] public static extern bool ClientToScreen(IntPtr hWnd, ref POINT point); [StructLayout(LayoutKind.Sequential)] public struct POINT { public int x; public int y; } } 

Then call it from the event:

 Win32.POINT p = new Win32.POINT(); px = Convert.ToInt16(txtMouseX.Text); py = Convert.ToInt16(txtMouseY.Text); Win32.ClientToScreen(this.Handle, ref p); Win32.SetCursorPos(px, py); 
+19
Feb 22 '14 at 21:21
source share



All Articles