HTML+JavaScript+CSS结合winform开发

用HTML+JavaScript+CSS写好网页后放到webbrowser中,实现Winform开发。
如图,先写好网页:
这里写图片描述
再写一个自定义控件,继承webbrowser,使用该控件时要调用InitUI方法初始化。

    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public partial class WebTool : WebBrowser
    {

        public WebTool(string htmlPath)
        {
            InitializeComponent();           
        }

        public void InitUI(string htmlPath)
        {
            string baseDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            if (System.IO.File.Exists(System.IO.Path.Combine(baseDirectory, htmlPath)))
            {
                this.ObjectForScripting = this;
                this.Navigate(System.IO.Path.Combine(baseDirectory, htmlPath));
            }
        }

        #region ----C#执行JS脚本----
        public void JsMethod(string data)
        {
            webBrowser.Document.InvokeScript("JsMethodTest", new object[] { data });
        }

        public string JsMethod1()
        {
            return webBrowser.Document.InvokeScript("JsMethodTest1").ToString();
        }
        #endregion
    }

一、JS调用c#函数
1、 加载HTML页面的webbrowser所在的控件要设置对COM组件可访问,如下:

    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public partial class WebTool : WebBrowser

2、 HTML页面加载完成后要设置

 this.ObjectForScripting = this;

3、 JS脚本执行window.external.functionName()来调用c#里的函数,functionName为c#中的函数名,此函数访问权限需为public

二、C#调用JS

webBrowser.Document.InvokeScript(参数)

这里写图片描述


object数组为要传给JS方法的参数

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值