如何得到webbrowser的句柄
webbrowser不能通過webbrowser.hwnd 來獲得句柄,上網查詢之后在microsoft網站中看到了應該用遍歷所以控件并查看其classname是否是shell embedding的方法來獲得,而實際裝載網頁的并不是它本身。webbrowswer的下一層子窗口是Shell DocObject View,再下一層是Internet Explorer_Server,Internet Explorer_Server才是真正裝載網頁的“窗口”。以下是一個例子:
'畫一個webbrowser1和一個command1
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Const GW_CHILD = 5
Private Const GW_HWNDNEXT = 2
Dim hwndWebB As Long
Private Sub Command1_Click()
??? fGetWebBHwnd Form1.hwnd
End Sub
Private Sub Form_Load()
??? WebBrowser1.Navigate2 "http://hi.baidu.com/spongeyu"
End Sub
Private Sub WebBrowser1_DownloadComplete()
??? If hwndWebB = 0 Then
??????? hwndWebB = fGetWebBHwnd(Me.hwnd)
??????? Debug.Print "hwndWebB=" & hwndWebB
??? End If
End Sub
'獲得webbrowser的最上層子控件Internet Explorer_Server的句柄
Public Function fGetWebBHwnd(hwndParent As Long) As Long
??? Dim hwndChild1 As Long
??? Dim hwndChild2 As Long
??? Dim hwndChild3 As Long
??? Dim classChild1
??? Dim classChild2
??? Dim classChild3
??? Dim sClassname1 As String * 256
??? Dim sClassname2 As String * 256
??? Dim sClassname3 As String * 256
???
??? '第一子層
??? hwndChild1 = GetWindow(hwndParent, GW_CHILD)
??? Debug.Print "hwnd=" & hwndChild1
??? classChild1 = GetClassName(hwndChild1, sClassname1, 256)
??? Debug.Print "classsname=" & sClassname1
??? If Left(sClassname1, Len("Shell Embedding")) <> "Shell Embedding" Then
??????? Do
??????????? hwndChild1 = GetWindow(hwndChild1, GW_HWNDNEXT)
??????????? Debug.Print "hwnd=" & hwndChild1
??????????? classChild1 = GetClassName(hwndChild1, sClassname1, 256)
??????????? Debug.Print "classsname=" & sClassname1
??????????? If Left(sClassname1, Len("Shell Embedding")) = "Shell Embedding" Then
???????????????? Exit Do
??????????? End If
??????????? DoEvents
??????? Loop While hwndChild1 <> 0
??? End If
??? '第二子層
??? If hwndChild1 <> 0 Then
??????? hwndChild2 = GetWindow(hwndChild1, GW_CHILD)
??????? Debug.Print "hwnd=" & hwndChild2
??????? classChild2 = GetClassName(hwndChild2, sClassname2, 256)
??????? Debug.Print "classsname=" & sClassname2
??????? If Left(sClassname2, Len("Shell DocObject View")) <> "Shell DocObject View" Then
??????????? Do
??????????????? hwndChild2 = GetWindow(hwndChild2, GW_HWNDNEXT)
??????????????? Debug.Print "hwnd=" & hwndChild2
??????????????? classChild2 = GetClassName(hwndChild2, sClassname2, 256)
??????????????? Debug.Print "classsname=" & sClassname2
??????????????? If Left(sClassname2, Len("Shell DocObject View")) = "Shell DocObject View" Then
???????????????????? Exit Do
??????????????? End If
??????????????? DoEvents
??????????? Loop While hwndChild2 <> 0
??????? End If
??? End If
??? '第三子層
??? If hwndChild2 <> 0 Then
??????? hwndChild3 = GetWindow(hwndChild2, GW_CHILD)
??????? Debug.Print "hwnd=" & hwndChild3
??????? classChild3 = GetClassName(hwndChild3, sClassname3, 256)
??????? Debug.Print "classsname=" & sClassname3
??????? If Left(sClassname3, Len("Internet Explorer_Server")) <> "Internet Explorer_Server" Then
??????????? Do
??????????????? hwndChild3 = GetWindow(hwndChild3, GW_HWNDNEXT)
??????????????? Debug.Print "hwnd=" & hwndChild3
??????????????? classChild3 = GetClassName(hwndChild3, sClassname3, 256)
??????????????? Debug.Print "classsname=" & sClassname3
??????????????? If Left(sClassname3, Len("Internet Explorer_Server")) = "Internet Explorer_Server" Then
???????????????????? Exit Do
??????????????? End If
??????????????? DoEvents
??????????? Loop While hwndChild3 <> 0
??????? End If
??? End If
??? If hwndChild3 <> 0 Then
??????? fGetWebBHwnd = hwndChild3
??? Else
??????? fGetWebBHwnd = 0
??? End If
End Function
另外還有一種簡便的方法:
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 Function fGetWebBHwnd1(hwndParent As Long) As Long
??? Dim lngHwnd As Long
??? lngHwnd = FindWindowEx(hwndParent, 0, "Shell Embedding", vbNullString)
??? lngHwnd = FindWindowEx(lngHwnd, 0, "Shell DocObject View", vbNullString)
??? lngHwnd = FindWindowEx(lngHwnd, 0, "Internet Explorer_Server", vbNullString)
??? If lngHwnd <> 0 Then
??????? fGetWebBHwnd1 = lngHwnd
??? Else
??????? fGetWebBHwnd1 = 0
??? End If
End Function
總結
以上是生活随笔為你收集整理的如何得到webbrowser的句柄的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何订阅MVP on dot NET(或
- 下一篇: Script Debugger + VS