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.
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:
Some of these files are optional tho, based upon what you want to do with them, but you can google that.