html模拟鼠标点击桌面,c# – 如何在屏幕上的某个位置模拟鼠标点击?

这是一个使用非托管函数来模拟鼠标点击的代码:

//This is a replacement for Cursor.Position in WinForms

[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 = 0x02;

public const int MOUSEEVENTF_LEFTUP = 0x04;

//This simulates a left mouse click

public static void LeftMouseClick(int xpos, int ypos)

{

SetCursorPos(xpos, ypos);

mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0);

mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0);

}

要保持鼠标按下一段特定的持续时间,您可以睡眠()执行此功能的线程,例如:

mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0);

System.Threading.Thread.Sleep(1000);

mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0);

上述代码将保持鼠标按下1秒,除非用户按下释放鼠标按钮。另外,请确保在主UI线程上不执行此代码,因为它会导致它挂起。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值