如何使用C#移动鼠标光标?
我想每x秒模拟鼠标移动。 为此,我将使用计时器(x秒),当计时器滴答时,我将使鼠标移动。
但是,我怎样才能使鼠标光标移动使用C#?
看看Cursor.Position
属性 。 它应该让你开始。
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); }
首先添加类(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; } }
然后调用它从事件:
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);