我需要在某些位置进行一些自动点击,但是当我将方法放在FOR中时,点击 Action 仅在最后一个循环中执行。
[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();
}
单击操作完美运行,但是当我放入某个循环时,程序仅在最后一个循环中时单击。
如何在循环内请求时执行点击操作?
最佳答案
这可能发生得太快了。尝试在单击事件后添加一个小的延迟,以便操作系统在触发更多事件之前有时间处理它。
关于c# - 如何在循环时执行鼠标单击 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54750972/