如何在 Winforms 应用程序中使用 CefSharp(chromium embedded framework c#)(入门篇)

目录

1:chromium embedded framework c#简介

2:CefSharp的安装

3:CefSharp实现代码片段

        1.实现打开现有网址,在 Form1.cs 中添加如下代码;

        2.使用CEF(作为用户界面)

​4.总结


1:chromium embedded framework c#简介

 CefSharp,简单来说就是一款.Net编写的浏览器包,方便你在Winform和WPF中内嵌的Chrome浏览器组件。它支持HTML5。
CefSharp的功能比较复杂,以下只介绍一些我觉得比较重要的并且目前经常用的功能。

CefSharp 是将功能齐全的符合标准的 Web 浏览器嵌入到 C# 或 VB.NET 应用程序中的最简单方法。CefSharp 具有用于 WinForms 和 WPF 应用程序的浏览器控件,以及用于自动化项目的无头(屏幕外)版本。CefSharp 基于 Chromium Embedded Framework,即 Google Chrome 的开源版本。

您可以将 CefSharp用作浏览器组件,而不是依赖于您的用户在 Windows 上安装的 Internet Explorer 版本,也可以用作应用程序的预定义用户界面。是的,您可以在 winforms c# 应用程序中使用 HTML 控件(按钮、输入),并根据需要使用 CSS 进行自定义(引导等)

2:CefSharp的安装

CefSharp的安装过程如下:

  1. 打开Visual Stduio,新建一个Windows窗体应用(.NET Framework)
  2. 点击项目鼠标右键,再点击"管理NuGet程序包";
  3. 搜索“CefSharp.WinForms”进行安装,因为创建的项目是winform项目所以选择CefSharp.WinForms,安装的时候需要选择版本,我选择的版本是 --75.1.143;
  4. 早期版本的CefSharp不能在“Any CPU”平台上运行,需要配置。打开“配置管理器”; 
  5. 新建“x86”和“x64”两个平台。从理论上来说使用x86或者x64平台都行,但由于之后要使用编译好的支持h264的x86内核,因此此处选择x86平台;
  6. 最后,修改App.config文件,它在 Visual Studio 中项目的解决方案资源管理器中可见,并在配置标签中添加以下标签:
<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <probing privatePath="x86"/>
    </assemblyBinding>
</runtime>

 然后,您的配置文件将如下所示:

 3:CefSharp实现代码片段

        1.实现打开现有网址,在 Form1.cs 中添加如下代码;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CefSharp;
using CefSharp.WinForms;

namespace Cefsharp1
{
    public partial class Form1 : Form
    {
        public ChromiumWebBrowser chromeBrowser;

        public Form1()
        {
            InitializeComponent();
            // Start the browser after initialize global component
            InitializeChromium();
        }
        public void InitializeChromium()
        {
            CefSettings settings = new CefSettings();
            // Initialize cef with the provided settings
            Cef.Initialize(settings);
            // Create a browser component
            chromeBrowser = new ChromiumWebBrowser("https://www.baidu.com");
            // Add it to the form and fill it to the form window.
            this.Controls.Add(chromeBrowser);
            chromeBrowser.Dock = DockStyle.Fill;
        }
        private void Form1_Load_1(object sender, EventArgs e)
        {
            //chromeBrowser.ShowDevTools();
        }
    }
}

 运行结果如下:

form1页面上便可以打开百度的地址了; 

        2.使用CEF(作为用户界面)

上面已经将CefSharp 实现为浏览器组件。但你可以做的不止这些,现在我们将使用本地文件实现一个纯 HTML、Javascript 和 CSS(引导)UI,并用它来操作系统的东西。

我们需要在 Visual Studio 项目中包含我们的 HTML 资产。您可以直接从 Visual Studio 或 Windows 资源管理器创建它们,但通过 Visual Studio 更容易,因为该文件夹会自动添加到项目资源中。

在这种情况下,我们将使用一个简单的 Bootstrap 接口 (包括 bootstrap 和 jQuery),它包含在以下目录中:

重要提示:打开本地文件的时候需要对所有文件进行一下操作

  • 选择html,js,css文件夹内的所有资源。
  • 在底部将“复制到输出目录"值设置为"始终复制"。并且将html文件的生成操作改成嵌入的资源

 

 做完上述操作之后再项目中新建一个类,代码如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CefSharp;
using CefSharp.WinForms;
using System.Diagnostics;

namespace Cefsharp1
{
    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);
        }
    }
}

