我有个程序想实现动态修改任意 IE窗口或Webbrowser 控件的网页内容。
翻了网上的资料发现VB只能够获得 Internet Explorer_Server 的句柄,而
Internet Explorer_Server的内容我就不知道该怎么获取了,更不要说去修改了。
想过调用SHDocVw.dll 来实现,但是只能局限在IE这类窗口中,其它的像使用Webbrowser 控件的这类窗口却没法获取。希望那位知道这方面知识的高手,能给我点帮助。谢谢啦

解决方案 »

  1.   

    http://community.csdn.net/Expert/topic/2735/2735009.xml?temp=.9995844
    http://community.csdn.net/Expert/topic/3075/3075369.xml?temp=.8185236
      

  2.   

    谢谢 TechnoFantasy(冰儿马甲www.applevb.com) 的回复,但是还是没有解决我的问题。
    http://community.csdn.net/Expert/topic/2735/2735009.xml?temp=.9995844
    中的 MSHTML 对象,的确对我有点启发,但是最好能够有现成的代码来参考。http://community.csdn.net/Expert/topic/3075/3075369.xml?temp=.8185236
    ztesoft(中兴软创 创造未来) 的回复
    枚举IE的子窗体,得到Internet Explorer_Server的句柄,然后调用ObjectFromLresult函数。前面的枚举我已经做到了,就是调用ObjectFromLresult函数我无法实现,ObjectFromLresult函数,目前VB好像是没法用的吧。如果能用最好给个实例。还有就是
    http://community.csdn.net/Expert/topic/3075/3075369.xml?temp=.8185236
    遇到的问题我以前正好遇到过,不过解决了。我就顺便回了一下,可惜没有分了 :P
      

  3.   

    呵呵,我也是突发奇想才去做的,但想不到这么难实现。现在获得句柄后发觉根本就没什么用,可能一开始思路就已经错了。但是应用SHDocVw.dll的方法又太局限,哎,实在是进退两难啊。
    这个程序在 VC++ 上实现起来好像挺简单的,而且有现成的源码,可以获得指定窗口中Internet Explorer_Server 类的Html代码,但是不能设置Internet Explorer_Server 类的html源码。只是小弟我水平有限,只能看着源码干瞪眼。难道用VB实现真的这么难吗?
      

  4.   

    获得了IHTMLDocument2接口之后可以查询其IPersistStream接口加载文档,也可以修改Body元素的InnerHTML。
    既然VC的代码已经有了,我也不用贴了
      

  5.   

    哈哈,总算问题解决了。原来早就有人研究过这方面的问题了,以前一直在找中文的资料,没有注意到语言的资料。不做私藏共享一下让大家一起研究一下
    原版:   http://www.mvps.org/emorcillo/vb6/inet/iedom.shtml代码:
    '
    ' 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 TypePrivate Declare Function GetClassName Lib "user32" _
       Alias "GetClassNameA" ( _
       ByVal hWnd As Long, _
       ByVal lpClassName As String, _
       ByVal nMaxCount As Long) As LongPrivate Declare Function EnumChildWindows Lib "user32" ( _
       ByVal hWndParent As Long, _
       ByVal lpEnumFunc As Long, _
       lParam As Long) As LongPrivate Declare Function RegisterWindowMessage Lib "user32" _
       Alias "RegisterWindowMessageA" ( _
       ByVal lpString As String) As LongPrivate 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 = &H2Private Declare Function ObjectFromLresult Lib "oleacc" ( _
       ByVal lResult As Long, _
       riid As UUID, _
       ByVal wParam As Long, _
       ppvObject As Any) As LongPrivate 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 IfEnd FunctionPrivate 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这段代码需要放在模块中才能正常使用,使用时需要引用Microsoft HTML Object Library
    然后创建一个IHTMLDocument 对象,set objname = IEDOMFromhWnd(hWnd) 就能使用了。
      

  6.   

    ps: 国外的高手到底多啊,看了他的代码让我自叹不如,遍历Internet Explorer_Server 对象的方法比我简单多了。看来我还有很多要学的啊