zoukankan      html  css  js  c++  java
  • Winform WebBrowser.ObjectForScripting 用法汇整

    WebBrowser.ObjectForScripting 设置或者获得一个对象,该对象可以由在WebBrowser控件中的Web页面上的Script COde 访问

    注意:

    使用该属性,可以在WebBrowser控件所拥有的Web 页面和拥有WebBrowser的应用程式之间通讯。该属性可以让你用客户端应用程式Code来写动态HTML,

    为使这个属性有效,须注意Web页面的Script须用window.external来调用,你需要在 ComVisibleAttribute Attribute来标注Class类

    例子:

    namespace WindowsFormsWebBrowser
    {
        /// <summary>
        /// Form1 as COM 
        /// </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;//允许使用ObjectForScripting

    // Uncomment the following line when you are finished debugging. webBrowser1.ScriptErrorsSuppressed = true;      // onclick按钮触发Form1的Test方法
    webBrowser1.DocumentText = "<html><head><script>" + "function hosttest(message) { alert(message); }" + "</script></head><body><button " + "onclick="window.external.Test('called from script code')">" + "call client code from script code</button>" + "</body></html>"; }   
    // 允许Web Page ONclick事件调用的方法,window.external.Test()
    public void Test(String message) { MessageBox.Show(message, "client code"); } // Button1的Click触发事件,调用web Page的JavaScript Function hosttest
    private void button1_Click(object sender, EventArgs e) { webBrowser1.Document.InvokeScript("hosttest", new String[] { "called from client code" }); } } }

    最后运行结果:
    
    
  • 相关阅读:
    pgspider 一些ppt 截图
    postgres cassandra_fdw 扩展试用
    使用 postgres s3 fdw + cube.js 分析 csv 数据
    cube.js 集成s3 的一种方法
    postgres s3 fdw 试用
    cube.js 集成 elasticsearch 的一种变通方法
    使用postgres_fdw 串接elasticsearch fdw
    postgres elasticsearch fdw 学习
    使用vcpkg 管理c&&c++ 包
    postgres pg_cron 扩展连接远程pg server
  • 原文地址:https://www.cnblogs.com/kittyguo/p/4763172.html
Copyright © 2011-2022 走看看