當前位置: 首頁>>代碼示例>>VB.NET>>正文


VB.NET WebBrowser.CreateSink方法代碼示例

本文整理匯總了VB.NET中System.Windows.Forms.WebBrowser.CreateSink方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET WebBrowser.CreateSink方法的具體用法?VB.NET WebBrowser.CreateSink怎麽用?VB.NET WebBrowser.CreateSink使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Windows.Forms.WebBrowser的用法示例。


在下文中一共展示了WebBrowser.CreateSink方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。

示例1: Main

' 導入命名空間
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Imports System.Security.Permissions

Namespace WebBrowserExtensibility

    <PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")> _
    Public Class Form1
        Inherits Form

        <STAThreadAttribute()> Public Shared Sub Main()
            Application.Run(New Form1())
        End Sub

        Private WithEvents wb As New WebBrowser2()

        Public Sub New()

            wb.Dock = DockStyle.Fill
            Controls.Add(wb)

            ' Attempt to navigate to an invalid address.
            wb.Navigate("www.widgets.microsoft.com")

        End Sub

        Private Sub wb_NavigateError( _
            ByVal sender As Object, _
            ByVal e As WebBrowserNavigateErrorEventArgs) _
            Handles wb.NavigateError

            ' Display an error message to the user.
            MessageBox.Show("Cannot navigate to " + e.Url)

        End Sub

    End Class

    Public Class WebBrowser2
        Inherits WebBrowser

        Private cookie As AxHost.ConnectionPointCookie
        Private helper As WebBrowser2EventHelper

        <PermissionSetAttribute(SecurityAction.LinkDemand, _
        Name := "FullTrust")> Protected Overrides Sub CreateSink()

            MyBase.CreateSink()

            ' Create an instance of the client that will handle the event
            ' and associate it with the underlying ActiveX control.
            helper = New WebBrowser2EventHelper(Me)
            cookie = New AxHost.ConnectionPointCookie( _
                Me.ActiveXInstance, helper, GetType(DWebBrowserEvents2))
        End Sub

        <PermissionSetAttribute(SecurityAction.LinkDemand, _
        Name := "FullTrust")> Protected Overrides Sub DetachSink()

            ' Disconnect the client that handles the event
            ' from the underlying ActiveX control.
            If cookie IsNot Nothing Then
                cookie.Disconnect()
                cookie = Nothing
            End If
            MyBase.DetachSink()

        End Sub

        Public Event NavigateError As WebBrowserNavigateErrorEventHandler

        ' Raises the NavigateError event.
        Protected Overridable Sub OnNavigateError( _
            ByVal e As WebBrowserNavigateErrorEventArgs)

            RaiseEvent NavigateError(Me, e)

        End Sub

        ' Handles the NavigateError event from the underlying ActiveX 
        ' control by raising the NavigateError event defined in this class.
        Private Class WebBrowser2EventHelper
            Inherits StandardOleMarshalObject
            Implements DWebBrowserEvents2

            Private parent As WebBrowser2

            Public Sub New(ByVal parent As WebBrowser2)
                Me.parent = parent
            End Sub

            Public Sub NavigateError(ByVal pDisp As Object, _
                ByRef URL As Object, ByRef frame As Object, _
                ByRef statusCode As Object, ByRef cancel As Boolean) _
                Implements DWebBrowserEvents2.NavigateError

                ' Raise the NavigateError event.
                Me.parent.OnNavigateError( _
                    New WebBrowserNavigateErrorEventArgs( _
                    CStr(URL), CStr(frame), CInt(statusCode), cancel))

            End Sub

        End Class

    End Class

    ' Represents the method that will handle the WebBrowser2.NavigateError event.
    Public Delegate Sub WebBrowserNavigateErrorEventHandler(ByVal sender As Object, _
        ByVal e As WebBrowserNavigateErrorEventArgs)

    ' Provides data for the WebBrowser2.NavigateError event.
    Public Class WebBrowserNavigateErrorEventArgs
        Inherits EventArgs

        Private urlValue As String
        Private frameValue As String
        Private statusCodeValue As Int32
        Private cancelValue As Boolean

        Public Sub New( _
            ByVal url As String, ByVal frame As String, _
            ByVal statusCode As Int32, ByVal cancel As Boolean)

            Me.urlValue = url
            Me.frameValue = frame
            Me.statusCodeValue = statusCode
            Me.cancelValue = cancel

        End Sub

        Public Property Url() As String
            Get
                Return urlValue
            End Get
            Set(ByVal value As String)
                urlValue = value
            End Set
        End Property

        Public Property Frame() As String
            Get
                Return frameValue
            End Get
            Set(ByVal value As String)
                frameValue = value
            End Set
        End Property

        Public Property StatusCode() As Int32
            Get
                Return statusCodeValue
            End Get
            Set(ByVal value As Int32)
                statusCodeValue = value
            End Set
        End Property

        Public Property Cancel() As Boolean
            Get
                Return cancelValue
            End Get
            Set(ByVal value As Boolean)
                cancelValue = value
            End Set
        End Property

    End Class

    ' Imports the NavigateError method from the OLE DWebBrowserEvents2 
    ' interface. 
    <ComImport(), Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D"), _
    InterfaceType(ComInterfaceType.InterfaceIsIDispatch), _
    TypeLibType(TypeLibTypeFlags.FHidden)> _
    Public Interface DWebBrowserEvents2

        <DispId(271)> Sub NavigateError( _
            <InAttribute(), MarshalAs(UnmanagedType.IDispatch)> _
            ByVal pDisp As Object, _
            <InAttribute()> ByRef URL As Object, _
            <InAttribute()> ByRef frame As Object, _
            <InAttribute()> ByRef statusCode As Object, _
            <InAttribute(), OutAttribute()> ByRef cancel As Boolean)

    End Interface

End Namespace
開發者ID:VB.NET開發者,項目名稱:System.Windows.Forms,代碼行數:188,代碼來源:WebBrowser.CreateSink


注:本文中的System.Windows.Forms.WebBrowser.CreateSink方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。