SendInput不执行点击鼠标按钮,除非我移动光标
本文关键字:非我 移动 光标 按钮 鼠标 执行 SendInput | 更新日期: 2023-09-27 18:13:32
SendInput不执行点击鼠标按钮,除非我移动光标。
如果你能帮助我,我将不胜感激,因为我似乎无法理解它。
我有一个程序,执行鼠标单击前景窗口,其中我使用SendInput来模拟鼠标左键单击。问题是,如果我移动光标到点击位置比SendInput将使点击,然而,如果我不移动光标比没有点击发生,即使我传递x和y点到MouseInputData。我想执行鼠标左键单击,而不需要实际移动光标。
下面是我的类(它相当简单和直接)
namespace StackSolution.Classes
{
public static class SendInputClass
{
[DllImport("user32.dll", SetLastError = true)]
static extern uint SendInput(uint nInputs, ref INPUT pInputs, int cbSize);
[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetCursorPos(out Point lpPoint);
[StructLayout(LayoutKind.Sequential)]
struct INPUT
{
public SendInputEventType type;
public MouseKeybdhardwareInputUnion mkhi;
}
[StructLayout(LayoutKind.Explicit)]
struct MouseKeybdhardwareInputUnion
{
[FieldOffset(0)]
public MouseInputData mi;
[FieldOffset(0)]
public KEYBDINPUT ki;
[FieldOffset(0)]
public HARDWAREINPUT hi;
}
[StructLayout(LayoutKind.Sequential)]
struct KEYBDINPUT
{
public ushort wVk;
public ushort wScan;
public uint dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
struct HARDWAREINPUT
{
public int uMsg;
public short wParamL;
public short wParamH;
}
struct MouseInputData
{
public int dx;
public int dy;
public uint mouseData;
public MouseEventFlags dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}
[Flags]
enum MouseEventFlags : 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
}
enum SendInputEventType : int
{
InputMouse,
InputKeyboard,
InputHardware
}
public static void ClickLeftMouseButton(int x, int y)
{
INPUT mouseInput = new INPUT();
mouseInput.type = SendInputEventType.InputMouse;
mouseInput.mkhi.mi.dx = x;
mouseInput.mkhi.mi.dy = y;
mouseInput.mkhi.mi.mouseData = 0;
//getting current cursor location
Point p;
if (GetCursorPos(out p))
SetCursorPos(x, y);
mouseInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_LEFTDOWN;
SendInput(1, ref mouseInput, Marshal.SizeOf(new INPUT()));
mouseInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_LEFTUP;
SendInput(1, ref mouseInput, Marshal.SizeOf(new INPUT()));
//returning cursor to previous position
SetCursorPos(p.X, p.Y);
}
}
}
相同的ClickLeftMouseButton函数将不会点击,如果我删除获取光标位置。
public static void ClickLeftMouseButton(int x, int y)
{
INPUT mouseInput = new INPUT();
mouseInput.type = SendInputEventType.InputMouse;
mouseInput.mkhi.mi.dx = x;
mouseInput.mkhi.mi.dy = y;
mouseInput.mkhi.mi.mouseData = 0;
mouseInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_LEFTDOWN;
SendInput(1, ref mouseInput, Marshal.SizeOf(new INPUT()));
mouseInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_LEFTUP;
SendInput(1, ref mouseInput, Marshal.SizeOf(new INPUT()));
}
提前感谢。
在使用SendInput
函数时,您应该考虑以下几点:
如果不指定MOUSEEVENTF_ABSOLUTE
标志,则dx
和dy
(MouseInputData结构)是相对于当前鼠标位置的坐标。如果指定MOUSEEVENTF_ABSOLUTE
,则dx
和dy
是0到65535之间的绝对坐标。所以,如果你的x和y坐标是屏幕坐标,你应该使用以下函数来计算dx
和dy
:
enum SystemMetric
{
SM_CXSCREEN = 0,
SM_CYSCREEN = 1,
}
[DllImport("user32.dll")]
static extern int GetSystemMetrics(SystemMetric smIndex);
int CalculateAbsoluteCoordinateX(int x)
{
return (x * 65536) / GetSystemMetrics(SystemMetric.SM_CXSCREEN);
}
int CalculateAbsoluteCoordinateY(int y)
{
return (y * 65536) / GetSystemMetrics(SystemMetric.SM_CYSCREEN);
}
此外,在你通过SendInput发送MOUSEDOWN和MOUSEUP事件之前,你必须将鼠标移动到你想要点击的控件上:
public static void ClickLeftMouseButton(int x, int y)
{
INPUT mouseInput = new INPUT();
mouseInput.type = SendInputEventType.InputMouse;
mouseInput.mkhi.mi.dx = CalculateAbsoluteCoordinateX(x);
mouseInput.mkhi.mi.dy = CalculateAbsoluteCoordinateY(y);
mouseInput.mkhi.mi.mouseData = 0;
mouseInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_MOVE |MouseEventFlags.MOUSEEVENTF_ABSOLUTE;
SendInput(1, ref mouseInput, Marshal.SizeOf(new INPUT()));
mouseInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_LEFTDOWN;
SendInput(1, ref mouseInput, Marshal.SizeOf(new INPUT()));
mouseInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_LEFTUP;
SendInput(1, ref mouseInput, Marshal.SizeOf(new INPUT()));
}
上面的代码假设x
和y
是屏幕像素坐标。您可以使用以下代码计算winform上按钮(目标)的坐标:
Point screenCoordsCentre=
button1.PointToScreen(new Point(button1.Width/2, button1.Height/2));
希望有帮助。