日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

如何得到webbrowser的句柄

發布時間:2025/3/17 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何得到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的句柄的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。