我想要做的是操纵鼠标。这对于我自己的目的将是一个简单的宏。所以它会将我的鼠标移动到屏幕上的某个位置,然后像点击某个间隔一样点击。如何在屏幕上的某个位置模拟鼠标点击?
32
A
回答
43
这里是正在使用的非托管函数来模拟鼠标点击代码:
//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);
}
要在特定的时间内按住鼠标,您可以Sleep()
正在执行该功能,例如线程:
mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0);
System.Threading.Thread.Sleep(1000);
mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0);
上面的代码将保持加压1秒钟鼠标除非用户按压释放鼠标按钮。 此外,请确保不要在主UI线程上执行此代码,因为它会导致挂起。
7
您可以按XY位置移动。下面的例子:
windows.Forms.Cursor.Position = New System.Drawing.Point(Button1.Location.X + Me.Location.X + 50, Button1.Location.Y + Me.Location.Y + 30)
要点击,可以使用下面的代码:
using System.Runtime.InteropServices;
private const UInt32 MOUSEEVENTF_LEFTDOWN = 0x0002;
private const UInt32 MOUSEEVENTF_LEFTUP = 0x0004;
[DllImport("user32.dll")]
private static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, uint dwExtraInf);
private void btnSet_Click(object sender, EventArgs e)
{
int x = Convert.ToInt16(txtX.Text);//set x position
int y = Convert.ToInt16(txtY.Text);//set y position
Cursor.Position = new Point(x, y);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);//make left button down
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);//make left button up
}
+1
问题是在WPF,而不是形式。 – Vlad
+1
不起作用:windows.Forms.Cursor.Position =新的System.Drawing.Point(Button1.Location.X + Me.Location.X + 50,Button1.Location.Y + Me.Location.Y + 30)。我也尝试过这种形式没有提到不会在wpf – MonsterMMORPG
相关问题
- 1. 鼠标在屏幕上的Java位置
- 2. 自动在屏幕上点击鼠标
- 3. 如何在屏幕C上的特定位置模拟四键鼠标事件#
- 4. 检查屏幕上的鼠标位置
- 5. 屏幕上的鼠标位置
- 6. 在Mac上模拟鼠标点击
- 7. 模拟移位鼠标点击
- 8. 位置在黑莓屏幕上点击
- 9. 如何在点击某个物体上的鼠标后在屏幕上打印某些内容?
- 10. 如何将鼠标光标位置设置为C#屏幕上的指定点?
- 11. 模拟Python上的鼠标点击
- 12. C#单击屏幕上的某个点
- 13. SharpDX如何让鼠标在游戏屏幕上的位置
- 14. 屏幕在点击鼠标时变黑
- 15. 如何在屏幕上只在鼠标位置blit
- 16. 如何模拟鼠标点击事件
- 17. 如何模拟鼠标点击?
- 18. 如何模拟鼠标点击?
- 19. 模拟鼠标点击QWebEngineView
- 20. 模拟鼠标点击motionevents
- 21. 模拟鼠标点击
- 22. 模拟鼠标点击AS3
- 23. 获取鼠标屏幕坐标点击
- 24. 如何让鼠标在imageview的点击位置上定位?
- 25. 如何在WPF屏幕上获取鼠标位置?
- 26. 如何在Qt屏幕上获得鼠标位置?
- 27. 如何点击屏幕上的坐标?
- 28. winapi在鼠标点击事件的屏幕上显示图标
- 29. C#如何获取屏幕上特定点的坐标。 (不是鼠标位置)
- 30. 如何在已启动的进程上模拟鼠标点击
Would [this](http://stackoverflow.com/questions/8242409/simulate-mouse-clicks-at-a-certain-position-on-inactive-window-in-c-sharp/8242484#8242484)成为你需要的东西?另外,正如有人在评论中提出的建议,您可能想使用UIAutomation。 – Nasreddine
感谢工作:) – MonsterMMORPG
好吧不太好工作。我如何设置点击持续时间。像100毫秒保持按下鼠标 – MonsterMMORPG