我正在使用网络浏览器控件,我正在打开一个网站。在网站的第三页,有一个按钮。因此,在加载第3页后,我想以编程方式按TAB键,将控件焦点置于按钮上,然后在加载后立即以编程方式在网站的第三页上按ENTER键。我正在使用vb.net 2008平台来做到这一点。请有人告诉我解决方案???
答案 0 :(得分:0)
首先,您需要找到要将密钥发送到的窗口句柄,然后发送您想要的实际密钥。以下是执行此操作所需的方法:
'API calls
Public Declare Function FindWindowExA Lib "user32.dll" (ByVal hWnd1 As IntPtr, ByVal hWnd2 As Int32, ByVal lpsz1 As String, ByVal lpsz2 As String) As IntPtr
Public Declare Function SendMessageA Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
'Function to find window by either ClassName or WindowText
Public Shared Function FindWindowEx(ByVal hWndParent As IntPtr, ByVal hWndChildAfter As Int32, ByVal ClassName As String, ByVal WindowName As String) As IntPtr
Return FindWindowExA(hWndParent, hWndChildAfter, ClassName, WindowName)
End Function
'Function to send pressed keys to specified window by handle
Public Shared Function SendKeyToWindow(ByVal hwnd As IntPtr, ByVal data As String) As Boolean
Try
Dim rc As Int32
Dim asciiChar As Byte
If data.Length < 1 Then 'if no data
Return True
End If
'If data.Length > 1 Then
' data = data.ToUpper
'End If
'sendMessageA is used to issue messages to windows simulating keypresses
Select Case data.ToUpper
Case Chr(13)
rc = SendMessageA(hwnd, WM_KEYDOWN, &HD, &H1C0001) 'send Enter Key
rc = SendMessageA(hwnd, WM_CHAR, &HD, &H1C0001) 'send Enter Key
rc = SendMessageA(hwnd, WM_KEYUP, &HBE, &HC0340001) 'send Ener Key
Case Chr(&H9) 'tab key
rc = SendMessageA(hwnd, WM_KEYDOWN, &H9, &HF0001) 'send tab Key
rc = SendMessageA(hwnd, WM_KEYUP, &H9, &HC00F0001) 'send tab Key
Case Else
asciiChar = CByte(Asc(data.Substring(0, 1)))
rc = SendMessageA(hwnd, WM_CHAR, asciiChar, 0) 'send 0 Key
End Select
Return True
Catch ex As Exception
Return False
End Try
Return False
End Function
在您的实例中,您可能只需要使用webbrowser控件的句柄调用sendKeyToWindow函数:
SendKeyToWindow(WebBrowser1.Handle,Chr(&amp; H9)) SendKeyToWindow(WebBrowser1.Handle,Chr(13))
希望有所帮助。