请问VB如何获取某窗体的句柄,并向该窗口发送后台按键信息?
我想给一个程序发送按键信息。可是在制作过程中发现该软件必须要在不停地升级中才能使用,并且窗口名会根据版本变化而变化。但有一个固定字符串“ClientVer:”我现在想做的...
我想给一个程序发送按键信息。可是在制作过程中发现该软件必须要在不停地升级中才能使用,并且窗口名会根据版本变化而变化。但有一个固定字符串“Client Ver :”我现在想做的就是找到含有“Client Ver :”的窗口句柄并向其发送后台按键信息。真心请教各位大虾,现在此谢谢大家了!
展开
1个回答
展开全部
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 IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long
Private Function GetWinText(ByVal hwnd As Long) As String
GetWinText = String(1024, Chr(0))
GetWindowText hwnd, GetWinText, Len(GetWinText)
GetWinText = Left$(GetWinText, InStr(GetWinText, Chr(0)) - 1)
End Function
Private Sub Command1_Click()
Dim i As Long, j As Long, s As String
i = GetWindow(hwnd, 0&)
Do Until i = 0
If IsWindowVisible(i) Then
s = Trim(GetWinText(i))
If instr(s,"Client Ver :") Then
'这时候i就是该程序的句柄,你可以在此发送按键消息了
'或者你也可以把这个i记录下来,然后在timer中向该窗口定时发送按键消息
exit sub
End If
End If
i = GetWindow(i, 2&)
Loop
End Sub
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 IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long
Private Function GetWinText(ByVal hwnd As Long) As String
GetWinText = String(1024, Chr(0))
GetWindowText hwnd, GetWinText, Len(GetWinText)
GetWinText = Left$(GetWinText, InStr(GetWinText, Chr(0)) - 1)
End Function
Private Sub Command1_Click()
Dim i As Long, j As Long, s As String
i = GetWindow(hwnd, 0&)
Do Until i = 0
If IsWindowVisible(i) Then
s = Trim(GetWinText(i))
If instr(s,"Client Ver :") Then
'这时候i就是该程序的句柄,你可以在此发送按键消息了
'或者你也可以把这个i记录下来,然后在timer中向该窗口定时发送按键消息
exit sub
End If
End If
i = GetWindow(i, 2&)
Loop
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询