如何用vba控制鼠标移动和点击

2024-12-05 04:13:38
推荐回答(2个)
回答1:

1、首先在开发工具中打开VBA编辑器。

2、在单元格区域当中输入一些自己想要的内容。

3、在VBA编辑器中插入模块。

4、在模块当中输入代首槐码,然知睁后运行。

5、这样用快捷键Alt+F8调出运行宏窗口,然后单击“执行”按钮,鼠标已限定在功能区范围移动了搭芹岁。

回答2:

Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Private Const MOUSEEVENTF_ABSOLUTE = &H8000 '指定鼠标使用绝对竖悉坐标系,此时,段哪屏幕在水平和垂直方向上均匀分割成65535×65535个单握纤码元
Private Const MOUSEEVENTF_MOVE = &H1 '移动鼠标
Private Const MOUSEEVENTF_LEFTDOWN = &H2 '模拟鼠标左键按下
Private Const MOUSEEVENTF_LEFTUP = &H4 '模拟鼠标左键抬起

Private Const SW = 1024
Private Const SH = 768

Private Sub Screen_Click(ByVal x As Long, ByVal y As Long)
mw = x / SW * 65535
mh = y / SH * 65535
'移动
mouse_event MOUSEEVENTF_ABSOLUTE Or MOUSEEVENTF_MOVE, mw, mh, 0, 0
'单击
mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
End Sub

Sub TEST()
Screen_Click 512, 384
End Sub