模拟Mouseclick - 鼠标不移动

时间:2016-08-05 11:42:00

标签: c# click mouse

我尝试了How to simulate Mouse Click in C#?

public void DoMouseClick(uint X, uint Y)
    {
        //Call the imported function with the cursor's current position
        mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
    }

我的问题是,点击在该点上执行,我的光标是,而不是在那一点,我设置为x和y。我的坐标没错。我也试过x = 1和y = 1。

1 个答案:

答案 0 :(得分:1)

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    static extern bool SetCursorPos(int x, int y);

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

    public const int MOUSEEVENTF_LEFTDOWN = 0x0002;//0x02;
    public const int MOUSEEVENTF_LEFTUP = 0x0004;//0x04;

    public void Click(int x, int y)
    {
        Thread.Sleep(2000);
        SetCursorPos(x, y);
        Thread.Sleep(1000);
        mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
        Thread.Sleep(1000);
        mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
    }