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的句柄相关推荐

  1. vb html ie弹出窗口,VB6 统制IE弹出模式窗口

    VB6 控制IE弹出模式窗口 最近一个小项目,开发一个插件,需要对一个第三方系统的网页内容进行操作.操作的内容还比较复杂.自然选择用BHO做. 做到一半卡住了,原因在于这个网站有一个showmodal ...

  2. wpf修改theme中的样式_WPF Mahapps.Metro 设置主题样式

    /// /// 设置App样式 /// /// 窗口标题栏样式 /// 背景样式 private void ChangeTheme(string accentName, string themeNam ...

  3. c#控制IE浏览器自动点击等事件WebBrowser,mshtml.IHTMLDocument2

    c#控制IE浏览器自动点击等事件WebBrowser,mshtml.IHTMLDocument2 原文:c#控制IE浏览器自动点击等事件WebBrowser,mshtml.IHTMLDocument2 ...

  4. wpf Webbrowser 乱码问题及弹窗被遮挡

    wpf的webbrowser在使用NavigateToString(string text);方法时如果字符串含有中文字符,并在html的头文件中没有声明Document的编码方式为UTF-8的话,由 ...

  5. WebBrowser keystroke

    问题描述:将对话框中的回车消息传递到WebBrowser控件中去. 一开始的解决方案是:通过iwebbrowser2的成员函数get_HWND获取控件的句柄,然后通过SendMessage发送按键消息 ...

  6. C# (转载)webbrowser专题(参考资料:https://www.cnblogs.com/blogpro/p/11458390.html)

    C# .Net 2.0实例学习:WebBrowser页面与WinForm交互技巧 2 Study Case 4:高亮显示 上一个例子中我们学会了查找文本--究跟到底,对Web页面还是只读不写.那么,如 ...

  7. C# webbrowser 获取网页元素 示例代码

    在应用webbrowser对网页进行自动化操作时,不能有效地获取网页元素,往往是后续编程的拦路虎,原因是webbrowser提供的GetElementById().GetElementFromPoin ...

  8. C# webbrowser专题

    C# .Net 2.0实例学习:WebBrowser页面与WinForm交互技巧 2 Study Case 4:高亮显示 上一个例子中我们学会了查找文本--究跟到底,对Web页面还是只读不写.那么,如 ...

  9. WebBrowser 事件

    私有事件 描述 BeforeNavigate2 在导航之前发生. (该事件并不在不刷新页面的时候发生) CommandStateChange 当命令状态改变时发生.该事件告诉你何时使能或者禁止 Bac ...

最新文章

  1. 强大的NVAE:以后再也不能说VAE生成的图像模糊了
  2. .NET for Apache Spark 1.0 版本发布
  3. windows 远程执行 cmd 命令的 9 种方法
  4. 在 CSS 中,width 和 height 指的是内容区域的宽度和高度
  5. C#LeetCode刷题之#13-罗马数字转整数(Roman to Integer)
  6. linux 命令查看块设备,系统运维|用 Linux blkid 命令查找块设备详情
  7. H3C交换机4核心节点IRF2虚拟化下检测机制
  8. Useful SQL script
  9. 结构体typedef struct和struct
  10. 如何使用NAS才能确保数据100%安全(数据存储解决方案)
  11. chrome 谷歌浏览器无法上网,IE浏览器可以上网
  12. win10卸载预装软件
  13. 2018计算机专硕学硕,2018年考研要弄清学硕与专硕的区别
  14. 函数模板案例—选择排序
  15. logo怎么设计才好看?好看的logo设计方法大公开。
  16. 关于F# 6的那些新功能?你了解吗?
  17. 在windows平台搭建基于nginx的直播服务器
  18. 学计算机的要起一个群名,微信群名字大全 一起走过的日子
  19. SAP HCM模块系统标准报表
  20. python鱼眼图像识别_一种基于鱼眼摄像头的人脸识别锁以及识别方法与流程

热门文章

  1. .Net Core----关于MVC中TempData持久化问题
  2. 从零搭建 vue-cli 脚手架
  3. canvas的getImageData和putImageDataAPI
  4. 伪分布式Hadoop2.x集群的搭建1
  5. VS2015 中使用 MVC4
  6. Hadoop怎么了,大数据路在何方?
  7. 双非本科上岸北大,复试成绩专业第一!
  8. 终于把公司的底裤扒了!
  9. 境内外赌博网站被捣毁,程序员被抓!!
  10. 我是怎样刚拿饿了么P7 offer,然后途虎一轮游的