c# - 如何在循环时执行鼠标单击 Action

标签 c#

我需要在某些位置进行一些自动点击,但是当我将方法放在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/

相关文章:

c# - 当值为 null 时,将可空 bool 值传递给部分 View 不起作用

c# - 如何更改 MVC 中子 Action 的顺序

c# - C# 中的 WeakReference - 操作实例是如何丢失的?

c# - 更改winform应用程序中所有表单的背景颜色

c# - 如何在 ASP.NET Core 中使用 HttpContextAccessor 读取 URI 参数

c# - 如何获得 ASP.NET MVC Ajax 响应以重定向到新页面而不是将 View 插入 UpdateTargetId?

c# - 如何从列表中删除冗余顶点

c# - Typedefs、C++/CLI、C# + 依赖注入(inject)

c# - 连接 C# 和 Java 的最佳方式是什么?

state-pattern - 状态模式可以帮助处理只读状态吗?