CefSharp框架的C#使用

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控件】

 

 

 


          

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

斯内科

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值