日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

如何得到webbrowser的句柄

發(fā)布時間:2025/3/17 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何得到webbrowser的句柄 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

webbrowser不能通過webbrowser.hwnd 來獲得句柄,上網(wǎng)查詢之后在microsoft網(wǎng)站中看到了應(yīng)該用遍歷所以控件并查看其classname是否是shell embedding的方法來獲得,而實際裝載網(wǎng)頁的并不是它本身。webbrowswer的下一層子窗口是Shell DocObject View,再下一層是Internet Explorer_Server,Internet Explorer_Server才是真正裝載網(wǎng)頁的“窗口”。以下是一個例子:

'畫一個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

總結(jié)

以上是生活随笔為你收集整理的如何得到webbrowser的句柄的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。