c# winform中使用WebKit實作網頁與winform的互動
google下載下傳位址 :https://code.google.com/archive/p/open-webkit-sharp/
github網址 : https://github.com/Erls-Corporation/open-webkit-sharp
開發工具:Vs2017
下載下傳之後 解壓壓縮包. 把Core檔案夾和References檔案夾下所有檔案拷貝到你的工程目錄(bin)下,然後打開你的項目,添加引用OpenWebKitSharp.dll和WebKit.Interop.dll(如果你的項目運作在.NET Framework 2.0 或 3.5 引用 Binary_NET2檔案夾下的這兩個檔案,NET4.0的話就引用Binary檔案夾下的這兩個dll);

到自己的WinForm程式,在工具箱選擇按右鍵-選擇項
點選浏覽選擇OpenWebKitSharp.dll,然後把WebKitBrowser拖到winfrom窗體裡。
想要html頁面通路背景 必須在前面加上
//目前類可以com元件的形式供外包調用
//[ComVisible(true)]
//[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
winfrom背景程式
public class myClass
{
public int Test(int a ,int b)
{
//System.Windows.Forms.MessageBox.Show("alert:JS回調成功");
int c = a * b;
return c;
//System.Windows.Forms.MessageBox.Show("JS回調值alert:"+c);
}
}
private void Form1_Load(object sender, EventArgs e)
{
webKitBrowser1.Navigate("url");
this.webKitBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webKitBrowser1_DocumentCompleted);
}
public void webKitBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
this.webKitBrowser1.GetScriptManager.ScriptObject = new myClass();
}
原文章位址:https://www.cnblogs.com/wxxf/p/9119315.html