次の方法で共有


方法 : DHTML コードとクライアント アプリケーション コード間の双方向の通信を実装する

WebBrowser コントロールを使用すると、既存のダイナミック HTML (DHTML) Web アプリケーションのコードを、Windows フォーム クライアント アプリケーションに追加できます。 作成に多大な時間を費やした DHTML ベースのコントロールが既にあり、このコードを書き直すことなく Windows フォームの豊富なユーザー インターフェイス機能を使用したい場合には、このコントロールが役に立ちます。

WebBrowser コントロールを使用すると、ObjectForScripting プロパティおよび Document プロパティを使用して、クライアント アプリケーションのコードと Web ページのスクリプト コードとの双方向の通信を実装できます。 また、DHTML 実装を非表示にしながら、Web コントロールをアプリケーション フォームにある他のコントロールとシームレスに組み合わせるように、WebBrowser コントロールを設定することもできます。 シームレスにコントロールを組み合わせるには、表示されるページの背景色と visual スタイルをフォームの他の部分に一致させ、AllowWebBrowserDrop プロパティ、IsWebBrowserContextMenuEnabled プロパティ、および WebBrowserShortcutsEnabled プロパティを使用して、標準のブラウザー機能を無効にします。

Windows フォーム アプリケーションに DHTML を埋め込むには

  1. WebBrowser コントロールの AllowWebBrowserDrop プロパティを false に設定して、WebBrowser コントロールにファイルをドロップしたときに、そのファイルが開かないようにします。

    webBrowser1.AllowWebBrowserDrop = False
    
    webBrowser1.AllowWebBrowserDrop = false;
    
  2. このコントロールの IsWebBrowserContextMenuEnabled プロパティを false に設定して、WebBrowser コントロールでユーザーが右クリックしたときに、ショートカット メニューが表示されないようにします。

    webBrowser1.IsWebBrowserContextMenuEnabled = False
    
    webBrowser1.IsWebBrowserContextMenuEnabled = false;
    
  3. このコントロールの WebBrowserShortcutsEnabled プロパティを false に設定して、WebBrowser コントロールがショートカット キーに応答しないようにします。

    webBrowser1.WebBrowserShortcutsEnabled = False
    
    webBrowser1.WebBrowserShortcutsEnabled = false;
    
  4. フォームのコンストラクターか Load イベント ハンドラーで ObjectForScripting プロパティを設定します。

    次のコードでは、スクリプト オブジェクトとして、フォーム クラス自体を使用しています。

    注意

    コンポーネント オブジェクト モデル (COM: Component Object Model) は、スクリプト オブジェクトにアクセスできる必要があります。 COM からフォームを表示できるようにするには、ComVisibleAttribute 属性をフォームのクラスに追加します。

    webBrowser1.ObjectForScripting = Me
    
    webBrowser1.ObjectForScripting = this;
    
  5. スクリプト コードで使用するパブリック プロパティまたはメソッドをアプリケーション コードに実装します。

    たとえば、スクリプト オブジェクトにフォーム クラスを使用する場合は、フォーム クラスに次のコードを追加します。

    Public Sub Test(ByVal message As String)
        MessageBox.Show(message, "client code")
    End Sub
    
    public void Test(String message)
    {
        MessageBox.Show(message, "client code");
    }
    
  6. スクリプト コードで window.external オブジェクトを使用して、指定のオブジェクトのパブリック プロパティおよびメソッドにアクセスします。

    次の HTML コードは、ボタン クリック時のスクリプト オブジェクトでメソッドを呼び出す方法を示しています。 このコードを、コントロールの Navigate メソッドを使用して読み込む HTML ドキュメントまたはコントロールの DocumentText プロパティに割り当てる HTML ドキュメントの BODY 要素にコピーします。

    <button onclick="window.external.Test('called from script code')">
        call client code from script code
    </button>
    
  7. アプリケーション コードで使用する関数をスクリプト コードに実装します。

    次の HTML SCRIPT 要素に関数の例を示します。 このコードを、コントロールの Navigate メソッドを使用して読み込む HTML ドキュメントまたはコントロールの DocumentText プロパティに割り当てる HTML ドキュメントの HEAD 要素に埋め込みます。

    <script>
    function test(message) { 
        alert(message); 
    }
    </script>
    
  8. クライアント アプリケーションのコードから スクリプト コードにアクセスするには、Document プロパティを使用します。

    たとえば、次のコードをボタンの Click イベント ハンドラーに追加します。

    webBrowser1.Document.InvokeScript("test", _
        New String() {"called from client code"})
    
    webBrowser1.Document.InvokeScript("test",
        new String[] { "called from client code" });
    
  9. DHTML のデバッグが終了したら、コントロールの ScriptErrorsSuppressed プロパティを true に設定して、スクリプト コードの問題に対して WebBrowser コントロールでエラー メッセージが表示されないようにします。

    ' Uncomment the following line when you are finished debugging.
    'webBrowser1.ScriptErrorsSuppressed = True
    
    // Uncomment the following line when you are finished debugging.
    //webBrowser1.ScriptErrorsSuppressed = true;
    

