WebBrowser.CreateSink メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > WebBrowser.CreateSink メソッドの意味・解説 

WebBrowser.CreateSink メソッド

メモ : このメソッドは、.NET Framework version 2.0新しく追加されたものです。

コントロール イベント処理できるクライアントに、基になる ActiveX コントロール関連付けます。

名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文構文

解説解説
使用例使用例

WebBrowser から派生したクラスでこのメソッド使用し通常の WebBrowser イベントOLE DWebBrowserEvents2 インターフェイスNavigateError イベント補完するコード例次に示します

Imports System
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
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Security.Permissions;

namespace WebBrowserExtensibility
{
    [PermissionSetAttribute(SecurityAction.Demand, Name="FullTrust")]
    public class Form1 : Form
    {
        [STAThread]
        public static void
 Main()
        {
            Application.Run(new Form1());
        }

        private WebBrowser2 wb = new WebBrowser2();
        public Form1()
        {
            wb.Dock = DockStyle.Fill;
            wb.NavigateError += new 
                WebBrowserNavigateErrorEventHandler(wb_NavigateError);
            Controls.Add(wb);

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

        private void wb_NavigateError(
            object sender, WebBrowserNavigateErrorEventArgs e)
        {
            // Display an error message to the user.
            MessageBox.Show("Cannot navigate to " + e.Url);
        }
    }

    public class WebBrowser2 : WebBrowser
    {
        AxHost.ConnectionPointCookie cookie;
        WebBrowser2EventHelper helper;

        [PermissionSetAttribute(SecurityAction.LinkDemand, Name="FullTrust")]
        protected override void CreateSink()
        {
            base.CreateSink();

            // Create an instance of the client that will handle the
 event
            // and associate it with the underlying ActiveX control.
            helper = new WebBrowser2EventHelper(this);
            cookie = new AxHost.ConnectionPointCookie(
                this.ActiveXInstance, helper, typeof(DWebBrowserEvents2));
        }

        [PermissionSetAttribute(SecurityAction.LinkDemand, Name="FullTrust")]
        protected override void DetachSink()
        {
            // Disconnect the client that handles the event
            // from the underlying ActiveX control.
            if (cookie != null)
            {
                cookie.Disconnect();
                cookie = null;
            }
            base.DetachSink();
        }

        public event WebBrowserNavigateErrorEventHandler NavigateError;

        // Raises the NavigateError event.
        protected virtual void OnNavigateError(
            WebBrowserNavigateErrorEventArgs e)
        {
            if (this.NavigateError != null)
            {
                this.NavigateError(this, e);
            }
        }

        // Handles the NavigateError event from the underlying ActiveX
 
        // control by raising the NavigateError event defined in this
 class.
        private class WebBrowser2EventHelper
 : 
            StandardOleMarshalObject, DWebBrowserEvents2
        {
            private WebBrowser2 parent;

            public WebBrowser2EventHelper(WebBrowser2 parent)
            {
                this.parent = parent;
            }

            public void NavigateError(object
 pDisp, ref object url, 
                ref object frame, ref object statusCode, ref bool
 cancel)
            {
                // Raise the NavigateError event.
                this.parent.OnNavigateError(
                    new WebBrowserNavigateErrorEventArgs(
                    (String)url, (String)frame, (Int32)statusCode, cancel));
            }
        }
    }

    // Represents the method that will handle the WebBrowser2.NavigateError
 event.
    public delegate void WebBrowserNavigateErrorEventHandler(object
 sender, 
        WebBrowserNavigateErrorEventArgs e);

    // Provides data for the WebBrowser2.NavigateError event.
    public class WebBrowserNavigateErrorEventArgs
 : EventArgs
    {
        private String urlValue;
        private String frameValue;
        private Int32 statusCodeValue;
        private Boolean cancelValue;

        public WebBrowserNavigateErrorEventArgs(
            String url, String frame, Int32 statusCode, Boolean cancel)
        {
            urlValue = url;
            frameValue = frame;
            statusCodeValue = statusCode;
            cancelValue = cancel;
        }

        public String Url
        {
            get { return urlValue; }
            set { urlValue = value; }
        }

        public String Frame
        {
            get { return frameValue; }
            set { frameValue = value; }
        }

        public Int32 StatusCode
        {
            get { return statusCodeValue; }
            set { statusCodeValue = value; }
        }

        public Boolean Cancel
        {
            get { return cancelValue; }
            set { cancelValue = value; }
        }
    }

    // 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)]
        void NavigateError(
            [In, MarshalAs(UnmanagedType.IDispatch)] object pDisp,
            [In] ref object URL, [In] ref object frame, 
            [In] ref object statusCode, [In, Out] ref bool cancel);
    }
}
.NET Framework のセキュリティ.NET Frameworkセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からWebBrowser.CreateSink メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からWebBrowser.CreateSink メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からWebBrowser.CreateSink メソッド を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

WebBrowser.CreateSink メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



WebBrowser.CreateSink メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS