CEF框架:
Chromium嵌入式框架,
Chromium Embedded Framework
Chromium Embedded Framework (CEF)是个基于Google Chromium项目的开源Web browser控件,支持Windows, Linux, Mac平台。除了提供C/C++接口外,也有其他语言的移植版。
因为基于Chromium,所以CEF支持Webkit & Chrome中实现的HTML5的特性,并且在性能上面,也比较接近Chrome。
CEF还提供的如下特性:自定义插件、自定义协议、自定义JavaScript对象和扩展;可控制的resource loading, navigation, context menus等等。
其中,对于.Net(C#)来说,框架是 CefSharp。
CefSharp源码主页:https://github.com/cefsharp/CefSharp
NET (WPF and Windows Forms) bindings for the Chromium Embedded Framework
比如 在Winform界面嵌入浏览器,类似于webBrowser控件
visual studio 2019中新建windows窗体应用程序CefSharpDemo
第一步:右键 解决方案----》属性:配置平台属性为x64.【配置管理器中增加 X64平台,选择x64平台】
第二步:
右键项目CefSharpDemo---->属性-----》生成,设置为X64平台
需要下载NGGet程序包cefSharp,
右键项目CefSharpDemo--->管理NGGet程序包:
---->先安装cef.redis,点击安装
---->再安装cefSharp.winforms 点击安装
第三步:
为窗体Form1增加控件Button:
控件ChromiumWebBrowser 也是继承于System.Windows.Forms.Control类的;
为测试窗体Form1.cs主要代码如下:
using System;
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;
namespace CefSharpDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
WebBrowser webBrowser = new WebBrowser();
private void Form1_Load(object sender, EventArgs e)
{
webBrowser.Url = new Uri("https://www.baidu.com");
webBrowser.Dock = DockStyle.Fill;
this.Controls.Add(webBrowser);
}
private void button1_Click(object sender, EventArgs e)
{
this.Controls.Remove(webBrowser);
string url = "https://www.qq.com";
ChromiumWebBrowser webview = new ChromiumWebBrowser(url);
webview.Dock = DockStyle.Fill;
this.Controls.Add(webview);
}
}
}
运行测试如下:
点击按钮【使用Cef控件】