Use of Cefsharp (write a browser with a few lines of code)

Introduction to installing
Cefsharp
CEF, full name Chromium Embedded Framework, is an open source Web Browser control based on the Google Chromium project. Its main purpose is to embed third-party applications to implement browser-related functions. For example, the compatibility mode of the domestic 360 secure browser is the core of IE, and the speed mode is the Chromium core; the recently launched Microsoft Edge is also based on the Chromium open source project.

CefSharp is the C# version of Cef, allowing Chromium browsers to be embedded in WinForms
 

installation steps

  • Step 1: Right-click the project, click  Manage NuGet Packages  , enter the Cefsharp keyword search, and select  CefSharp.WinForms  to install

After installation, you can see the following information in packages.config

  • Step 3: Write code and initialize the browser
using CefSharp;
using CefSharp.WinForms;
using System.Windows.Forms;
 
namespace 我的浏览器
{
    public partial class Form1 : Form
    {
        // 浏览器对象
        public ChromiumWebBrowser chromeBrowser;
        public Form1()
        {
            InitializeComponent();
         
        }
 
        public void InitCef()
        {
            //参数设置
            CefSettings settings = new CefSettings();
          //  settings.Locale = "zh-CN";
            // settings.CefCommandLineArgs.Add("disable-gpu", "1");//去掉gpu,否则chrome显示有问题
            Cef.Initialize(settings);
            //创建实例
            chromeBrowser = new ChromiumWebBrowser("https://www.baidu.com");
            // 将浏览器放入容器中
            this.Controls.Add(chromeBrowser);
            chromeBrowser.Dock = DockStyle.Fill;
        }
 
        private void Form1_Load(object sender, System.EventArgs e)
        {
            // 初始化浏览器
            InitCef();
        }
    }
}

Finally start the program and successfully access Baidu

Guess you like

Origin blog.csdn.net/zgscwxd/article/details/132983418