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

WebBrowser コントロールを使用して、既存の動的 HTML (DHTML) Web アプリケーション コードを Windows フォーム クライアント アプリケーションに追加できます。 これは、DHTML ベースのコントロールの作成にかなりの開発時間を投資し、既存のコードを再作成せずに Windows フォームの機能が豊富なユーザー インターフェイスを利用したい場合に役立ちます。

WebBrowser コントロールでは、ObjectForScripting プロパティと Document プロパティを通じて、クライアント アプリケーション コードと Web ページ内のスクリプト コードの間の双方向の通信を実装できます。 また、WebBrowser コントロールを構成して、Web コントロールをアプリケーション フォームのその他のコントロールとシームレスに統合し、DHTML 実装を非表示にすることができます。 コントロールをシームレスに統合するには、背景色と視覚スタイルが残りのフォームと一致するように表示されるページの書式を設定し、AllowWebBrowserDropIsWebBrowserContextMenuEnabled、および 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. フォームのコンストラクターの ObjectForScripting プロパティを設定するか、OnLoad メソッドをオーバーライドします。

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

    webBrowser1.ObjectForScripting = new MyScriptObject(this);
    
    webBrowser1.ObjectForScripting = New MyScriptObject(Me)
    
  5. スクリプト オブジェクトを実装します。

    public class MyScriptObject
    {
        private Form1 _form;
    
        public MyScriptObject(Form1 form)
        {
            _form = form;
        }
    
        public void Test(string message)
        {
            MessageBox.Show(message, "client code");
        }
    }
    
    Public Class MyScriptObject
        Private _form As Form1
    
        Public Sub New(ByVal form As Form1)
            _form = form
        End Sub
    
        Public Sub Test(ByVal message As String)
            MessageBox.Show(message, "client code")
        End Sub
    
    End Class
    
  6. 指定されたオブジェクトのパブリック プロパティおよびメソッドへのアクセスには、スクリプト コードで window.external オブジェクトを使用します。

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

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

    次の HTML SCRIPT 要素は、関数の例を提供しています。 コントロールの Navigate メソッドを使用して読み込む、またはコントロールの 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 コントロールに読み込まれます。

using System;
using System.Windows.Forms;

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);
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        webBrowser1.AllowWebBrowserDrop = false;
        webBrowser1.IsWebBrowserContextMenuEnabled = false;
        webBrowser1.WebBrowserShortcutsEnabled = false;
        webBrowser1.ObjectForScripting = new MyScriptObject(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>";
    }

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

public class MyScriptObject
{
    private Form1 _form;

    public MyScriptObject(Form1 form)
    {
        _form = form;
    }

    public void Test(string message)
    {
        MessageBox.Show(message, "client code");
    }
}
Imports System.Windows.Forms

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

    Protected Overrides Sub OnLoad(e As EventArgs)
        MyBase.OnLoad(e)

        webBrowser1.AllowWebBrowserDrop = False
        webBrowser1.IsWebBrowserContextMenuEnabled = False
        webBrowser1.WebBrowserShortcutsEnabled = False
        webBrowser1.ObjectForScripting = New MyScriptObject(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

    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

Public Class MyScriptObject
    Private _form As Form1

    Public Sub New(ByVal form As Form1)
        _form = form
    End Sub

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

End Class

コードのコンパイル

このコードには、次のものが必要です。

  • System アセンブリおよび System.Windows.Forms アセンブリへの参照。

関連項目