c# winform中使用WebKit实现网页与winform的交互

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);

c# winform中使用WebKit实现网页与winform的交互_第1张图片

到自己的WinForm程序,在工具箱选择按右键-选择项 

c# winform中使用WebKit实现网页与winform的交互_第2张图片

点击浏览选择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

你可能感兴趣的:(c# winform中使用WebKit实现网页与winform的交互)