WinForm -- CEF的使用和一些坑处理
https://blog.csdn.net/coolbeliever/article/details/109507780
CEF的引用
1. 通过NuGet包管理器安装cef相应的包
2. 使用
2.1 初始化
-
static class Program
-
{
-
/// <summary>
-
/// 应用程序的主入口点。
-
/// </summary>
-
[
-
static void Main()
-
{
-
// 基本设置
-
var settings = new CefSettings()
-
{
-
//By default CefSharp will use an in-memory cache, you need to specify a Cache Folder to persist data
-
//CachePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "CefSharp\\Cache")
-
};
-
// 设置是否使用GPU
-
settings.CefCommandLineArgs.Add("disable-gpu", "1");
-
// 设置是否使用代理服务
-
settings.CefCommandLineArgs.Add("no-proxy-server", "1");
-
// 设置是否启动js交互,假如需要原生与js方法互调,则需要设置为true
-
CefSharpSettings.LegacyJavascriptBindingEnabled = true;
-
// 初始化
-
Cef.Initialize(settings, performDependencyCheck: true, browserProcessHandler: null);
-
-
Application.EnableVisualStyles();
-
Application.SetCompatibleTextRenderingDefault(false);
-
Application.Run(new SplashForm());
-
}
-
}
2.2 Form显示页面
-
public partial class BrowserForm: Form
-
{
-
private ChromiumWebBrowser webBrowser;
-
-
public BrowserForm()
-
{
-
InitializeComponent();
-
}
-
-
private void Form_Load(object sender, EventArgs e)
-
{
-
string path = "www.baidu.com";
-
webBrowser = new ChromiumWebBrowser(path)
-
{
-
Dock = DockStyle.Fill,
-
MenuHandler = new MenuHandler()
-
};
-
this.Controls.Add(webBrowser);
-
}
-
}
启动程序,百度首页就会被加载出来,一个简单的浏览器就这样实现了
3. cef的复杂使用
-
public partial class BrowserForm: Form
-
{
-
private ChromiumWebBrowser webBrowser;
-
-
public BrowserForm()
-
{
-
InitializeComponent();
-
}
-
-
private void Form_Load(object sender, EventArgs e)
-
{
-
string path = "file:///WebRes/view/answerList.html";
-
webBrowser = new ChromiumWebBrowser(path)
-
{
-
Dock = DockStyle.Fill,
-
MenuHandler = new MenuHandler()
-
};
-
this.Controls.Add(webBrowser);
-
-
webBrowser.FrameLoadEnd += WebBrowser_FrameLoadEnd;
-
webBrowser.RegisterAsyncJsObject("answerListForm", new JsEvent(this));
-
}
-
-
private void WebBrowser_FrameLoadEnd(object sender, FrameLoadEndEventArgs e)
-
{
-
if (e.Frame.IsMain)
-
{
-
string answerMessage = JsonHelper.SerializeObject(Config.teacherSetAnswer);
-
webBrowser.ExecuteScriptAsync("getTeacherSetAnswer(" + answerMessage + ")");
-
}
-
}
-
/*
-
* ============================================================================================
-
* JS与调用c#
-
*/
-
public class JsEvent
-
{
-
AnswerListForm form;
-
public JsEvent(AnswerListForm form)
-
{
-
this.form = form;
-
}
-
-
public void test(string message)
-
{
-
if (form != null)
-
{
-
form.test(message);
-
}
-
}
-
}
-
}
3.1 加载本地html
加载本地文件格式:file:/// + 相对路径
3.2 去除鼠标右键事件
-
class MenuHandler : CefSharp.IContextMenuHandler
-
{
-
void CefSharp.IContextMenuHandler.OnBeforeContextMenu(CefSharp.IWebBrowser browserControl, CefSharp.IBrowser browser, CefSharp.IFrame frame, CefSharp.IContextMenuParams parameters, CefSharp.IMenuModel model)
-
{
-
model.Clear();
-
}
-
-
bool CefSharp.IContextMenuHandler.OnContextMenuCommand(CefSharp.IWebBrowser browserControl, CefSharp.IBrowser browser, CefSharp.IFrame frame, CefSharp.IContextMenuParams parameters, CefSharp.CefMenuCommand commandId, CefSharp.CefEventFlags eventFlags)
-
{
-
//throw new NotImplementedException();
-
return false;
-
}
-
-
void CefSharp.IContextMenuHandler.OnContextMenuDismissed(CefSharp.IWebBrowser browserControl, CefSharp.IBrowser browser, CefSharp.IFrame frame)
-
{
-
//throw new NotImplementedException();
-
}
-
-
bool CefSharp.IContextMenuHandler.RunContextMenu(CefSharp.IWebBrowser browserControl, CefSharp.IBrowser browser, CefSharp.IFrame frame, CefSharp.IContextMenuParams parameters, CefSharp.IMenuModel model, CefSharp.IRunContextMenuCallback callback)
-
{
-
return false;
-
}
-
}
在初始化浏览器时,添加
-
webBrowser = new ChromiumWebBrowser(path)
-
{
-
MenuHandler = new MenuHandler()
-
};
3.3 c#调用js方法时机
-
webBrowser.FrameLoadEnd += WebBrowser_FrameLoadEnd;
-
private void WebBrowser_FrameLoadEnd(object sender, FrameLoadEndEventArgs e)
-
{
-
if (e.Frame.IsMain)
-
{
-
string answerMessage = JsonHelper.SerializeObject(Config.teacherSetAnswer);
-
webBrowser.ExecuteScriptAsync("getTeacherSetAnswer(" + answerMessage + ")");
-
}
-
}
监听WebBrowser_FrameLoadEnd事件,在其回调后才能调用js
3.4 js调用c#方法
-
webBrowser.RegisterAsyncJsObject("answerListForm", new JsEvent(this));
-
-
public class JsEvent
-
{
-
AnswerListForm form;
-
public JsEvent(AnswerListForm form)
-
{
-
this.form = form;
-
}
-
-
public void test(string message)
-
{
-
if (form != null)
-
{
-
form.test(message);
-
}
-
}
-
}
注入JsEvent对象,并添加注入标识符(answerListForm),在js的调用方式是:answerListForm.test(“king”);
4 CEF的一些坑
4.1 平台
目前CefSharp还不支持AnyCPU(貌似新版本开始支持:https://github.com/cefsharp/CefSharp/issues/1714),需要将你的项目设置为x86或x64
4.2 运行环境需求
- Microsoft .Net 4.5.2 or greater(4.5.2或以上)
- Microsoft Visual C++ Redistributable Package (either x86 or x64 depending on your application)(vc++运行库)
4.3 嵌套了ChromiumWebBrowser 的form关闭
建议使用webBrowser.GetBrowser().CloseBrowser(true);方式来关闭,而不直接调用form.Close()