怎么用 postmessage 模拟鼠标点击 Command1
本窗口句柄me.hwnd,本窗口内的Command1控件.
怎么用 postmessage 模拟鼠标点击 Command1 ,
哪位VB友帮写个示例源码,通过模拟鼠标点击
Command1,而触发Click或Down事件.谢谢了
[解决办法]
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As LongPrivate Const WM_LBUTTONDOWN = &H201Private Const WM_LBUTTONUP = &H202Private Sub Command1_Click() MsgBox "123"End SubPrivate Sub Command2_Click() PostMessage Command1.hwnd, WM_LBUTTONDOWN, 0, 0 PostMessage Command1.hwnd, WM_LBUTTONUP, 0, 0End Sub
[解决办法]
'添加 Command1 Command2 点击 Command2后PostMessage模拟点击 Command1
'本窗口就用Me.hwnd不必去寻找
'本代码是可以让你更灵活的学习寻找指定窗口下的子句柄
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Const BM_CLICK = &HF5
Const GW_CHILD = 5
Const GW_HWNDNEXT = &H2
Dim HwndVal&, Rtn&, SchWindow$, FindStr$
Private Sub Form_Load()
Me.Caption = "模拟鼠标点击"
SchWindow = Me.Caption '寻找的窗口与下面寻找的标题, 两者皆可自定义,灵活运用,本窗口就直接Me.Hwnd即可
FindStr = "Command1"
End Sub
Private Sub Command1_Click()
MsgBox "您点击了" & FindStr
End Sub
Private Sub Command2_Click()
Rtn = GetWinHwnd(SchWindow, UCase(FindStr)) '返回寻找的窗口句柄
If Rtn > 0 Then PostMessage HwndVal, BM_CLICK, ByVal 0&, ByVal 0&
End Sub
Function GetWinHwnd(Title As String, Fstr As String) As Long
Dim Tmp$
Dim StrTitle As String * 255 '用来存储窗口的标题
HwndVal = FindWindow(vbNullString, Title) '取得寻找窗口的句柄
If HwndVal = 0 Then MsgBox "无此标题的窗口!": Exit Function
HwndVal = GetWindow(HwndVal, GW_CHILD) '取得寻找窗口的第一个子窗口
Do
DoEvents
GetWindowText HwndVal, StrTitle, Len(StrTitle) '取得下一个窗口的标题
If Left(StrTitle, 1) <> vbNullChar Then
Tmp = Left(StrTitle, InStr(StrTitle, vbNullChar) - 1) '-1是排除Chr(0)
If UCase(Trim(Tmp)) = Fstr Then GetWinHwnd = HwndVal: Exit Do
End If
HwndVal = GetWindow(HwndVal, GW_HWNDNEXT) 'GW_HWNDNEXT 用来取得下一个窗口
Loop
End Function