¿Cómo mover el cursor del mouse usando C #?

I want to simulate mouse movement every x seconds. For that, I'll use a timer (x seconds) and when the timer ticks I'll make the mouse movement.

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

preguntado el 08 de noviembre de 11 a las 13:11

This sounds like half a solution to a problem you're not telling us about, that probably has more elegant solutions. -

It's quite possible! We don't understand why but the screen saver is activates passed 10 minutes. But we put 999 minutes :P -

Then you ought to be looking for solutions that prevent the screen saver from activating when your application is running, rather than fiddling with the mouse or the screensaver settings. E.g. P/Invoke SetThreadExecutionState. I suspected this was screensaver related - programmed mouse movements don't reset the screensaver timer. -

2 Respuestas

Echa un vistazo a la Cursor.Position Propiedad. It should get you started.

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);

respondido 08 nov., 11:17

Thanks @JamesHill, I did not remember how to do this and your example is excellent. I my case I have added some calculations to x and y in order to make mouse movement time related (pixel per second) - Pimienta

Is this WinForms approach? - greenoldman

I feel like I should mention this so someone doesn't get into the hilarious problem I just had. Cursor.Clip will restrict your mouse's movement to the size specified by Location y Size. So the snippet above will only allow your mouse to move within the application's bounding box. - Brandon

Cursor.Position may require a certain setting if used in a máquina virtual. - Pollitzer

Works fine, and if the Cursor.Clip line is removed it also works when the window is minimized. - Ben

First Add a Class called Win32.cs

public class Win32
    public static extern long SetCursorPos(int x, int y);

    public static extern bool ClientToScreen(IntPtr hWnd, ref POINT point);

    public struct POINT
        public int x;
        public int y;

        public POINT(int X, int Y)
            x = X;
            y = Y;

You can use it then like this:

Win32.POINT p = new Win32.POINT(xPos, yPos);

Win32.ClientToScreen(this.Handle, ref p);
Win32.SetCursorPos(p.x, p.y);

Respondido 13 Oct 20, 12:10

where is POINT Type from? - RollRoll

How do you get the mouse cursor position using this method? - barlop

this is good.. it should be noted that this is relative to the top left of the form. So it's the same coordinates used by e.g. a control on a form, and the same coordinates used in(and - to answer my q in my comment above - that can be gotten from), the MouseEventArgs e, of eg Form's MouseMove method. - barlop

this.Handle implies a WinForm app but you can get a handle to any running window outside of the current running WinForm process. Example: IntPtr desktopWinHandle = Win32.GetDesktopWindow(); where GetDesktopWindow is declared as [DllImport("user32.dll", SetLastError = false)] public static extern IntPtr GetDesktopWindow(); Within the Win32 class. Also checkout the Win32 interop method EnumWindows(). [DllImport("user32.dll")] public static extern int EnumWindows(WndEnumProc lpEnumFunc, int lParam); - Vance McCorkle

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.