標籤:winform style blog class code tar
簡單來說,就是在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()">
測試可以輸出