[转]VB.NET2005控制WebBrowser弹出页面的行为

VB.NET2005控制WebBrowser弹出页面的行为

王明洪

实用效果挺好,资料来自日本的一个BBS。

Option Strict On
Imports System.Runtime.InteropServices
Imports System.Security.Permissions
Class ExWebBrowser
    Inherits WebBrowser
    Private cookie As AxHost.ConnectionPointCookie
    Private helper As WebBrowser2EventHelper

    <System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Hidden)> _
    <System.Runtime.InteropServices.DispIdAttribute(200)> _
    Public ReadOnly Property Application() As Object
        Get
            If IsNothing(Me.ActiveXInstance) Then
                Throw New AxHost.InvalidActiveXStateException(“Application”, AxHost.ActiveXInvokeKind.PropertyGet)
            End If
            Return DirectCast(Me.ActiveXInstance, IWebBrowser2).Application
        End Get
    End Property
    <System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Hidden)> _
    <System.Runtime.InteropServices.DispIdAttribute(552)> _
    Public Property RegisterAsBrowser() As Boolean
        Get
            If IsNothing(Me.ActiveXInstance) Then
                Throw New AxHost.InvalidActiveXStateException(“RegisterAsBrowser”, AxHost.ActiveXInvokeKind.PropertyGet)
            End If
            Return DirectCast(Me.ActiveXInstance, IWebBrowser2).RegisterAsBrowser
        End Get
        Set(ByVal value As Boolean)
            If IsNothing(Me.ActiveXInstance) Then
                Throw New AxHost.InvalidActiveXStateException(“RegisterAsBrowser”, AxHost.ActiveXInvokeKind.PropertySet)
            End If
            DirectCast(Me.ActiveXInstance, IWebBrowser2).RegisterAsBrowser = value
        End Set
    End Property
    <PermissionSetAttribute(SecurityAction.LinkDemand, Name:=”FullTrust”)> _
    Protected Overrides Sub CreateSink()
        MyBase.CreateSink()
        helper = New WebBrowser2EventHelper(Me)
        cookie = New AxHost.ConnectionPointCookie(Me.ActiveXInstance, helper, GetType(DWebBrowserEvents2))
    End Sub
    <PermissionSetAttribute(SecurityAction.LinkDemand, Name:=”FullTrust”)> _
    Protected Overrides Sub DetachSink()
        If cookie IsNot Nothing Then
            cookie.Disconnect()
            cookie = Nothing
        End If
        MyBase.DetachSink()
    End Sub
    Public Event NewWindow2 As WebBrowserNewWindow2EventHandler
    Protected Overridable Sub OnNewWindow2(ByVal e As WebBrowserNewWindow2EventArgs)
        RaiseEvent NewWindow2(Me, e)
    End Sub
    Private Class WebBrowser2EventHelper
        Inherits StandardOleMarshalObject
        Implements DWebBrowserEvents2
        Private parent As ExWebBrowser
        Public Sub New(ByVal parent As ExWebBrowser)
            Me.parent = parent
        End Sub
        Public Sub NewWindow2(ByRef ppDisp As Object, ByRef cancel As Boolean) Implements DWebBrowserEvents2.NewWindow2
            Dim e As New WebBrowserNewWindow2EventArgs(ppDisp)
            Me.parent.OnNewWindow2(e)
            ppDisp = e.ppDisp
            cancel = e.Cancel
        End Sub
    End Class
End Class
Public Delegate Sub WebBrowserNewWindow2EventHandler(ByVal sender As Object, ByVal e As WebBrowserNewWindow2EventArgs)
Public Class WebBrowserNewWindow2EventArgs
    Inherits System.ComponentModel.CancelEventArgs
    Private ppDispValue As Object
    Public Sub New(ByVal ppDisp As Object)
        Me.ppDispValue = ppDisp
    End Sub
    Public Property ppDisp() As Object
        Get
            Return ppDispValue
        End Get
        Set(ByVal value As Object)
            ppDispValue = value
        End Set
    End Property
End Class
<ComImport(), Guid(“34A715A0-6587-11D0-924A-0020AFC7AC4D”), _
InterfaceType(ComInterfaceType.InterfaceIsIDispatch)> _
Public Interface DWebBrowserEvents2
    <DispId(DISPID.NEWWINDOW2)> Sub NewWindow2( _
        <[In](), Out(), MarshalAs(UnmanagedType.IDispatch)> ByRef ppDisp As Object, _
        <[In](), Out()> ByRef cancel As Boolean)
End Interface
Public Enum DISPID
    NEWWINDOW2 = 251
End Enum
<ComImport(), Guid(“D30C1661-CDAF-11D0-8A3E-00C04FC9E26E”), _
InterfaceType(ComInterfaceType.InterfaceIsIDispatch)> _
Public Interface IWebBrowser2
    ReadOnly Property Application() As <MarshalAs(UnmanagedType.IDispatch)> Object
    Property RegisterAsBrowser() As <MarshalAs(UnmanagedType.VariantBool)> Boolean
End Interface

引文来源  VB.NET2005控制WebBrowser弹出页面的行为_ICSOFT_新浪博客