16,554
社区成员
发帖
与我相关
我的任务
分享
Imports System.ComponentModel
Imports System.Runtime.InteropServices
Imports System.Security.Permissions
Imports System
Imports System.Security
Imports System.Collections.Generic
Imports System.Text
Imports System.Runtime.InteropServices.ComTypes
Imports System.Windows.Forms
''' <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
Enum WindowsMessages
WM_WINDOWPOSCHANGED = &H47
WM_WINDOWPOSCHANGING = &H46
WM_WININICHANGE = &H1A
End Enum
<PermissionSet(SecurityAction.LinkDemand, Name:="FullTrust")>
Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = CInt(WindowsMessages.WM_PARENTNOTIFY) Then
Dim wp As Integer = m.WParam.ToInt32()
Dim X As Integer = wp And &HFFFF
If X = CInt(WindowsMessages.WM_DESTROY) Then Me.OnQuit()
End If
MyBase.WndProc(m)
End Sub
Public Event Quit As EventHandler
Protected Sub OnQuit()
RaiseEvent Quit(Me, EventArgs.Empty)
End Sub
#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