标签:
WebBrowser.ObjectForScripting 设置或者获得一个对象,该对象可以由在WebBrowser控件中的Web页面上的Script COde 访问
注意:
使用该属性,可以在WebBrowser控件所拥有的Web 页面和拥有WebBrowser的应用程式之间通讯。该属性可以让你用客户端应用程式Code来写动态HTML,
为使这个属性有效,须注意Web页面的Script须用window.external来调用,你需要在 ComVisibleAttribute Attribute来标注Class类
例子:
namespace WindowsFormsWebBrowser { /// <summary> /// WebBrowser /// </summary> [ComVisible(true)] public partial class Form1 : Form { private Button button1 = new Button(); private WebBrowser webBrowser1 = new WebBrowser(); public Form1() { InitializeComponent(); 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" }); } } }
Winform WebBrowser.ObjectForScripting 用法汇整
标签:
原文地址:http://www.cnblogs.com/kittyguo/p/4763172.html