VB后台点击,执行窗口指定位置点击.就是相对坐标.问题

2020-04-16 文化 122阅读
Option Explicit
'在VB窗体上添加两个CommandButton,使用默认名称,即可观察测试效果
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function ClientToScreen Lib "user32" (ByVal hWnd As Long, lpPoint As POINTAPI) As Long
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 Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long
Const MOUSEEVENTF_LEFTDOWN = &H2
Const MOUSEEVENTF_LEFTUP = &H4
Const MOUSEEVENTF_MIDDLEDOWN = &H20
Const MOUSEEVENTF_MIDDLEUP = &H40
Const MOUSEEVENTF_MOVE = &H1
Const MOUSEEVENTF_ABSOLUTE = &H8000
Const MOUSEEVENTF_RIGHTDOWN = &H8
Const MOUSEEVENTF_RIGHTUP = &H10
Private Sub Command1_Click()
Dim Point0 As POINTAPI
Dim Point1 As POINTAPI

GetCursorPos Point0 '获取当前鼠标坐标

Point1.X = 2 '指定偏移坐标X值,实际应用时视目标窗体而定
Point1.Y = 2 '指定偏移坐标Y值,实际应用时视目标窗体而定

'将偏移Command2的偏移坐标转换成屏幕坐标
'实际应用时使用指定窗口的句柄
ClientToScreen Command2.hWnd, Point1

'将鼠标移动到转换后的Point1坐标,即指定窗口的偏移坐标
SetCursorPos Point1.X, Point1.Y

'模拟鼠标左键Click动作
mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&

'恢复原鼠标坐标
SetCursorPos Point0.X, Point0.Y
End Sub
Private Sub Command2_Click()
Debug.Print "Click"
End Sub
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com