You can use this on Win Forms and on WPF!
Let's do it!
Getting Started
Open a new project on Visual Studio and install CefSharp using NuGet Packages. You will need .NETFramework 4.5.2 or higher!
After adding these Nuget packages re-open the visual studio, otherwise it will give you some errors while the project is building. Then you will need to change the project platform to x64 or x86. With the newer versions of CefSharp you can work with AnyCPU, but it won't just magically work out of the box like x86 and x64 will. Goto this link to get this issue fixed!
2) Now you will need to import Cef to your code.
If the browser is loaded with a web page, we should use "Load" method pass a new URL to Cef. Otherwise you will have to go from the beginning - initialize and add the Cef browser to the panel/form
2) Now you will need to import Cef to your code.
using CefSharp;
using CefSharp.WinForms;
Creating a UI & Using Cef as a browser
On my sample source code I have added panels - one for the controllers and other one for Cef to load. Also there are two text boxes and few buttons.
public void InitializeBrowser()
{
if (browser != null)
{
browser.Load(txtUrl.Text.Trim());
}
else
{
Cef.Initialize(new CefSettings());
browser = new ChromiumWebBrowser(txtUrl.Text.Trim());
pnlBrowser.Controls.Add(browser);
browser.Dock = DockStyle.Fill;
browser.LoadingStateChanged += Browser_LoadingStateChanged;
}
}
{
if (browser != null)
{
browser.Load(txtUrl.Text.Trim());
}
else
{
Cef.Initialize(new CefSettings());
browser = new ChromiumWebBrowser(txtUrl.Text.Trim());
pnlBrowser.Controls.Add(browser);
browser.Dock = DockStyle.Fill;
browser.LoadingStateChanged += Browser_LoadingStateChanged;
}
}
If the browser is loaded with a web page, we should use "Load" method pass a new URL to Cef. Otherwise you will have to go from the beginning - initialize and add the Cef browser to the panel/form
Executing Scripts
After the application finishes loading bing.com, you can type a search text on second text box and click "send text". Then you can see the bing search text box is filled with your search text.
You can use ExecuteScriptAsync to execute scripts.
browser.ExecuteScriptAsync("document.querySelector('#sb_form_q').value = '" + txtSearchText.Text + "';");
Then click "Search" button to submit the search query.!
browser.ExecuteScriptAsync("document.querySelector('input[type=submit]').click();");
Main Form Coding
using CefSharp;
using CefSharp.WinForms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CefSharp_
{
public partial class Mainf : Form
{
public ChromiumWebBrowser browser;
public Mainf()
{
InitializeComponent();
}
private void Mainf_Load(object sender, EventArgs e)
{
}
public void InitializeBrowser()
{
if (browser != null)
{
browser.Load(txtUrl.Text.Trim());
}
else
{
Cef.Initialize(new CefSettings());
browser = new ChromiumWebBrowser(txtUrl.Text.Trim());
pnlBrowser.Controls.Add(browser);
browser.Dock = DockStyle.Fill;
browser.LoadingStateChanged += Browser_LoadingStateChanged;
}
}
private void Browser_LoadingStateChanged(object sender, LoadingStateChangedEventArgs e)
{
if (!e.IsLoading)
{
lblStatus.Invoke(new Action(() => lblStatus.Text = "Done!"));
}
else
{
lblStatus.Invoke(new Action(() => lblStatus.Text = "Loading..!"));
}
}
private void btnGo_Click(object sender, EventArgs e)
{
if(txtUrl.Text != "")
{
InitializeBrowser();
}
}
private void btnSendTxt_Click(object sender, EventArgs e)
{
browser.ExecuteScriptAsync("document.querySelector('#sb_form_q').value = '" + txtSearchText.Text + "';");
}
private void btnSearch_Click(object sender, EventArgs e)
{
browser.ExecuteScriptAsync("document.querySelector('input[type=submit]').click();");
}
private void Mainf_FormClosing(object sender, FormClosingEventArgs e)
{
Cef.Shutdown();
}
}
}
using CefSharp.WinForms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CefSharp_
{
public partial class Mainf : Form
{
public ChromiumWebBrowser browser;
public Mainf()
{
InitializeComponent();
}
private void Mainf_Load(object sender, EventArgs e)
{
}
public void InitializeBrowser()
{
if (browser != null)
{
browser.Load(txtUrl.Text.Trim());
}
else
{
Cef.Initialize(new CefSettings());
browser = new ChromiumWebBrowser(txtUrl.Text.Trim());
pnlBrowser.Controls.Add(browser);
browser.Dock = DockStyle.Fill;
browser.LoadingStateChanged += Browser_LoadingStateChanged;
}
}
private void Browser_LoadingStateChanged(object sender, LoadingStateChangedEventArgs e)
{
if (!e.IsLoading)
{
lblStatus.Invoke(new Action(() => lblStatus.Text = "Done!"));
}
else
{
lblStatus.Invoke(new Action(() => lblStatus.Text = "Loading..!"));
}
}
private void btnGo_Click(object sender, EventArgs e)
{
if(txtUrl.Text != "")
{
InitializeBrowser();
}
}
private void btnSendTxt_Click(object sender, EventArgs e)
{
browser.ExecuteScriptAsync("document.querySelector('#sb_form_q').value = '" + txtSearchText.Text + "';");
}
private void btnSearch_Click(object sender, EventArgs e)
{
browser.ExecuteScriptAsync("document.querySelector('input[type=submit]').click();");
}
private void Mainf_FormClosing(object sender, FormClosingEventArgs e)
{
Cef.Shutdown();
}
}
}
Use this link to download the source code 😋 (344 MB with all references!)
For more detail, goto CefSharp official web site
cant input on facebook comment
ReplyDelete