using System; using System.IO; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using CefSharp; using CefSharp.WinForms; namespace TestWebview { public class CefCustomObject { // Declare a local instance of chromium and the main form in order to execute things from here in the main thread private static ChromiumWebBrowser _instanceBrowser = null; // The form class needs to be changed according to yours private static Form1 _instanceMainForm = null; public CefCustomObject(ChromiumWebBrowser originalBrowser, Form1 mainForm) { _instanceBrowser = originalBrowser; _instanceMainForm = mainForm; } /// /// 窗口加载完毕时需要出发的全局JS对象; /// public void OnFrameLoadEnd(object sender, FrameLoadEndEventArgs e) { if (e.Frame.IsMain) { _instanceBrowser.ExecuteScriptAsync(@" document.body.onmouseup = function() { bound.onSelected(window.getSelection().toString()); } "); } } /// /// 显示一个弹出对话框,前端JS调用的方法 /// /// 传入参数 public void showAlertMsg(string msg) { MessageBox.Show("Msg From Web JS: [" + msg + "]"); } } }