.net 默认的WebBrowser控件有时候并不能拦截所有的URL跳转(比如Flash跳转),而本站所提供的 WebBrowserExt 控件则可以拦截所有的跳转事件,并且还可以屏蔽IE内核点击链接时的跳转声。

具体的用法:先复制下面的代码,然后按F5编译运行一次程序,就可以在控件工具栏中找到 WebBrowserExt 控件,拖放或者手动实例化这个控件即可。

新的拦截事件位于 BeforeNavigateBeforeNewWindow 这两个事件中,禁止跳转链接声位于 DisableNavigationSounds 这个属性中!

详细代码:

Imports System.ComponentModel
Imports System.Runtime.InteropServices

''' <summary>扩展WebBrowser,拥有跳转前获取URL的能力</summary>
Public Class WebBrowserExt
    Inherits WebBrowser

    Shadows cookie As AxHost.ConnectionPointCookie
    Shadows events As WebBrowserExtEvents

    Protected Overrides Sub CreateSink()
        MyBase.CreateSink()
        events = New WebBrowserExtEvents(Me)
        cookie = New AxHost.ConnectionPointCookie(Me.ActiveXInstance, events, GetType(DWebBrowserEvents2))
    End Sub

    Protected Overrides Sub DetachSink()
        If Not cookie Is Nothing Then
            cookie.Disconnect()
            cookie = Nothing
        End If
        MyBase.DetachSink()
    End Sub

    ''' <summary>在跳转前</summary>
    Public Event BeforeNavigate(ByVal sender As Object, ByVal e As NavEventArgsExt)
    ''' <summary>在弹出新窗体前</summary>
    Public Event BeforeNewWindow(ByVal sender As Object, ByVal e As NavEventArgsExt)

    Protected Sub OnBeforeNewWindow(ByVal url As String, ByRef cancel As Boolean)
        Dim args As New NavEventArgsExt(url, Nothing)
        RaiseEvent BeforeNewWindow(Me, args)
        cancel = args.Cancel
    End Sub

    Protected Sub OnBeforeNavigate(ByVal url As String, ByVal frame As String, ByRef cancel As Boolean)
        Dim args As New NavEventArgsExt(url, frame)
        RaiseEvent BeforeNavigate(Me, args)
        cancel = args.Cancel
    End Sub



    ''' <summary>跳转事件封包</summary>
    Public Class NavEventArgsExt
        Inherits CancelEventArgs

        Sub New(ByVal url As String, ByVal frame As String)
            MyBase.New()
            _Url = url
            _Frame = frame
        End Sub

        Private _Url As String
        ReadOnly Property Url As String
            Get
                Return _Url
            End Get
        End Property

        Private _Frame As String
        ReadOnly Property Frame As String
            Get
                Return _Frame
            End Get
        End Property
    End Class


    Private Class WebBrowserExtEvents
        Inherits StandardOleMarshalObject
        Implements DWebBrowserEvents2

        Dim _browser As WebBrowserExt
        Sub New(ByVal browser As WebBrowser)
            _browser = browser
        End Sub

        Public Sub BeforeNavigate2(ByVal pDisp As Object, ByRef url As Object, ByRef flags As Object, ByRef targetFrameName As Object, ByRef postData As Object, ByRef headers As Object, ByRef cancel As Boolean) Implements DWebBrowserEvents2.BeforeNavigate2
            _browser.OnBeforeNavigate(CType(url, String), CType(targetFrameName, String), cancel)
        End Sub

        Public Sub NewWindow3(ByVal pDisp As Object, ByRef cancel As Boolean, ByRef flags As Object, ByRef URLContext As Object, ByRef URL As Object) Implements DWebBrowserEvents2.NewWindow3
            _browser.OnBeforeNewWindow(CType(URL, String), cancel)
        End Sub
    End Class

    <ComImport(), Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D"), _
    InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch), _
    TypeLibType(TypeLibTypeFlags.FHidden)> _
    Public Interface DWebBrowserEvents2

        <DispId(250)> _
        Sub BeforeNavigate2(<[In](), MarshalAs(UnmanagedType.IDispatch)> ByVal pDisp As Object, <[In]()> ByRef url As Object, <[In]()> ByRef flags As Object, <[In]()> ByRef targetFrameName As Object, <[In]()> ByRef postData As Object, <[In]()> ByRef headers As Object, <[In](), Out()> ByRef cancel As Boolean)

        <DispId(273)> _
        Sub NewWindow3(<[In](), MarshalAs(UnmanagedType.IDispatch)> ByVal pDisp As Object, <[In](), Out()> ByRef cancel As Boolean, <[In]()> ByRef flags As Object, <[In]()> ByRef URLContext As Object, <[In]()> ByRef URL As Object)

    End Interface


#Region "禁用网页跳转声"

    Dim _disableNavigationSounds As Boolean = False

    ''' <summary>禁止链接跳转声</summary>
    Property DisableNavigationSounds As Boolean
        Get
            Return _disableNavigationSounds
        End Get
        Set(ByVal value As Boolean)
            If _disableNavigationSounds = value Then Return
            CoInternetSetFeatureEnabled(FEATURE_DISABLE_NAVIGATION_SOUNDS, SET_FEATURE_ON_PROCESS, value)
            _disableNavigationSounds = value
        End Set
    End Property


    Const FEATURE_DISABLE_NAVIGATION_SOUNDS As Integer = 21
    Const SET_FEATURE_ON_PROCESS As Integer = &H2

    <DllImport("urlmon.dll"), PreserveSig()> _
    Private Shared Function CoInternetSetFeatureEnabled(ByVal FeatureEntry As Integer, <MarshalAs(UnmanagedType.U4)> ByVal dwFlags As Integer, ByVal fEnable As Boolean) As <MarshalAs(UnmanagedType.[Error])> Integer
    End Function


#End Region

End Class