如何使用 C # 移动鼠标光标?

我想模拟鼠标每 x 秒的移动。为此,我将使用一个计时器(x 秒) ,当计时器滴答作响时,我将使鼠标移动。

但是,如何使用 C # 使鼠标光标移动呢?

194439 次浏览

Take a look at the Cursor.Position Property. 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);
}

First Add a Class called 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;


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