vb.net webbrowser 操作 iframe方法

webbrowser   操作 iframe时,如不在一个框架内,则会提示安全错误,以下为解决类及方法,
 
 
 

'在项目中引用 mshtml和SHDocVw
'源码如下:
Imports System
Imports System.Runtime.InteropServices
Imports mshtml
Imports SHDocVw
Namespace CodecentrixSample
    Public Class CrossFrameIE
        Const E_ACCESSDENIED As Integer = &H80070005
        Private Shared IID_IWebBrowserApp As Guid = New Guid("0002DF05-0000-0000-C000-000000000046")
        Private Shared IID_IWebBrowser2 As Guid = New Guid("D30C1661-CDAF-11D0-8A3E-00C04FC9E26E")
        ' Returns null in case of failure.
        Public Shared Function GetDocumentFromWindow(ByVal htmlWindow As IHTMLWindow2) As IHTMLDocument2
            If htmlWindow Is Nothing Then
                Return Nothing
            End If
            ' First try the usual way to get the document.
            Try
                Dim doc As IHTMLDocument2 = htmlWindow.document
                Return doc
            Catch comEx As COMException
                ' I think COMException won't be ever fired but just to be sure ...
                If (comEx.ErrorCode <> E_ACCESSDENIED) Then
                    Return Nothing
                End If
            Catch ex As System.UnauthorizedAcces***ception

            Catch ex As Exception
                ' Any other error.
                Return Nothing
            End Try
            ' At this point the error was E_ACCESSDENIED because the frame contains a document from another domain.
            ' IE tries to prevent a cross frame scripting security issue.
            Try
                ' Convert IHTMLWindow2 to IWebBrowser2 using IServiceProvider.
                Dim sp As IServiceProvider = htmlWindow.window
                Dim sp1 As IServiceProvider = CType(htmlWindow, IServiceProvider)
                ' Use IServiceProvider.QueryService to get IWebBrowser2 object.
                Dim brws As Object = Nothing
                sp.QueryService(IID_IWebBrowserApp, IID_IWebBrowser2, brws)
                ' Get the document from IWebBrowser2.
                Dim browser As SHDocVw.WebBrowser = brws     '.IWebBrowser2 = brws
                Return browser.Document
            Catch
                Return Nothing
            End Try
        End Function
    End Class
    ' This is the COM IServiceProvider interface, not System.IServiceProvider .Net interface!
    _
    Public Interface IServiceProvider
        _
     Function QueryService(ByRef guidService As Guid, ByRef riid As Guid, ByRef ppvObject As Object) As Integer
    End Interface
End Namespace
 
使用方法:
Dim win As mshtml.IHTMLWindow2 = tmpframe.window
Dim doc As mshtml.IHTMLDocument2 = CodecentrixSample.CrossFrameIE.GetDocumentFromWindow(win)
MessageBox.Show(doc.body.innerHTML)
 
 
 
 
 
备注信息:

C#包含mshtml, SHDocVw, AxSHDocVw, IHTMLDocument2 的引用

包含mshtml, SHDocVw, AxSHDocVw的引用 

在vs2005中,这样引进mshtml

在代码开头加上using mshtml; 

(具体方法    引用--》COM--》Microsoft HTML Object Library
using mshtml;

)

  

SHDocVw的引用,网上有篇文章这么做的:

SHDocVw一定要在下面这个路径找:(C:\Program Files\Microsoft Visual Studio 8\Application\PreEmptive Solutions\Dotfuscator Community Edition)

还有篇文章是这么来添加引用的:

1 在 Visual C# 2005 或 Visual C# .NET 中新建一个 Windows 应用程序项目。默认情况下创建 Form1。

注意:在 Visual C# 2005 中,如果您找不到 SHDocVw.dll 文件或 AxSHDocVw.dll 文件,请在 Visual Studio 命令提示符下运行下面的命令:

aximp %WINDIR%\system32\shdocvw.dll

然后,为 Microsoft WebBrowser 控件创建公共语言运行库代理 (SHDocVw.dll) 和 Windows 窗体代理 (AxSHDocVw.dll)。若要在 Visual C# 2005 中添加 DLL 文件,请按下列步骤操作:

a 在“项目”菜单上,单击“添加引用”。

b 在“添加引用”对话框中,单击“浏览”。

c 找到并选择 AxSHDocVw.dll 和 SHDocVw.dll 文件。

d 若要为这两个文件添加项目引用,请单击“确定”。

2 在“工具”菜单上,单击“自定义工具箱”以打开“自定义工具箱”对话框。在“COM 组件”选项卡上,添加一个对“Microsoft WebBrowser”的引用。单击“确定”,将 WebBrowser 控件添加到 Windows 窗体工具箱。WebBrowser 控件会显示出来,并且在工具箱中带有“Explorer”(资源管理器)字样。

注意:在 Visual Studio 2005 中,不必执行步骤 2。

3 使用该工具箱向 Form1 添加一个 WebBrowser 控件、一个 OpenFileDialog 控件和一个 CommandButton 控件。这就会向 Form1 类添加“AxWebBrowser1”、“OpenFileDialog1”和“Button1”成员变量。在 Visual C# 2005 中,会添加“webBrowser1”、“openFileDialog1”和“button1”成员变量

我的是vs2005,所以没有第2步说的东西。但是做完第一步之后,我的工具箱里没有像第3步那样出现AxWebBrowser1控件,还是原来的webBrowser控件

你可能感兴趣的:(vb/vb.net/vbs,webbrowser,iframe,vb.net,microsoft,interface,工具)