循环时如何执行鼠标点击动作

时间:2019-02-18 15:52:27

标签: c#

我需要在某些位置进行一些自动单击,但是当我将方法放入FOR中时,click操作仅在最后一个循环中执行。

[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
//Mouse actions
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
private const int MOUSEEVENTF_RIGHTUP = 0x10;
public void DoMouseClick()
{
    // Call the imported function with the cursor's current position
    mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}

[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);

public static void MoveCursorToPoint(int x, int y)
{
    SetCursorPos(x, y);
}

我希望如何使用:

for (int i = 0; i <= 3; i++)
{
    MoveCursorToPoint(100, 100);
    DoMouseClick();
}

click动作效果很好,但是当我放入某个循环时,程序仅在最后一个循环中才单击。

每次在循环内被请求时如何执行点击操作?

1 个答案:

答案 0 :(得分:1)

它可能发生得太快。尝试在click事件之后添加一个小的延迟,以使操作系统有时间在触发更多事件之前对其进行处理。