可以使用控件 WebBrowser 将现有的动态 HTML (DHTML) Web 应用程序代码添加到 Windows 窗体客户端应用程序。 当你在创建基于 DHTML 的控件方面投入了大量开发时间,并且你希望利用 Windows 窗体丰富的用户界面功能,而无需重写现有代码时,这非常有用。
该WebBrowser控件允许通过ObjectForScripting和Document属性在客户端应用程序代码和网页脚本代码之间实现双向通信。 此外,还可以配置 WebBrowser 控件,以便 Web 控件与应用程序窗体上的其他控件无缝混合,从而隐藏其 DHTML 实现。 若要无缝混合控件,请设置所显示页面的格式,使其背景色和视觉样式与窗体的其余部分匹配,并使用AllowWebBrowserDrop和IsWebBrowserContextMenuEnabledWebBrowserShortcutsEnabled属性禁用标准浏览器功能。
在 Windows 窗体应用程序中嵌入 DHTML
将 WebBrowser 控件 AllowWebBrowserDrop 的属性设置为
false
阻止 WebBrowser 控件打开放入其中的文件。webBrowser1.AllowWebBrowserDrop = false;
webBrowser1.AllowWebBrowserDrop = False
将控件 IsWebBrowserContextMenuEnabled 的属性设置为
false
防止 WebBrowser 控件在用户右键单击时显示其快捷菜单。webBrowser1.IsWebBrowserContextMenuEnabled = false;
webBrowser1.IsWebBrowserContextMenuEnabled = False
设置控件WebBrowserShortcutsEnabled的属性以防止
false
WebBrowser控件响应快捷键。webBrowser1.WebBrowserShortcutsEnabled = false;
webBrowser1.WebBrowserShortcutsEnabled = False
在 ObjectForScripting 窗体的构造函数中设置属性或重写 OnLoad 方法。
以下代码将窗体类本身用于脚本对象。
webBrowser1.ObjectForScripting = new MyScriptObject(this);
webBrowser1.ObjectForScripting = New MyScriptObject(Me)
实现脚本对象。
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
window.external
使用脚本代码中的对象访问指定对象的公共属性和方法。以下 HTML 代码演示如何从按钮单击调用脚本对象的方法。 将此代码复制到使用控件的方法或分配给DocumentText控件属性的 HTML 文档的 Navigate BODY 元素中。
<button onclick="window.external.Test('called from script code')"> call client code from script code </button>
在脚本代码中实现应用程序代码将使用的函数。
以下 HTML SCRIPT 元素提供了一个示例函数。 将此代码复制到使用控件的 Navigate 方法加载的 HTML 文档的 HEAD 元素中,或分配给控件的 DocumentText 属性的 HTML 文档的 HEAD 元素中。
<script> function test(message) { alert(message); } </script>
使用 Document 属性从客户端应用程序代码访问脚本代码。
例如,将以下代码添加到按钮 Click 事件处理程序。
webBrowser1.Document.InvokeScript("test", new String[] { "called from client code" });
webBrowser1.Document.InvokeScript("test", _ New String() {"called from client code"})
完成 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 代码通过 DocumentText 属性加载到 WebBrowser 控件中,而不是从单独的 HTML 文件加载。
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 程序集的引用。