public static void Action(double x, double y, MouseAction buttonState) { Point point = new Point((int)x, (int)y); Cursor.Position = point; NativeMethods.INPUT[] input = new NativeMethods.INPUT[1]; NativeMethods.MOUSEINPUT mi = new NativeMethods.MOUSEINPUT(); mi.dx = 0; mi.dy = 0; if (buttonState == MouseAction.LeftDown) { mi.dwFlags = NativeMethods.MOUSEEVENTF_LEFTDOWN | NativeMethods.MOUSEEVENTF_ABSOLUTE; } else if (buttonState == MouseAction.LeftUp) { mi.dwFlags = NativeMethods.MOUSEEVENTF_LEFTUP | NativeMethods.MOUSEEVENTF_ABSOLUTE; } else if (buttonState == MouseAction.RightDown) { mi.dwFlags = NativeMethods.MOUSEEVENTF_RIGHTDOWN | NativeMethods.MOUSEEVENTF_ABSOLUTE; } else if (buttonState == MouseAction.RightUp) { mi.dwFlags = NativeMethods.MOUSEEVENTF_RIGHTUP | NativeMethods.MOUSEEVENTF_ABSOLUTE; } input[0] = new NativeMethods.INPUT(); input[0].mi = mi; NativeMethods.SendInput((uint)input.Length, input, Marshal.SizeOf(input[0])); }
private static void DoMouse(NativeMethods.MOUSEEVENTF flags, Point newPoint, int scrollSize = 0) { NativeMethods.INPUT input = new NativeMethods.INPUT(); NativeMethods.MOUSEINPUT mi = new NativeMethods.MOUSEINPUT(); input.dwType = NativeMethods.InputType.Mouse; input.mi = mi; input.mi.dwExtraInfo = IntPtr.Zero; // mouse co-ords: top left is (0,0), bottom right is (65535, 65535) // convert screen co-ord to mouse co-ords... input.mi.dx = newPoint.X * 65535 / Screen.PrimaryScreen.Bounds.Width; input.mi.dy = newPoint.Y * 65535 / Screen.PrimaryScreen.Bounds.Height; input.mi.time = 0; input.mi.mouseData = scrollSize * 120; // can be used for WHEEL event see msdn input.mi.dwFlags = flags; int cbSize = Marshal.SizeOf(typeof(NativeMethods.INPUT)); int result = NativeMethods.SendInput(1, ref input, cbSize); if (result == 0) { Debug.WriteLine(Marshal.GetLastWin32Error()); } }
public static void Action(double x, double y, MouseAction buttonState) { Point point = new Point((int)x, (int)y); Cursor.Position = point; NativeMethods.INPUT[] input = new NativeMethods.INPUT[1]; NativeMethods.MOUSEINPUT mi = new NativeMethods.MOUSEINPUT(); mi.dx = 0; mi.dy = 0; if (buttonState == MouseAction.LeftDown) mi.dwFlags = NativeMethods.MOUSEEVENTF_LEFTDOWN | NativeMethods.MOUSEEVENTF_ABSOLUTE; else if (buttonState == MouseAction.LeftUp) mi.dwFlags = NativeMethods.MOUSEEVENTF_LEFTUP | NativeMethods.MOUSEEVENTF_ABSOLUTE; else if (buttonState == MouseAction.RightDown) mi.dwFlags = NativeMethods.MOUSEEVENTF_RIGHTDOWN | NativeMethods.MOUSEEVENTF_ABSOLUTE; else if (buttonState == MouseAction.RightUp) mi.dwFlags = NativeMethods.MOUSEEVENTF_RIGHTUP | NativeMethods.MOUSEEVENTF_ABSOLUTE; input[0] = new NativeMethods.INPUT(); input[0].mi = mi; NativeMethods.SendInput((uint)input.Length, input, Marshal.SizeOf(input[0])); }
private void DoMouseInput(int x, int y, bool absoluteCoordinates, bool? mouseDown) { // TODO: Maybe we should instead send WM_MOUSEMOVE, WM_LBUTTONDOWN etc. // messages directly to the destination window so that we don't need to // position the mouse cursor which makes it harder e.g. to // click on the "Stop" button of the simulator. // Convert the screen coordinates into mouse coordinates. Coordinates cs = new Coordinates(x, y); cs = GetMouseCoordinatesFromScreenCoordinates(cs); var mi = new NativeMethods.MOUSEINPUT(); mi.dx = cs.X; mi.dy = cs.Y; if (absoluteCoordinates) mi.dwFlags |= NativeMethods.MOUSEEVENTF.ABSOLUTE; if (!(!absoluteCoordinates && x == 0 && y == 0)) { // A movement occured. mi.dwFlags |= NativeMethods.MOUSEEVENTF.MOVE; } if (mouseDown.HasValue) { mi.dwFlags |= mouseDown.Value ? NativeMethods.MOUSEEVENTF.LEFTDOWN : NativeMethods.MOUSEEVENTF.LEFTUP; } var input = new NativeMethods.INPUT(); input.type = NativeMethods.INPUT_MOUSE; input.U.mi = mi; NativeMethods.INPUT[] inputs = { input }; if (NativeMethods.SendInput(1, inputs, NativeMethods.INPUT.Size) == 0) throw new Win32Exception(); }
private static void DoMouse( NativeMethods.MOUSEEVENTF flags, Point newPoint, int scrollSize = 0 ) { NativeMethods.INPUT input = new NativeMethods.INPUT(); NativeMethods.MOUSEINPUT mi = new NativeMethods.MOUSEINPUT(); input.dwType = NativeMethods.InputType.Mouse; input.mi = mi; input.mi.dwExtraInfo = IntPtr.Zero; // mouse co-ords: top left is (0,0), bottom right is (65535, 65535) // convert screen co-ord to mouse co-ords... input.mi.dx = newPoint.X * 65535 / Screen.PrimaryScreen.Bounds.Width; input.mi.dy = newPoint.Y * 65535 / Screen.PrimaryScreen.Bounds.Height; input.mi.time = 0; input.mi.mouseData = scrollSize * 120; // can be used for WHEEL event see msdn input.mi.dwFlags = flags; int cbSize = Marshal.SizeOf( typeof ( NativeMethods.INPUT ) ); int result = NativeMethods.SendInput( 1, ref input, cbSize ); // if ( result == 0 ) // Debug.WriteLine( Marshal.GetLastWin32Error() ); }
/// <summary> /// Send mouse click to relative position in the client window. /// </summary> /// <param name="client">Target client.</param> /// <param name="button">System.Windows.Forms.MouseButtons to click.</param> /// <param name="x">X position of point to click relative to client window's X position.</param> /// <param name="y">Y position of point to click relative to client window's Y position.</param> /// <param name="doubleClick">True for double click, false for single click.</param> /// <returns>True on success.</returns> public static bool SendMouseClick(int client, MouseButtons button, int x, int y, bool doubleClick) { ClientInfo ci; if (ClientInfoCollection.GetClient(client, out ci)) { NativeMethods.INPUT[] inputs = new NativeMethods.INPUT[2]; NativeMethods.MOUSEINPUT mi = new NativeMethods.MOUSEINPUT(); inputs[0].type = NativeMethods.INPUT_MOUSE; inputs[1].type = NativeMethods.INPUT_MOUSE; mi.dx = 0; mi.dy = 0; switch (button) { case MouseButtons.None: return(false); case MouseButtons.Left: mi.dwFlags = NativeMethods.MOUSEEVENTF_LEFTDOWN; inputs[0].mkhi.mi = mi; mi.dwFlags = NativeMethods.MOUSEEVENTF_LEFTUP; inputs[1].mkhi.mi = mi; break; case MouseButtons.Right: mi.dwFlags = NativeMethods.MOUSEEVENTF_RIGHTDOWN; inputs[0].mkhi.mi = mi; mi.dwFlags = NativeMethods.MOUSEEVENTF_RIGHTUP; inputs[1].mkhi.mi = mi; break; case MouseButtons.Middle: mi.dwFlags = NativeMethods.MOUSEEVENTF_MIDDLEDOWN; inputs[0].mkhi.mi = mi; mi.dwFlags = NativeMethods.MOUSEEVENTF_MIDDLEUP; inputs[1].mkhi.mi = mi; break; case MouseButtons.XButton1: mi.mouseData = NativeMethods.XBUTTON1; mi.dwFlags = NativeMethods.MOUSEEVENTF_XDOWN; inputs[0].mkhi.mi = mi; mi.dwFlags = NativeMethods.MOUSEEVENTF_XUP; inputs[1].mkhi.mi = mi; break; case MouseButtons.XButton2: mi.mouseData = NativeMethods.XBUTTON2; mi.dwFlags = NativeMethods.MOUSEEVENTF_XDOWN; inputs[0].mkhi.mi = mi; mi.dwFlags = NativeMethods.MOUSEEVENTF_XUP; inputs[1].mkhi.mi = mi; break; } if (!ci.PrepareWindowForInput()) { ci.DetachFromWindow(); return(false); } NativeMethods.WINDOWPLACEMENT wp = new NativeMethods.WINDOWPLACEMENT(); if (NativeMethods.GetWindowPlacement(ci.WindowHandle, ref wp)) { /*int screenX = wp.rcNormalPosition.X + x; * int screenY = wp.rcNormalPosition.Y + y; * int absX = (screenX * 65536 / Screen.PrimaryScreen.Bounds.Width); * int absY = (screenY * 65536 / Screen.PrimaryScreen.Bounds.Height); * inputs[0].mkhi.mi.dx = absX; * inputs[1].mkhi.mi.dx = absX; * inputs[0].mkhi.mi.dy = absY; * inputs[1].mkhi.mi.dy = absY;*/ // using this method because absolute position is always off by a pixel or 2 if (!NativeMethods.SetCursorPos(x + wp.rcNormalPosition.X, y + wp.rcNormalPosition.Y)) { return(false); } } else { return(false); } uint success = NativeMethods.SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(inputs[0])); if (doubleClick && success == inputs.Length) { success = NativeMethods.SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(inputs[0])); } ci.DetachFromWindow(); return(success == inputs.Length); } return(false); }