16,554
社区成员
发帖
与我相关
我的任务
分享
Imports System
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text
Namespace ExtendWebBrowserSample
Partial Public Class ExtendWebBrowser
Inherits System.Windows.Forms.WebBrowser
Private cookie As System.Windows.Forms.AxHost.ConnectionPointCookie
''vb.net不区分大小写,与系统提供的相同需改名
Private events1 As WebBrowserExtendedEvents
Public Sub New()
''不需要
'InitializeComponent()
End Sub
Public Sub New(ByVal container As IContainer)
container.Add(Me)
''不需要
'InitializeComponent()
End Sub
'This method will be called to give you a chance to create your own event sink
Protected Overloads Overrides Sub CreateSink()
'MAKE SURE TO CALL THE BASE or the normal events won't fire
MyBase.CreateSink()
events1 = New WebBrowserExtendedEvents(Me)
cookie = New System.Windows.Forms.AxHost.ConnectionPointCookie(Me.ActiveXInstance, events1, GetType(DWebBrowserEvents2))
End Sub
Protected Overloads Overrides Sub DetachSink()
If cookie IsNot Nothing Then
cookie.Disconnect()
cookie = Nothing
End If
MyBase.DetachSink()
End Sub
'This new event will fire when the page is navigating
Public Event BeforeNavigate As EventHandler
Public Event BeforeNewWindow As EventHandler
Protected Sub OnBeforeNewWindow(ByVal url As String, ByRef cancel As Boolean)
Dim args As New WebBrowserExtendedNavigatingEventArgs(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 WebBrowserExtendedNavigatingEventArgs(url, frame)
''直接触发事件就可以了,不能赋值的
RaiseEvent BeforeNavigate(Me, args)
'Pass the cancellation chosen back out to the events
cancel = args.Cancel
End Sub
'This class will capture events from the WebBrowser
Private Class WebBrowserExtendedEvents
Inherits System.Runtime.InteropServices.StandardOleMarshalObject
Implements DWebBrowserEvents2
''因为DWebBrowserEvents2是接口,所以用Implements
Private _Browser As ExtendWebBrowser
Public Sub New(ByVal browser As ExtendWebBrowser)
_Browser = browser
End Sub
'Implement whichever events you wish
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(DirectCast(URL, String), DirectCast(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(DirectCast(URL, String), cancel)
End Sub
End Class
<System.Runtime.InteropServices.ComImport(), System.Runtime.InteropServices.Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D"), System.Runtime.InteropServices.InterfaceTypeAttribute(System.Runtime.InteropServices.ComInterfaceType.InterfaceIsIDispatch), System.Runtime.InteropServices.TypeLibType(System.Runtime.InteropServices.TypeLibTypeFlags.FHidden)> _
Public Interface DWebBrowserEvents2
<System.Runtime.InteropServices.DispId(250)> _
Sub BeforeNavigate2(<System.Runtime.InteropServices.In(), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.IDispatch)> ByVal pDisp As Object, <System.Runtime.InteropServices.In()> ByRef URL As Object, <System.Runtime.InteropServices.In()> ByRef flags As Object, <System.Runtime.InteropServices.In()> ByRef targetFrameName As Object, <System.Runtime.InteropServices.In()> ByRef postData As Object, <System.Runtime.InteropServices.In()> ByRef headers As Object, _
<System.Runtime.InteropServices.In(), System.Runtime.InteropServices.Out()> ByRef cancel As Boolean)
<System.Runtime.InteropServices.DispId(273)> _
Sub NewWindow3(<System.Runtime.InteropServices.In(), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.IDispatch)> ByVal pDisp As Object, <System.Runtime.InteropServices.In(), System.Runtime.InteropServices.Out()> ByRef cancel As Boolean, <System.Runtime.InteropServices.In()> ByRef flags As Object, <System.Runtime.InteropServices.In()> ByRef URLContext As Object, <System.Runtime.InteropServices.In()> ByRef URL As Object)
End Interface
End Class
Public Class WebBrowserExtendedNavigatingEventArgs
Inherits CancelEventArgs
Private _Url As String
Public ReadOnly Property Url() As String
Get
Return _Url
End Get
End Property
Private _Frame As String
Public ReadOnly Property Frame() As String
Get
Return _Frame
End Get
End Property
Public Sub New(ByVal url As String, ByVal frame As String)
MyBase.New()
_Url = url
_Frame = frame
End Sub
End Class
End Namespace
Imports System
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text
Namespace ExtendWebBrowserSample
Public Partial Class ExtendWebBrowser
Inherits System.Windows.Forms.WebBrowser
Private cookie As System.Windows.Forms.AxHost.ConnectionPointCookie
Private events As WebBrowserExtendedEvents
Public Sub New()
InitializeComponent()
End Sub
Public Sub New(ByVal container As IContainer)
container.Add(Me)
InitializeComponent()
End Sub
'This method will be called to give you a chance to create your own event sink
Protected Overloads Overrides Sub CreateSink()
'MAKE SURE TO CALL THE BASE or the normal events won't fire
MyBase.CreateSink()
events = New WebBrowserExtendedEvents(Me)
cookie = New System.Windows.Forms.AxHost.ConnectionPointCookie(Me.ActiveXInstance, events, GetType(DWebBrowserEvents2))
End Sub
Protected Overloads Overrides Sub DetachSink()
If cookie IsNot Nothing Then
cookie.Disconnect()
cookie = Nothing
End If
MyBase.DetachSink()
End Sub
'This new event will fire when the page is navigating
Public Event BeforeNavigate As EventHandler
Public Event BeforeNewWindow As EventHandler
Protected Sub OnBeforeNewWindow(ByVal url As String, ByRef cancel As Boolean)
Dim h As EventHandler = BeforeNewWindow
Dim args As New WebBrowserExtendedNavigatingEventArgs(url, Nothing)
RaiseEvent h(Me, args)
cancel = args.Cancel
End Sub
Protected Sub OnBeforeNavigate(ByVal url As String, ByVal frame As String, ByRef cancel As Boolean)
Dim h As EventHandler = BeforeNavigate
Dim args As New WebBrowserExtendedNavigatingEventArgs(url, frame)
RaiseEvent h(Me, args)
'Pass the cancellation chosen back out to the events
cancel = args.Cancel
End Sub
'This class will capture events from the WebBrowser
Private Class WebBrowserExtendedEvents
Inherits System.Runtime.InteropServices.StandardOleMarshalObject
Inherits DWebBrowserEvents2
Private _Browser As ExtendWebBrowser
Public Sub New(ByVal browser As ExtendWebBrowser)
_Browser = browser
End Sub
'Implement whichever events you wish
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)
_Browser.OnBeforeNavigate(DirectCast(URL, String), DirectCast(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)
_Browser.OnBeforeNewWindow(DirectCast(URL, String), cancel)
End Sub
End Class
<System.Runtime.InteropServices.ComImport(), System.Runtime.InteropServices.Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D"), System.Runtime.InteropServices.InterfaceTypeAttribute(System.Runtime.InteropServices.ComInterfaceType.InterfaceIsIDispatch), System.Runtime.InteropServices.TypeLibType(System.Runtime.InteropServices.TypeLibTypeFlags.FHidden)> _
Public Interface DWebBrowserEvents2
<System.Runtime.InteropServices.DispId(250)> _
Sub BeforeNavigate2(<System.Runtime.InteropServices.In(), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.IDispatch)> ByVal pDisp As Object, <System.Runtime.InteropServices.In()> ByRef URL As Object, <System.Runtime.InteropServices.In()> ByRef flags As Object, <System.Runtime.InteropServices.In()> ByRef targetFrameName As Object, <System.Runtime.InteropServices.In()> ByRef postData As Object, <System.Runtime.InteropServices.In()> ByRef headers As Object, _
<System.Runtime.InteropServices.In(), System.Runtime.InteropServices.Out()> ByRef cancel As Boolean)
<System.Runtime.InteropServices.DispId(273)> _
Sub NewWindow3(<System.Runtime.InteropServices.In(), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.IDispatch)> ByVal pDisp As Object, <System.Runtime.InteropServices.In(), System.Runtime.InteropServices.Out()> ByRef cancel As Boolean, <System.Runtime.InteropServices.In()> ByRef flags As Object, <System.Runtime.InteropServices.In()> ByRef URLContext As Object, <System.Runtime.InteropServices.In()> ByRef URL As Object)
End Interface
End Class
Public Class WebBrowserExtendedNavigatingEventArgs
Inherits CancelEventArgs
Private _Url As String
Public ReadOnly Property Url() As String
Get
Return _Url
End Get
End Property
Private _Frame As String
Public ReadOnly Property Frame() As String
Get
Return _Frame
End Get
End Property
Public Sub New(ByVal url As String, ByVal frame As String)
MyBase.New()
_Url = url
_Frame = frame
End Sub
End Class
End Namespace