Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 304
Nigel Avatar asked Oct 18 '12 15:10

Nigel


2 Answers

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.

like image 162
Andy Avatar answered Oct 12 '22 22:10

Andy


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.

like image 21
Dmitry Azaraev Avatar answered Oct 12 '22 21:10

Dmitry Azaraev