使用例

次の完成したコード例は、この機能を理解するために役立つデモ アプリケーションです。 HTML コードは、個別の HTML ファイルから読み込まれるのではなく、DocumentText プロパティによって WebBrowser コントロールへ読み込まれます。

Imports System
Imports System.Windows.Forms
Imports System.Security.Permissions

<PermissionSet(SecurityAction.Demand, Name:="FullTrust")> _
<System.Runtime.InteropServices.ComVisibleAttribute(True)> _
Public Class Form1
    Inherits Form

    Private webBrowser1 As New WebBrowser()
    Private WithEvents button1 As New Button()

    <STAThread()> _
    Public Shared Sub Main()
        Application.EnableVisualStyles()
        Application.Run(New Form1())
    End Sub

    Public Sub New()
        button1.Text = "call script code from client code"
        button1.Dock = DockStyle.Top
        webBrowser1.Dock = DockStyle.Fill
        Controls.Add(webBrowser1)
        Controls.Add(button1)
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) _
        Handles Me.Load

        webBrowser1.AllowWebBrowserDrop = False
        webBrowser1.IsWebBrowserContextMenuEnabled = False
        webBrowser1.WebBrowserShortcutsEnabled = False
        webBrowser1.ObjectForScripting = Me
        ' Uncomment the following line when you are finished debugging.
        'webBrowser1.ScriptErrorsSuppressed = True

        webBrowser1.DocumentText = _
            "<html><head><script>" & _
            "function test(message) { alert(message); }" & _
            "</script></head><body><button " & _
            "onclick=""window.external.Test('called from script code')"" > " & _
            "call client code from script code</button>" & _
            "</body></html>"
    End Sub

    Public Sub Test(ByVal message As String)
        MessageBox.Show(message, "client code")
    End Sub

    Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
        Handles button1.Click

        webBrowser1.Document.InvokeScript("test", _
            New String() {"called from client code"})

    End Sub

End Class
using System;
using System.Windows.Forms;
using System.Security.Permissions;

[PermissionSet(SecurityAction.Demand, Name="FullTrust")]
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public class Form1 : Form
{
    private WebBrowser webBrowser1 = new WebBrowser();
    private Button button1 = new Button();

    [STAThread]
    public static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }

    public Form1()
    {
        button1.Text = "call script code from client code";
        button1.Dock = DockStyle.Top;
        button1.Click += new EventHandler(button1_Click);
        webBrowser1.Dock = DockStyle.Fill;
        Controls.Add(webBrowser1);
        Controls.Add(button1);
        Load += new EventHandler(Form1_Load);
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        webBrowser1.AllowWebBrowserDrop = false;
        webBrowser1.IsWebBrowserContextMenuEnabled = false;
        webBrowser1.WebBrowserShortcutsEnabled = false;
        webBrowser1.ObjectForScripting = this;
        // Uncomment the following line when you are finished debugging.
        //webBrowser1.ScriptErrorsSuppressed = true;

        webBrowser1.DocumentText =
            "<html><head><script>" +
            "function test(message) { alert(message); }" +
            "</script></head><body><button " +
            "onclick=\"window.external.Test('called from script code')\">" +
            "call client code from script code</button>" +
            "</body></html>";
    }

    public void Test(String message)
    {
        MessageBox.Show(message, "client code");
    }

    private void button1_Click(object sender, EventArgs e)
    {
        webBrowser1.Document.InvokeScript("test",
            new String[] { "called from client code" });
    }

}

コードのコンパイル

このコードで必要な要素は次のとおりです。

  • System アセンブリと System.Windows.Forms アセンブリへの参照。

Visual Basic または Visual C# のコマンド ラインからこの例をビルドする方法の詳細については、「コマンド ラインからのビルド (Visual Basic)」または「csc.exe を使用したコマンド ラインからのビルド」を参照してください。 Visual Studio で新しいプロジェクトにコードを貼り付けてこの例をビルドすることもできます。 詳細については 方法 : 完成した Windows フォーム コードの例を Visual Studio を使ってコンパイルして実行する および 方法 : 完成した Windows フォーム コードの例を Visual Studio を使ってコンパイルして実行する および 方法 : 完成した Windows フォーム コードの例を Visual Studio を使ってコンパイルして実行する および 方法 : 完成した Windows フォーム コードの例を Visual Studio を使ってコンパイルして実行する および 方法 : 完成した Windows フォーム コードの例を Visual Studio を使ってコンパイルして実行する.

参照

参照

WebBrowser

WebBrowser.Document

WebBrowser.ObjectForScripting

その他の技術情報

WebBrowser コントロール (Windows フォーム)