How to get the DOM of a WebBrowser control from a window handle (VB6)

转载 2004年09月07日 18:40:00

How to get the DOM of a WebBrowser control from a window handle (VB6)
http://www.mvps.org/emorcillo/en/code/vb6/iedom.shtml

The following code is the VB version of the C code in the KB article 249232 - HOWTO: Get IHTMLDocument2 from a HWND. It requires oleacc.dll to be installed on the system (oleacc.dll is part of the Active Accessibility).

'
' Requires: reference to "Microsoft HTML Object Library"
'

Private Type UUID
   Data1 As Long
   Data2 As Integer
   Data3 As Integer
   Data4(0 To 7) As Byte
End Type

Private Declare Function GetClassName Lib "user32" _
   Alias "GetClassNameA" ( _
   ByVal hWnd As Long, _
   ByVal lpClassName As String, _
   ByVal nMaxCount As Long) As Long

Private Declare Function EnumChildWindows Lib "user32" ( _
   ByVal hWndParent As Long, _
   ByVal lpEnumFunc As Long, _
   lParam As Long) As Long

Private Declare Function RegisterWindowMessage Lib "user32" _
   Alias "RegisterWindowMessageA" ( _
   ByVal lpString As String) As Long

Private Declare Function SendMessageTimeout Lib "user32" _
   Alias "SendMessageTimeoutA" ( _
   ByVal hWnd As Long, _
   ByVal msg As Long, _
   ByVal wParam As Long, _
   lParam As Any, _
   ByVal fuFlags As Long, _
   ByVal uTimeout As Long, _
   lpdwResult As Long) As Long
      
Private Const SMTO_ABORTIFHUNG = &H2

Private Declare Function ObjectFromLresult Lib "oleacc" ( _
   ByVal lResult As Long, _
   riid As UUID, _
   ByVal wParam As Long, _
   ppvObject As Any) As Long

Private Declare Function FindWindow Lib "user32" _
   Alias "FindWindowA" ( _
   ByVal lpClassName As String, _
   ByVal lpWindowName As String) As Long

'
' IEDOMFromhWnd
'
' Returns the IHTMLDocument interface from a WebBrowser window
'
' hWnd - Window handle of the control
'
Function IEDOMFromhWnd(ByVal hWnd As Long) As IHTMLDocument
Dim IID_IHTMLDocument As UUID
Dim hWndChild As Long
Dim lRes As Long
Dim lMsg As Long
Dim hr As Long

   If hWnd <> 0 Then
      
      If Not IsIEServerWindow(hWnd) Then
      
         ' Find a child IE server window
         EnumChildWindows hWnd, AddressOf EnumChildProc, hWnd
         
      End If
      
      If hWnd <> 0 Then
            
         ' Register the message
         lMsg = RegisterWindowMessage("WM_HTML_GETOBJECT")
            
         ' Get the object pointer
         Call SendMessageTimeout(hWnd, lMsg, 0, 0, _
                 SMTO_ABORTIFHUNG, 1000, lRes)

         If lRes Then
               
            ' Initialize the interface ID
            With IID_IHTMLDocument
               .Data1 = &H626FC520
               .Data2 = &HA41E
               .Data3 = &H11CF
               .Data4(0) = &HA7
               .Data4(1) = &H31
               .Data4(2) = &H0
               .Data4(3) = &HA0
               .Data4(4) = &HC9
               .Data4(5) = &H8
               .Data4(6) = &H26
               .Data4(7) = &H37
            End With
               
            ' Get the object from lRes
            hr = ObjectFromLresult(lRes, IID_IHTMLDocument,_
                     0, IEDOMFromhWnd)
               
         End If

      End If
      
   End If

End Function

Private Function IsIEServerWindow(ByVal hWnd As Long) As Boolean
Dim lRes As Long
Dim sClassName As String

   ' Initialize the buffer
   sClassName = String$(100, 0)
   
   ' Get the window class name
   lRes = GetClassName(hWnd, sClassName, Len(sClassName))
   sClassName = Left$(sClassName, lRes)
   
   IsIEServerWindow = StrComp(sClassName, _
                      "Internet Explorer_Server", _
                      vbTextCompare) = 0
   
End Function

'
' Copy this function to a .bas module
'
Function EnumChildProc(ByVal hWnd As Long, lParam As Long) As Long
   
   If IsIEServerWindow(hWnd) Then
      lParam = hWnd
   Else
      EnumChildProc = 1
   End If
   
End Function

CSA课程:A段架构师的职责

-
  • 1970年01月01日 08:00

Get a window’s handle from the current cursor position.

  • 2006年02月23日 09:05
  • 5KB
  • 下载

多线程存取WinForm控件

采集器使用的是System.Windows.Forms.WebBrowser, 在constructor里面添加了DocumentChanged的event handler, 来模拟浏览器访问webm...
  • gbstack08
  • gbstack08
  • 2012-03-10 17:02:03
  • 883

Mina报 Don't know how to handle message of type 'java.lang.String'异常

[WARN]-[org.apache.mina.core.service.IoHandlerAdapter.exceptionCaught(IoHandlerAdapter.java:55)]EXCE...
  • tfhui928
  • tfhui928
  • 2014-11-13 10:37:25
  • 4969

how to get the return value from a thread in python?

how to get the return value from a thread in python? up vote19down votefa...
  • pi9nc
  • pi9nc
  • 2013-12-05 13:40:12
  • 1757

How to access a user control from another user control in ASP.NET

  • 2009年08月22日 14:20
  • 8KB
  • 下载

Link a ComboBox to a WebBrowser Control

  • 2006年02月23日 09:05
  • 2KB
  • 下载

How to get a job at Google, interview questions, hiring process

The Google hiring process is designed to hire the most talented, creative, and articulate people in ...
  • zhuxiaoyang2000
  • zhuxiaoyang2000
  • 2011-03-29 20:27:00
  • 1665

Disable Input to a WebBrowser Control

  • 2006年02月23日 09:05
  • 1KB
  • 下载

WebBrowser-HTML DOM

全面介绍WebBrowser的中文资料比较少,同时,使用WebBrowser操作网页最好具备一定的HTML DOM背景知识http://www.w3school.com.cn/ 是一个很不错的网站,上...
  • X_Craft
  • X_Craft
  • 2009-07-14 10:45:00
  • 1101
收藏助手
不良信息举报
您举报文章:How to get the DOM of a WebBrowser control from a window handle (VB6)
举报原因:
原因补充:

(最多只允许输入30个字)