首页 > 编程 > JavaScript > 正文

HTML+JavaScript+CSS结合winform开发

2019-11-08 02:02:08
字体:
来源:转载
供稿:网友

用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方法的参数


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表