public static void SendMouseInput(int positionX, int positionY, int maxX, int maxY, bool leftDown) { if (positionX > int.MaxValue) { throw new ArgumentOutOfRangeException("positionX"); } if (positionY > int.MaxValue) { throw new ArgumentOutOfRangeException("positionY"); } Input[] i = new Input[2]; i[0] = new Input(); i[0].Type = InputMouse; i[0].MouseInput.X = (positionX * 70000) / maxX; i[0].MouseInput.Y = (positionY * 70000) / maxY; i[0].MouseInput.Flags = MouseEventAbsolute | MouseEventMove; if (!lastLeftDown && leftDown) { i[1] = new Input(); i[1].Type = InputMouse; i[1].MouseInput.Flags = MouseEventLeftDown; } else if (lastLeftDown && !leftDown) { i[1] = new Input(); i[1].Type = InputMouse; i[1].MouseInput.Flags = MouseEventLeftUp; } lastLeftDown = leftDown; // send it off uint result = SendInput(2, i, Marshal.SizeOf(i[0])); if (result == 0) throw new Win32Exception(Marshal.GetLastWin32Error()); }
private static extern uint SendInput(uint numInputs, Input[] inputs, int size);