已知另外一个程序里的按钮句柄,通过vb程序里代码控制另外一个程序里的按钮单击事件:—————————————————————————————————————————— 首先应该添加以下声明: Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) 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 SendMessageByString Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As String) As LongPrivate Const BM_CLICK = &HF5'然后使用以下代码:对按钮发送BM_Click消息点击;至于传入数据使用SendMessageByString,这个在此不多说了 Dim hBtn As Long Dim hParent As Long hParent = FindWindow("按钮父窗口的类名", "按钮的父窗口名称") '通过窗体类获取窗体句柄 hBtn = FindWindowEx(hParent, Null, Null, "按钮的标题") '获取按钮句柄 SendMessage hBtn, BM_CLICK, 0, 0 '触发按钮的点击事件
用以下方法成功,分享一下死辦法:Option Explicit 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 SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Private Declare Function SendMessageByStr& Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long Dim MsnFrame As Long, I As LongPrivate Sub Command1_Click() MsnFrame = 0: I = 0 If FindWindow(vbNullString, "MSN Messenger") = 0 Then Shell "C:\Program Files\MSN Messenger\msnmsgr.exe", vbNormalFocus End If
Timer1.Enabled = True Timer1.Interval = 1000 End SubPrivate Sub Timer1_Timer() I = I + 1 If I = 5 Then Timer1.Enabled = False If FindWindow(vbNullString, "MSN Messenger") > 0 Then MsnFrame = FindWindow(vbNullString, "MSN Messenger") If MsnFrame > 0 Then
Else MsgBox "Error1" End If Else MsgBox "Error2" End If SetForegroundWindow MsnFrame SendKeys "[email protected]" SendKeys "{Tab}" SendKeys "123456789" SendKeys "{Tab}" SendKeys "{Tab}" SendKeys "{Tab}" SendKeys "{Tab}" SendKeys "{Tab}" SendKeys "{Enter}" End If End Sub
——————————————————————————————你说的是什么控制?
http://community.csdn.net/Expert/topic/4351/4351404.xml?temp=7.021731E-02然后对帐号/密码Text来SendMessageByString
最后对登录Button来SendMessage就行。
首先应该添加以下声明:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) 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 SendMessageByString Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As String) As LongPrivate Const BM_CLICK = &HF5'然后使用以下代码:对按钮发送BM_Click消息点击;至于传入数据使用SendMessageByString,这个在此不多说了
Dim hBtn As Long
Dim hParent As Long
hParent = FindWindow("按钮父窗口的类名", "按钮的父窗口名称") '通过窗体类获取窗体句柄
hBtn = FindWindowEx(hParent, Null, Null, "按钮的标题") '获取按钮句柄
SendMessage hBtn, BM_CLICK, 0, 0 '触发按钮的点击事件
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 SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function SendMessageByStr& Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String)
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Dim MsnFrame As Long, I As LongPrivate Sub Command1_Click()
MsnFrame = 0: I = 0
If FindWindow(vbNullString, "MSN Messenger") = 0 Then
Shell "C:\Program Files\MSN Messenger\msnmsgr.exe", vbNormalFocus
End If
Timer1.Enabled = True
Timer1.Interval = 1000
End SubPrivate Sub Timer1_Timer()
I = I + 1
If I = 5 Then
Timer1.Enabled = False
If FindWindow(vbNullString, "MSN Messenger") > 0 Then
MsnFrame = FindWindow(vbNullString, "MSN Messenger")
If MsnFrame > 0 Then
Else
MsgBox "Error1"
End If
Else
MsgBox "Error2"
End If
SetForegroundWindow MsnFrame
SendKeys "[email protected]"
SendKeys "{Tab}"
SendKeys "123456789"
SendKeys "{Tab}"
SendKeys "{Tab}"
SendKeys "{Tab}"
SendKeys "{Tab}"
SendKeys "{Tab}"
SendKeys "{Enter}"
End If
End Sub