首页 > 编程知识 正文

cefsharp多线程,winform第三方控件

时间:2023-05-05 04:48:05 阅读:134735 作者:312

简而言之,基本信息CefSharp是一个以. Net编写的浏览器包,对于嵌入在Winform和WPF中的Chrome浏览器组件非常有用。

资源GitHub地址:传送门

维客帮助文档地址:传送门

CefSharp最小样例项目:传送门

gitter交流讨论区:传送门

快速入门本文使用cefsharp/71的版本

需要3http://www.Sina.com/cef sharp 45.0或更高版本。 必须安装VC 2013 redistributablepackagex 86之前的版本。 必须安装VC 2012 redistributablepackagex 86.net framework 4.5.2。 这个安装没有想象中那么简单。 与坑爹相比,对构成进行各种修改。 根据官网的A构成方案,这个安装没有想象中那么简单,对于外语不好的我来说,看了英语文档很头疼。 虽然是外国人给的闭坑指南,但是感觉没有什么蛋用的。 这里介绍一下b方案的安装和部署过程吧。 a方案我就不提了。 想看的人请在上面的官方网站上确认。

整个简单的测试部署过程工程可通过GitHub下载:传送门

创建项目test.app(winform项目),并删除其中的Form1表格。 创建工程Test.Chrome (类库)。 将NuGet引用添加到Test.Chrome项目中,搜索CefSharp,然后选择CefSharp.Winforms。 指向解决方案上的配置管理器,并将平台设置为x86或x64。 将Form1窗体添加到Test.Chrome项目中,并添加与CefSharp窗体相关的代码。 Test.App将添加对Test.Chrome项目的引用,修改Program.cs文件,然后引用Test.Chrome项目的Form1表单。 详细的部署流程屏幕快照创建基本的winform APP描述,并使用NuGet浏览CefSharp包。 使用Nuget添加引用,搜索CefSharp,然后添加CefSharp.WinForm。 CefSharp.WinForm依赖于几个包。 这个选择这个安装就可以了。 Nuget会自动将其他依赖的软件包一起下载。

安装完成后,本地Packages文件夹中有以下文件:

官方文档建议在安装NuGet包后,关闭并重新打开vs,以确保vs具有的智能感知参考没有问题

右键单击摘要方案-选择配置管理-将目标平台更改为x86或x64

选择x86或x64

在您的表单Form1表单中添加相应的代码,然后浏览usingcef(asbrowser )中的代码。 usingcef(asbrowser )在代码中引用相应的dll

using CefSharp; using CefSharp.WinForms; 完整示例:

用户系统; using system.collections.generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using CefSharp; using CefSharp.WinForms; namespaceembebbedchromium { publicpartialclassform 1: form } publicchromiumwebbrowserchromebrowser; public form1((初始化组件) ); //startthebrowserafterinitializeglobalcomponentinitializechromium (; } privatevoidform1_ load (object sender,EventArgs e ) )//初始化浏览器,然后单击public void initialize chromium { cefsettingsssettings } //createabrowsercomponentchromebrowser=newchromiumwebbrowser (' https://www.Baidu.com); //addittotheformandfillittotheformwindow。

this.Controls.Add(chromeBrowser); chromeBrowser.Dock = DockStyle.Fill; }//窗体关闭时,记得停止浏览器 private void Form1_FormClosing(object sender, FormClosingEventArgs e) { Cef.Shutdown(); } }}

3.2. Using CEF (as User Interface)

这块参考官方文档:传送门,做了部分内容的完善,这块我理解的就是提供了一个js调用C#类方法的一个示例。

下载Bootstrap相关的文件,传送门

将下载好的Bootstrap文件夹复制拷贝到你的VS项目中,并添加html文件夹,ckdmj新建一个index.html文件,具体如下图所示:

html文件内容参考这里:传送门,网页下方有示例,也有模板可以下载。

<!DOCTYPE html><html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags --> <title>Bootstrap 101 Template</title> <!-- Bootstrap --> <link rel="external nofollow" href="css/bootstrap.min.css" rel="stylesheet"> <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --> <!-- WARNING: Respond.js doesn't work if you view the page via file:// --> <!--[if lt IE 9]> <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script> <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> <![endif]--> </head> <body> <h1>Hello, world!</h1> <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <!-- Include all compiled plugins (below), or include individual files as needed --> <script src="js/bootstrap.min.js"></script> </body></html>

将上面的css、fonts、html、js文件夹里的文件全部选中—》然后点击鼠标右键,选中属性—》设置始终复制

新建一个类CefCustomObject,用来让js调用C#中类的方法,具体代码如下:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using CefSharp;using CefSharp.WinForms;using System.Diagnostics;namespace embebbedChromium{ class CefCustomObject { // Declare a local instance of chromium and the main form in order to execute things from here in the main thread private static ChromiumWebBrowser _instanceBrowser = null; // The form class needs to be changed according to yours private static Form1 _instanceMainForm = null; public CefCustomObject(ChromiumWebBrowser originalBrowser, Form1 mainForm) { _instanceBrowser = originalBrowser; _instanceMainForm = mainForm; } public void showDevTools() { _instanceBrowser.ShowDevTools(); } public void opencmd() { ProcessStartInfo start = new ProcessStartInfo("cmd.exe", "/c pause"); Process.Start(start); } }}

修改Form1窗体的代码

using System;using System.IO;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using CefSharp;using CefSharp.WinForms;using System.Runtime.InteropServices;namespace embebbedChromium{ public partial class Form1 : Form { public ChromiumWebBrowser chromeBrowser; public Form1() { InitializeComponent(); // Start the browser after initialize global component InitializeChromium(); //需要添加此句代码,否则下面执行会报错CefSharpSettings.LegacyJavascriptBindingEnabled = true; // Register an object in javascript named "cefCustomObject" with function of the CefCustomObject class :3 chromeBrowser.RegisterJsObject("cefCustomObject", new CefCustomObject(chromeBrowser, this)); } private void Form1_Load(object sender, EventArgs e) { //此句代码执行有错,官网示例的跑不起来 //chromeBrowser.ShowDevTools(); } public void InitializeChromium() { CefSettings settings = new CefSettings(); // Note that if you get an error or a white screen, you may be doing something wrong ! // Try to load a local file that you're sure that exists and give the complete path instead to test // for example, replace page with a direct path instead : // String page = @"C:UsersSDkCarlosDesktopafolderindex.html"; String page = string.Format(@"{0}html-resourceshtmlindex.html", Application.StartupPath); //String page = @"C:UsersSDkCarlosDesktopartyom-HOMEPAGEindex.html"; if (!File.Exists(page)) { MessageBox.Show("Error The html file doesn't exists : "+page); } // Initialize cef with the provided settings Cef.Initialize(settings); // Create a browser component chromeBrowser = new ChromiumWebBrowser(page); // Add it to the form and fill it to the form window. this.Controls.Add(chromeBrowser); chromeBrowser.Dock = DockStyle.Fill; // Allow the use of local resources in the browser BrowserSettings browserSettings = new BrowserSettings(); browserSettings.FileAccessFromFileUrls = CefState.Enabled; browserSettings.UniversalAccessFromFileUrls = CefState.Enabled; chromeBrowser.BrowserSettings = browserSettings; } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { Cef.Shutdown(); } }} 运行查看效果,如下图所示

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。