Windows Forms: Chromium Browser using CefSharp in C#

This post shows you How to make a Chromium Browser using CefSharp (Chromium Embedded Framework) in C# Windows Forms Application.

Creating a Chromium browser using CefSharp in C# involves several steps.

How to embed Chrome browser in a WinForms Application

Here's a basic guide to get you started

Creating a new Windows Forms Application project. Next, you need to install the CefSharp NuGet package into your project.

You can do this via the NuGet Package Manager in Visual Studio by right-clicking on your project, then select Manage NuGet Packages -> Search cefsharp -> Install it, or via the Package Manager Console with the following command

Install-Package CefSharp.WinForms

CefSharp is the the easiest way to embed a full-featured standards-complaint web browser into your C# or VB.NET app. CefSharp has browser controls for WinForms and WPF apps, and a headless (offscreen) version for automation projects too. CefSharp is based on Chromium Embedded Framework, the open source version of Google Chrome.

Open your form designer, then drag and drop Button, TextBox and Panel controls from the Visual Studio toolbox to your winform.

You can design a simple ui chromium web browser as show below.

install cefsharp windows forms c#

Adding the Form_Load event handler to your form that allows you to initialize the cefsharp. Before you can use CefSharp controls, you need to initialize CefSharp in your application. You can do this in your Form_Load event handler or in the Main method of your application.

You can add a ChromiumWebBrowser control to your Panel control by manual or designer.

private void Form1_Load(object sender, EventArgs e)
{
    CefSettings settings = new CefSettings();
    // Initialize CefSharp
    Cef.Initialize(settings);
    txtUrl.Text = "https://foxlearn.com";
    // Create ChromiumWebBrowser instance
    chrome = new ChromiumWebBrowser(txtUrl.Text);
    // Add ChromiumWebBrowser to your form
    this.pContainer.Controls.Add(chrome);
    chrome.Dock = DockStyle.Fill;
    chrome.AddressChanged += Chrome_AddressChanged;
}

Adding the AddressChanged event handler to the ChromiumWebBrowser allows you to change the address.

private void Chrome_AddressChanged(object sender, AddressChangedEventArgs e)
{
    this.Invoke(new MethodInvoker(() =>
    {
        txtUrl.Text = e.Address;
    }));
}

Adding the click event hanlder to the Go, Refresh, Forward and Back buttons as the following c# code.

private void btnGo_Click(object sender, EventArgs e)
{
    chrome.Load(txtUrl.Text);
}

private void btnRefresh_Click(object sender, EventArgs e)
{
    chrome.Refresh();
}

private void btnForward_Click(object sender, EventArgs e)
{
    if (chrome.CanGoForward)
        chrome.Forward();
}

private void btnBack_Click(object sender, EventArgs e)
{
    if (chrome.CanGoBack)
        chrome.Back();
}

And don't forget to call the Shutdown method in the FormClosing event handler. It's important to properly shut down CefSharp when your application exits to release resources.

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

Through the c# example above, you've learned how to install cefsharp in windows forms application, use cefsharp to create a simple chromium web browser in c# .net

VIDEO TUTORIAL