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 Sub Command1_Click()
Dim hw As Long
hw = FindWindowEx(0&, 0&, vbNullString, "窗口标题写在这")
If hw > 0 Then
hw = FindWindowEx(hw, 0&, vbNullString, "按钮文字写在这")
If hw > 0 Then
'按钮找到了,hw就是其句柄,可进一步处理
Else
'按钮没找到,按钮有可能不是窗口的子窗口。你可以用Spy++之类的软件检测一下
End If
Else
'窗口没找到,你可以改用类来查找。可以用Spy++之类的软件检测一下。或者改用枚举的方法
End If
End Sub
这里有一个通用型的代码,移动鼠标到目标上即可
你需要在窗体划两个Label 一个Timer
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) 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 Type POINTAPI
x As Long
y As Long
End Type
Dim n As POINTAPI
Dim a As Long
Private Sub Form_Load()
Timer1.Interval = 100
Label1.Caption = "移动鼠标指针"
End Sub
Private Sub Timer1_Timer()
GetCursorPos n
a = WindowFromPoint(n.x, n.y)
If a <> 0 Then
Dim s As String
s = String(100, Chr(0))
GetWindowText a, s, 100
Label1.Caption = "目标标题或文本: " & Trim(s)
Label2.Caption = "目标句柄为 " & a
End If
End Sub
更多看参考资料
Private Const WM_LBUTTONDOWN = &H201
Private Const WM_LBUTTONUP = &H202
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, lParam As Any) As Long
Private Sub Form_Load()
Shell "calc.exe", vbNormalNoFocus
End Sub
Private Sub Command1_Click()
Dim Hwnd1 As Long
Dim Hwnd2 As Long
Hwnd1 = FindWindow("SciCalc", "计算器")
Hwnd2 = FindWindowEx(Hwnd1, 0, "Button", "1")
SendMessage Hwnd2, WM_LBUTTONDOWN, ByVal 0&, ByVal 0&
SendMessage Hwnd2, WM_LBUTTONUP, ByVal 0&, ByVal 0&
End Sub
可以,findwindow之类的api就是做这个用的,之后就可以用sendmessage向取得的句柄发送各种消息,比如点击、设定文本等等。