vb.net 教程 20-3 控制Ie浏览器 6

前端之家收集整理的这篇文章主要介绍了vb.net 教程 20-3 控制Ie浏览器 6前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
五、获得Ie窗口 mshtml.IHTMLDocument2
本小节是比较关键的一步,从Internet Explorer_Server获得IHTMLDocument2对象
使用到的api函数
1、RegisterWindowMessage()函数定义一个新的窗口消息
    Private Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" (
        ByVal lpString As String) As Integer
2、SendMessageTimeout()函数将指定的消息发送到一个或多个窗口
    Private Declare Function SendMessageTimeout Lib "user32" Alias "SendMessageTimeoutA" (
        ByVal hWND As Integer,ByVal msg As Integer,ByVal wParam As Integer,ByRef lParam As Integer,ByVal fuFlags As Integer,ByVal uTimeout As Integer,ByRef lpdwResult As Integer) As Integer
3、ObjectFromLresult()函数检索是一个基于先前生成的对象的引用访问的对象请求的接口指针
    Private Declare Function ObjectFromLresult Lib "oleacc" (
        ByVal lResult As Integer,ByRef riid As Guid,ByRef ppvObject As mshtml.IHTMLDocument2) As Integer

4、用到的常量:
    Private Const SMTO_ABORTIFHUNG = &H2
    Private Const SMTO_NOTIMEOUTIFNOTHUNG = &H8

具体代码
    ''' <summary>
    ''' 从Internet Explorer_Server获得IHTMLDocument2对象
    ''' </summary>
    ''' <param name="IEShwnd">Internet Explorer_Server 句柄</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ''' 
    Public Function getDocumentfromIES(ByVal IEShwnd As Integer) As mshtml.IHTMLDocument2
        Dim WM_Html_GETOBJECT As Integer
        WM_Html_GETOBJECT = RegisterWindowMessage("WM_HTML_GETOBJECT")
        Dim tempInt As Integer = 0
        SendMessageTimeout(IEShwnd,WM_Html_GETOBJECT,SMTO_ABORTIFHUNG,1000,tempInt)

        Dim GUID_IHTMLDocument As New Guid("{626FC520-A41E-11CF-A731-00A0C9082637}")

        Dim I_IEdocument As mshtml.IHTMLDocument2
        If ObjectFromLresult(tempInt,GUID_IHTMLDocument,I_IEdocument) = 0 Then
            Return I_IEdocument
        End If
        Return Nothing
    End Function

通过getDocumentfromIES()就可以获得某个Internet Explorer_Server关联的IHTMLDocument2


由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。

学习更多vb.net知识,请参看vb.net 教程 目录

原文链接:https://www.f2er.com/vb/256393.html

猜你在找的VB相关文章