五、获得Ie窗口 mshtml.IHTMLDocument2
本小节是比较关键的一步,从Internet Explorer_Server获得IHTMLDocument2对象
使用到的api函数:
1、RegisterWindowMessage()函数定义一个新的窗口消息
Private Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" ( ByVal lpString As String) As Integer2、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
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 教程 目录