using CefSharp;
using CefSharp.WinForms;
using System;
using System.Drawing;
using System.Windows.Forms;
namespace myProject
public partial class Form1 : Form
public ChromiumWebBrowser browser;
private string myUrl = "https://www.google.com/";
public Form1()
InitializeComponent();
InitializeChromium();
browser.MouseDown += ChromeBrowser_MouseDown;
public void InitializeChromium()
browser = new ChromiumWebBrowser(myUrl);
browser.Location = new Point(26, 59);
browser.Size = new Size(988,566);
browser.Dock = DockStyle.None;
this.Controls.Add(browser);
private void ChromeBrowser_MouseDown(object sender, MouseEventArgs e)
//This handler never gets fired!!!
if (e.Button == MouseButtons.Left)
//get value from html element
private void button1_Click(object sender, EventArgs e)
browser.Load(addressBar.Text);
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
Cef.Shutdown();
}
public void InitializeChromium()
browser = new ChromiumWebBrowser(myUrl);
browser.Location = new Point(26, 59);
browser.Size = new Size(988,566);
browser.Dock = DockStyle.None;
this.Controls.Add(browser);
browser.JavascriptMessageReceived += Browser_JavascriptMessageReceived;
browser.FrameLoadEnd += Browser_FrameLoadEnd;
}
实现事件处理程序
现在剩下的工作就是通过添加以下代码来实现这些事件处理程序:
private void Browser_FrameLoadEnd(object sender, FrameLoadEndEventArgs e)
if (e.Frame.IsMain)
browser.ExecuteScriptAsync(@"
document.addEventListener('click', function(e) {
var parent = e.target.parentElement;
// run some validation with if(){..}
// some more javascript
CefSharp.PostMessage(parent.outerHTML);
}, false);
private void Browser_JavascriptMessageReceived(object sender, JavascriptMessageReceivedEventArgs e)
if (e.Message != null)
// Extract data from e.Message.toString() and use delegates/callbacks/Invokes
// to reference the main UI thread for updating the necessary controls.
}
using CefSharp;
using CefSharp.WinForms;
using System;
using System.Drawing;
using System.Windows.Forms;
namespace myProject
public partial class Form1 : Form
public ChromiumWebBrowser browser;
private string myUrl = "https://www.google.com/";
public Form1()
InitializeComponent();
InitializeChromium();
public void InitializeChromium()
browser = new ChromiumWebBrowser(myUrl);
browser.Location = new Point(26, 59);
browser.Size = new Size(988,566);
browser.Dock = DockStyle.None;
this.Controls.Add(browser);
browser.JavascriptMessageReceived += Browser_JavascriptMessageReceived;
browser.FrameLoadEnd += Browser_FrameLoadEnd;
private void Browser_FrameLoadEnd(object sender, FrameLoadEndEventArgs e)
if (e.Frame.IsMain)
browser.ExecuteScriptAsync(@"
document.addEventListener('click', function(e) {
var parent = e.target.parentElement;
// run some validation with if(){..}
// some more javascript
CefSharp.PostMessage(parent.outerHTML);
}, false);
private void Browser_JavascriptMessageReceived(object sender, JavascriptMessageReceivedEventArgs e)
if (e.Message != null)
// Extract data from e.Message.toString() and use delegates/callbacks/Invokes
// to reference the main UI thread for updating the necessary controls.
private void button1_Click(object sender, EventArgs e)
browser.Load(addressBar.Text);