鼠标单击代码

本文关键字:代码 单击 鼠标 | 更新日期: 2023-09-27 18:28:28

我正试图通过代码点击桌面,所以我做到了:

    public static void MouseLeftClick(Point pos)
    {
        System.Windows.Forms.Cursor.Position = pos;
        mouse_event(MOUSEEVENTF_LEFTDOWN, pos.X, pos.Y, 0, 0);
        mouse_event(MOUSEEVENTF_LEFTUP, pos.X, pos.Y, 0, 0);
    }

我意识到只有当我添加System.Windows.Forms.Cursor.Position=pos;为什么?mouse_event x,y参数没用?

鼠标单击代码

您读过mouse_event函数的描述吗?

例如,如果仅使用RIGHTDOWN,则X和Y参数不表示设置鼠标的坐标。。

以下是如何处理mouse事件:

[DllImport("user32.dll")]
private static extern void mouse_event(MouseEventFlags dwFlags, uint dx, uint dy, uint dwData, UIntPtr dwExtraInfo);
...
// Converts into pixels
uint x = (uint)(pos.X * 65535 / Screen.PrimaryScreen.Bounds.Width);
uint y = (uint)(pos.Y * 65535 / Screen.PrimaryScreen.Bounds.Height);
// Moves the mouse (absolute)
mouse_event(MouseEventFlags.MOVE | MouseEventFlags.ABSOLUTE, x, y, 0, UIntPtr.Zero);
// Now button down
mouse_event(MouseEventFlags.RIGHTDOWN, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlags.RIGHTUP, 0, 0, 0, UIntPtr.Zero);

当然,设置光标位置要比使用mouse_event来判断鼠标已经移动简单得多。

顺便说一句,这个功能已经被取代了。请改用SendInput

相关文章: