office交流网--QQ交流群号

Access培训群:792054000         Excel免费交流群群:686050929          Outlook交流群:221378704    

Word交流群:218156588             PPT交流群:324131555

VBA调用Sendmessage或PostMessage来模拟鼠标单击按钮事件

2017-09-10 18:30:00
zstmtony
原创
18767

注意,PostMessage是马上返回,而SendMessage则会等待执行完成后才返回

确实遇到过用sendMessage发送消息时有时候写的软件卡住了  用PostMessage就可以  


'用类似的方法可以实现类似游戏外挂或修改器的软件,或其它模拟鼠标功能的软件 Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Any) As Long
'鼠标按下
Const WM_LBUTTONDOWN = &H201
'鼠标弹出
Const WM_LBUTTONUP = &H202
'鼠标双击
Const WM_COMMAND = &H203
 
Private Sub Command1_Click()
Dim lngHWnd1 As Long, lngHWnd2 As Long
 
lngHWnd1 = FindWindow(vbNullString, "你要控制窗体的标题")
  If lngHWnd1 <> 0 Then
  lngHWnd2 = FindWindowEx(lngHWnd1, 0, vbNullString, "里面窗体或控件的标题")
   
  If lngHWnd2 <> 0 Then
  SendMessage lngHWnd2, WM_LBUTTONDOWN, 0&, 0&
  SendMessage lngHWnd2, WM_LBUTTONUP, 0&, 0&
   
  End If
  Else
  lngHWnd2 = 0
  End If
End Sub
分享