zoukankan      html  css  js  c++  java
  • C# WinForm与客户端脚本的互操作

    using System;
    using System.Windows.Forms;
    using System.Web;
    using System.Security.Permissions;

    namespace WindowsFormsApplication2
    {
        [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
        [System.Runtime.InteropServices.ComVisibleAttribute(true)]

        public partial class JsCallWinFun : Form
        {
            private WebBrowser webBrowser1 = new WebBrowser();
            private Button button1 = new Button();

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

            public JsCallWinFun()
            {
                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)
            {//客户端脚本调用WinFrom方法示例
                webBrowser1.AllowWebBrowserDrop = false;
                webBrowser1.IsWebBrowserContextMenuEnabled = true;
                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)
            {
                //WinFrom调用客户端脚本示例
                webBrowser1.Document.InvokeScript("test",
                    new String[] { "called from client code" });
            }
        }
    }

  • 相关阅读:
    Spring容器基础ClassPathXmlApplicationContext(一起看源码)
    Spring容器基础xmlbeanfactory(一起看源码)
    java类库字符串操作
    反射
    maven配置文件解析
    红帽(Red Hat Linux)下SVN服务器的安装与配置
    Ant构建与部署Java项目---入门
    输入两个链表,找出他们的第一个公共节点
    java实现双向链表
    java实现双端链表
  • 原文地址:https://www.cnblogs.com/gghxh/p/1106206.html
Copyright © 2011-2022 走看看