如何在C#中将鼠标光标位置设置到屏幕上的指定点?

如何在C#中将鼠标光标位置设置到屏幕上的指定点?

我必须hacke主板缓冲区接收鼠标和键盘坐标并按???

是否有另一个做点击或我想象?

以下将设置鼠标位置并执行单击:

public static void ClickSomePoint() { // Set the cursor position System.Windows.Forms.Cursor.Position = new Point(20, 35); DoClickMouse(0x2); // Left mouse button down DoClickMouse(0x4); // Left mouse button up } static void DoClickMouse(int mouseButton) { var input = new INPUT() { dwType = 0, // Mouse input mi = new MOUSEINPUT() { dwFlags = mouseButton } }; if (SendInput(1, input, Marshal.SizeOf(input)) == 0) { throw new Exception(); } } [StructLayout(LayoutKind.Sequential)] struct MOUSEINPUT { int dx; int dy; int mouseData; public int dwFlags; int time; IntPtr dwExtraInfo; } struct INPUT { public uint dwType; public MOUSEINPUT mi; } [DllImport("user32.dll", SetLastError=true)] static extern uint SendInput(uint cInputs, INPUT input, int size); 

请记住,这对用户来说可能非常烦人。

🙂


如果要单击表单上的按钮,可以使用'PerformClick()'方法。