Are there any samples of using CefGlue or CefSharp in a windows forms application with minimum setup?
Asked Answered
P

2

12

I am (still) using Visual Studio 2005 and wanting to embed a webkit browser within a c# winforms application, preferably as a winforms control.

I am looking for a simple example of either CefGlue or CefSharp to get started with, along with the minimum necessary dlls. I cannot make any sense of the CefSharp sample on GitHub.

Prolegomenon answered 18/10, 2012 at 15:13 Comment(0)
E
14

It is pretty easy however very sadly documented.

To get it working, I made a new Forms application and added a toolstripContainer to my form. Also added references to CefSharp.dll and CefSharp.WinForms.dll to my project.

This is my code for my class:

public partial class frmBrowser : Form, IRequestHandler
{
    private readonly WebView web_view;

    public frmBrowser()
    {
        InitializeComponent();
        web_view = new WebView("http://stackoverflow.com", new BrowserSettings());
        web_view.Dock = DockStyle.Fill; 
        web_view.RequestHandler = this;
        tsContainer.ContentPanel.Controls.Add(web_view);
    }

    #region IRequestHandler Members

    bool IRequestHandler.OnBeforeBrowse(IWebBrowser browser, IRequest request,
                               NavigationType naigationvType, bool isRedirect)
    {
        System.Diagnostics.Debug.WriteLine("OnBeforeBrowse");
        return false;
    }

    bool IRequestHandler.OnBeforeResourceLoad(IWebBrowser browser,
                                     IRequestResponse requestResponse)
    {
        System.Diagnostics.Debug.WriteLine("OnBeforeResourceLoad");
        IRequest request = requestResponse.Request;

        if (request.Url.EndsWith("header.png"))
        {
            MemoryStream stream = new System.IO.MemoryStream();

            FileStream file = new FileStream(@"C:\tmp\header.png", FileMode.Open, FileAccess.Read, FileShare.Read);
            byte[] bytes = new byte[file.Length];
            file.Read(bytes, 0, (int)file.Length);
            stream.Write(bytes, 0, (int)file.Length);
            file.Close();

            requestResponse.RespondWith(stream, "image/png");
        }

        return false;
    }

    void IRequestHandler.OnResourceResponse(IWebBrowser browser, string url,
                                   int status, string statusText,
                                   string mimeType, WebHeaderCollection headers)
    {
        System.Diagnostics.Debug.WriteLine("OnResourceResponse");
    }

    #endregion
}

The region with the request handlers is optional, thats for when you want to influence the calls. In my example I rerouted the call to the header image to an image on my c drive.

That's it what you need for code. You also need to have the following files addes to the folder of your executable:

  • avcodec-54.dll
  • avformat-54.dll
  • avutil-51.dll
  • chrome.pak
  • icudt.dll
  • libcef.dll
  • libEGL.dll
  • libGLESv2.dll
  • the locales folder

Some of these files are optional tho, based upon what you want to do with them, but you can google that.

Elston answered 15/11, 2012 at 11:1 Comment(4)
Thank you for this! Also, if you don't have DirectX installed you'll get a "d3dx9_43.dll missing" error if you have libEGL.dll and/or libGLESv2.dll in your runtime exe dir, they will attempt to load d3dx9_43.dll to enable WebGL content. Removing libEGL.dll and libGLESv2.dll fixes this.Firewood
I also had to add <startup useLegacyV2RuntimeActivationPolicy="true"><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client"/></startup> when targeting .Net 4 client profile to the app-config to get it to work with the pre-compiled CefSharp binaries. This fixed a mixed mode assembly error.Priapic
Quietly blows up on web_view = new WebView("http://stackoverflow.com", new BrowserSettings()); for me, no errors in a try... catch.Bogard
Hi, any small tutorial on how to add a cache as well? I was trying awesomium and it was as easy as creating a websession and saving it to disk. Due to the lack of tutorials, do you know how to do the same in cefsharp? Thank you!Ewers
S
5

CefGlue (outdated version for CEF1) and Xilium.CefGlue (CEF3) already contains demo applications. Xilium.CefGlue contains two demo applications - first (called CefGlue.Demo works on windows under winforms and using GtkSharp on linux), and CefGlue.Client - also very simple winforms only demo. So CefGlue.Client already have very simple winforms control.

UPD: Xilium.CefGlue assembly targeted to .NET 2.0. Xilium.CefGlue.Client targeted to .NET 3.5 client profile. But in general it is doesn't use any 3.5-specific and can be fixed. But i'm recommend use minimum .NET 4.0 runtime, due it have much better GC. It no have any sense install 2.0 instead of 4.0 at production.

Saltire answered 26/10, 2012 at 16:23 Comment(2)
When running demo project in Xilium.CefGlue in VS2013, I am getting an error: "A project with an Output Type of Class Library cannot be started directly". I have set the startup project to be demo. Do you know what I should change to run the demo project?Irisirisa
Use CefGlue.Demo.WinForms. Also use 2357 branch, because 2454 is not ready (but will be updated soon).Saltire

© 2022 - 2024 — McMap. All rights reserved.