Simulate mouseclick??
-
Hi, I want to send a mouseclick to a defined window in c#, without moving the cursor. It should just leftclick on a specified (X,Y) position on the screen. Can i do that without moving the cursor? And can it work if the target window is hidden/minimized? Hope someone can give me a code example :)
-
Hi, I want to send a mouseclick to a defined window in c#, without moving the cursor. It should just leftclick on a specified (X,Y) position on the screen. Can i do that without moving the cursor? And can it work if the target window is hidden/minimized? Hope someone can give me a code example :)
Here you go!
[Flags]
public enum MouseEvent : uint
{
MOUSEEVENTF_MOVE = 0x0001,
MOUSEEVENTF_LEFTDOWN = 0x0002,
MOUSEEVENTF_LEFTUP = 0x0004,
MOUSEEVENTF_RIGHTDOWN = 0x0008,
MOUSEEVENTF_RIGHTUP = 0x0010,
MOUSEEVENTF_MIDDLEDOWN = 0x0020,
MOUSEEVENTF_MIDDLEUP = 0x0040,
MOUSEEVENTF_XDOWN = 0x0080,
MOUSEEVENTF_XUP = 0x0100,
MOUSEEVENTF_WHEEL = 0x0800,
MOUSEEVENTF_VIRTUALDESK = 0x4000,
MOUSEEVENTF_ABSOLUTE = 0x8000
}public enum InputType : uint
{
INPUT_MOUSE = 0,
INPUT_KEYBOARD = 1,
INPUT_HARDWARE = 2
}[StructLayout(LayoutKind.Sequential)]
public struct MOUSEINPUT
{
private InputType type;
public UInt32 dx;
public UInt32 dy;
public UInt32 mouseData;
public MouseEvent dwFlags;
public UInt32 time;
public IntPtr dwExtraInfo;
}public static class MouseInput
{
[DllImport("User32.dll")]
public static extern UInt32 SendInput(UInt32 nInputs, MOUSEINPUT[] pInputs, int cbSize);private static Point Normalize(Point pt) { return new Point((pt.X \* 65535) / Screen.PrimaryScreen.Bounds.Width, (pt.Y \* 65535) / Screen.PrimaryScreen.Bounds.Height); } public static void Move(Point pt) { pt = Normalize(pt); MOUSEINPUT\[\] input = new MOUSEINPUT\[1\]; input\[0\].dx = Convert.ToUInt32(pt.X); input\[0\].dy = Convert.ToUInt32(pt.Y); input\[0\].dwFlags = MouseEvent.MOUSEEVENTF\_MOVE | MouseEvent.MOUSEEVENTF\_ABSOLUTE; SendInput(1, input, Marshal.SizeOf(input\[0\])); } public static void Click(Point pt) { Move(pt); pt = Normalize(pt); MOUSEINPUT\[\] input = new MOUSEINPUT\[2\]; input\[0\].dx = Convert.ToUInt32(pt.X); input\[0\].dy = Convert.ToUInt32(pt.Y); input\[0\].dwFlags = MouseEvent.MOUSEEVENTF\_LEFTDOWN | MouseEvent.MOUSEEVENTF\_ABSOLUTE; input\[1\].dx = Convert.ToUInt32(pt.X); input\[1\].dy = Convert.ToUInt32(pt.Y); input\[1\].dwFlags = MouseEvent.MOUSEEVENTF\_LEFTUP | MouseEvent.MOUSEEVENTF\_ABSOLUTE; SendInput(2, input, Marshal.SizeOf(input\[0\])); }
}
To simulate a click at
100,100
therefore you'd do this:MouseInput.Click( new Point( 100, 100 ) );
I don't think you can do it without moving the cursor. You could save the current mouse position, click at the point where you want to click and then move right back to the saved position!
-
Here you go!
[Flags]
public enum MouseEvent : uint
{
MOUSEEVENTF_MOVE = 0x0001,
MOUSEEVENTF_LEFTDOWN = 0x0002,
MOUSEEVENTF_LEFTUP = 0x0004,
MOUSEEVENTF_RIGHTDOWN = 0x0008,
MOUSEEVENTF_RIGHTUP = 0x0010,
MOUSEEVENTF_MIDDLEDOWN = 0x0020,
MOUSEEVENTF_MIDDLEUP = 0x0040,
MOUSEEVENTF_XDOWN = 0x0080,
MOUSEEVENTF_XUP = 0x0100,
MOUSEEVENTF_WHEEL = 0x0800,
MOUSEEVENTF_VIRTUALDESK = 0x4000,
MOUSEEVENTF_ABSOLUTE = 0x8000
}public enum InputType : uint
{
INPUT_MOUSE = 0,
INPUT_KEYBOARD = 1,
INPUT_HARDWARE = 2
}[StructLayout(LayoutKind.Sequential)]
public struct MOUSEINPUT
{
private InputType type;
public UInt32 dx;
public UInt32 dy;
public UInt32 mouseData;
public MouseEvent dwFlags;
public UInt32 time;
public IntPtr dwExtraInfo;
}public static class MouseInput
{
[DllImport("User32.dll")]
public static extern UInt32 SendInput(UInt32 nInputs, MOUSEINPUT[] pInputs, int cbSize);private static Point Normalize(Point pt) { return new Point((pt.X \* 65535) / Screen.PrimaryScreen.Bounds.Width, (pt.Y \* 65535) / Screen.PrimaryScreen.Bounds.Height); } public static void Move(Point pt) { pt = Normalize(pt); MOUSEINPUT\[\] input = new MOUSEINPUT\[1\]; input\[0\].dx = Convert.ToUInt32(pt.X); input\[0\].dy = Convert.ToUInt32(pt.Y); input\[0\].dwFlags = MouseEvent.MOUSEEVENTF\_MOVE | MouseEvent.MOUSEEVENTF\_ABSOLUTE; SendInput(1, input, Marshal.SizeOf(input\[0\])); } public static void Click(Point pt) { Move(pt); pt = Normalize(pt); MOUSEINPUT\[\] input = new MOUSEINPUT\[2\]; input\[0\].dx = Convert.ToUInt32(pt.X); input\[0\].dy = Convert.ToUInt32(pt.Y); input\[0\].dwFlags = MouseEvent.MOUSEEVENTF\_LEFTDOWN | MouseEvent.MOUSEEVENTF\_ABSOLUTE; input\[1\].dx = Convert.ToUInt32(pt.X); input\[1\].dy = Convert.ToUInt32(pt.Y); input\[1\].dwFlags = MouseEvent.MOUSEEVENTF\_LEFTUP | MouseEvent.MOUSEEVENTF\_ABSOLUTE; SendInput(2, input, Marshal.SizeOf(input\[0\])); }
}
To simulate a click at
100,100
therefore you'd do this:MouseInput.Click( new Point( 100, 100 ) );
I don't think you can do it without moving the cursor. You could save the current mouse position, click at the point where you want to click and then move right back to the saved position!