这个类会在 Javascript 中公开,后面你可以看到非常基础,展示了 chrome 浏览器的开发工具,并启动了一个 cmd.exe 进程。

现在我们的InitializeChromium函数应该如下所示:form1代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CefSharp;
using CefSharp.WinForms;

namespace Cefsharp1
{
    public partial class Form1 : Form
    {
        public ChromiumWebBrowser chromeBrowser;

        public Form1()
        {
            InitializeComponent();
            // Start the browser after initialize global component
            InitializeChromium();
            // Register an object in javascript named "cefCustomObject" with function of the CefCustomObject class :3

        }

        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:\Users\SDkCarlos\Desktop\afolder\index.html";

            //String page = string.Format(@"{0}\html-resources\html\index.html", Application.StartupPath);
            String page = @"D:\c\GZH\Cefsharp1\html-resources\html\index.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;
            CefSharpSettings.LegacyJavascriptBindingEnabled = true;
            chromeBrowser.RegisterJsObject("cefCustomObject", new CefCustomObject(chromeBrowser, this));
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Cef.Shutdown();
        }

        private void Form1_Load_1(object sender, EventArgs e)
        {
            //chromeBrowser.ShowDevTools();
        }
    }
}

启动时会访问:D:\c\GZH\Cefsharp1\html-resources\html\index.html

index.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.0">
    <title>Document</title>
</head>
<body>
    <h1>
        cef启动了本地文件:
        启动时会访问:D:\c\GZH\Cefsharp1\html-resources\html\index.html
    </h1>
    <button class="btn btn-info" onclick="cefCustomObject.showDevTools();">Open Chrome Dev Tools</button>
    <button class="btn btn-primary" onclick="cefCustomObject.opencmd();">Open cmd.exe</button>
</body>
</html>

 运行结果如下:

因为不是网页所以调试工具用这种方式打开:

也可以访问本地cmd窗口:

4.总结

 近期因为工作需要简单尝试了一下CefSharp,上面只是初步的使用,具体还需要进一步研究,还有发现最新版本的CefSharp使用上述方式打开本地文件时会报错,还需要进一步探索

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
cefsharp.winforms.netcore 114.2.100 是一个适用于 Windows Form 应用程序Chromium Embedded Framework(CEF)的 .NET Core 版本。在该版本CEFSharp 提供了对视频播放的支持。 CEFSharp 是一个基于 CEF 的 .NET 接口封装,它允许开发人员在自己的 .NET 应用程序嵌入一个完整的 Chromium 浏览器。通过使用 CEFSharp开发人员可以轻松地在 Windows Form 应用程序实现强大的浏览器功能,包括支持 HTML5 标准的视频播放。 使用 CEFSharp 进行视频播放非常简单。首先,需要确保你的应用程序引用了正确的版本,并且安装了相关的依赖项。然后,在你的 Windows Form 页面,你可以使用 CefSharp.WinForms.ChromiumWebBrowser 控件来嵌入浏览器。 在浏览器加载网页时,如果网页包含视频元素,CEFSharp 将会自动处理视频播放。你可以使用嵌入好的浏览器控件的方法和事件来控制视频播放,例如播放、暂停、停止等。 在 CEFSharp ,也可以使用自定义的 JavaScript 代码来操作视频播放。你可以通过调用 JavaScript 代码在 C# 控制视频的播放状态。要实现自定义的视频控制,你可以使用 CefSharp.WinForms.IJavascriptCallback 接口来进行通信。 总结来说,cefsharp.winforms.netcore 114.2.100 提供了对视频播放的支持。开发人员可以使用 CEFSharp 在他们的 .NET Core Windows Form 应用程序嵌入一个完整的 Chromium 浏览器并实现强大的视频播放功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值