Je veux simuler le mouvement de la souris toutes les x secondes. Pour cela, je vais utiliser une timer (x secondes) et quand la timer sera activée, je ferai le mouvement de la souris.
Mais comment puis-je déplacer le curseur de la souris en utilisant C #?
Examinez la propriété Cursor.Position
. Cela devrait vous aider à démarrer.
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); }
Première classe d’ajout (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; } }
Alors appelez-le de l’événement:
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);