VBA調用Sendmessage或PostMessage來模擬鼠標單擊按鈕事件

2017-09-10 18:30:00
zstmtony
原創
4257

註意,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
分享