0

我正在使用一个 Web 浏览器控件来打开一个网站。在网站的第三页,有一个按钮。因此,在加载第 3 页后,我想首先以编程方式按 TAB 键以将控制焦点放在按钮上,然后在加载后立即以编程方式在网站的第三页上按 ENTER 键。我正在使用 vb.net 2008 平台来执行此操作。请任何人告诉我解决方案???

4

1 回答 1

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(&H9)) SendKeyToWindow(WebBrowser1.Handle, Chr(13))

希望有帮助。

于 2012-06-22T05:57:24.943 回答