html中的js调用c#的winform程序

来源:互联网 发布:宁波知乎 编辑:程序博客网 时间:2024/05/21 09:53

简单来说,就是在c#客户端winform中加载一个webBrowser1,然后通过webBrowser1中对页面的点击js效果触发c#的函数响应。亲测通过。

C#代码如下:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace testJS{    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]    [System.Runtime.InteropServices.ComVisibleAttribute(true)]    public partial class Form1 : Form    {        public void say()        {            MessageBox.Show("OK");        }        public Form1()        {            InitializeComponent();        }        private void Form1_Load(object sender, EventArgs e)        {            this.webBrowser1.ObjectForScripting = this;            this.webBrowser1.Navigate("http://localhost/1.html");        }    }}

js代码如下:

<script>
function say()
{
var tnum=window.external.say();//getDebugPath()为c#方法
}
</script>
hi
<input type="button" value="OK" onclick="say()">



测试可以输出


0 0
原创粉丝点击