[Winfrom]Cefsharp配置与初始化

2017-07-15 10:17:25来源:http://www.cnblogs.com/wolf-sun/p/7168952.html作者:博客园_Wolfy人点击

摘要

在做客户端程序的时候,本来打算使用wpf的,但在内嵌cefsharp的时候,发现输入法有问题,所以使用了winform作为cefsharp的容器。


系列文章


CefSharp 在同一窗口打开链接的方法



[Winform]CefSharp ——js调用c#方法



cefsharp wpf 中文输入问题解决方法


[Winfrom]Cefsharp配置与初始化


初始化与配置
/// <summary>
/// 浏览器配置
/// </summary>
private CefSharp.CefSettings _settings;
/// <summary>
/// 浏览器对象
/// </summary>
CefSharp.WinForms.ChromiumWebBrowser _webView;
/// <summary>
/// 加载页面
/// </summary>
/// <param name="url"></param>
private void LoadPage(string pageName)
{
string url = string.Format(StaticParameter.htmlPathFormat, StaticParameter.ExeDir, pageName);
try
{
if (_webView == null)
{
_settings = new CefSharp.CefSettings();
_settings.UserAgent = StaticParameter.UserAgent;
// Enable WebRTC
_settings.CefCommandLineArgs.Add("enable-media-stream", "1");
//Disable GPU Acceleration
_settings.CefCommandLineArgs.Add("disable-gpu", "1");
// Don't use a proxy server, always make direct connections. Overrides any other proxy server flags that are passed.
// Slightly improves Cef initialize time as it won't attempt to resolve a proxy
_settings.CefCommandLineArgs.Add("no-proxy-server", "1");
_settings.Locale = "zh-CN";
//缓存路径
_settings.CachePath = StaticParameter.ExeDir + "/BrowserCache";
//浏览器引擎的语言
_settings.AcceptLanguageList = "zh-CN,zh;q=0.8";
_settings.LocalesDirPath = StaticParameter.ExeDir + "/localeDir";
//日志文件
_settings.LogFile = StaticParameter.ExeDir + "/LogData";
_settings.PersistSessionCookies = true;
_settings.UserDataPath = StaticParameter.ExeDir + "/userData";
CefSharp.Cef.Initialize(_settings);
_webView = new CefSharp.WinForms.ChromiumWebBrowser(url)
{
BrowserSettings =
{
DefaultEncoding = "UTF-8"
},
MenuHandler = new MenuHandler(),
LifeSpanHandler = new OpenPageSelf(),
Dock = DockStyle.Fill
};
ScriptCallbackManager scriptManager = new ScriptCallbackManager();
scriptManager.MiniWinidowHandler += scriptManager_MiniWinidowHandler;
_webView.LoadError += _webView_LoadError;
_webView.RegisterJsObject("googleBrower", scriptManager,
new CefSharp.BindingOptions { CamelCaseJavascriptNames = false });
this.Controls.Add(_webView);
}
else
{
_webView.Load(url);
}
}
catch (Exception ex)
{
LogInfoData.WriteLog(new LogInfo
{
IsSend = false,
Message = ex.Message,
Op = "loadpage_err"
});
}
}

禁用右键菜单


/// <summary>
/// 禁用右键菜单
/// </summary>
internal class MenuHandler : IContextMenuHandler
{
public void OnBeforeContextMenu(IWebBrowser browserControl, IBrowser browser,
IFrame frame, IContextMenuParams parameters, IMenuModel model)
{
model.Clear();
}
public bool OnContextMenuCommand(IWebBrowser browserControl, IBrowser browser, IFrame frame,
IContextMenuParams parameters, CefMenuCommand commandId, CefEventFlags eventFlags)
{
return false;
}
public void OnContextMenuDismissed(IWebBrowser browserControl, IBrowser browser, IFrame frame)
{
}
public bool RunContextMenu(IWebBrowser browserControl, IBrowser browser,
IFrame frame, IContextMenuParams parameters, IMenuModel model, IRunContextMenuCallback callback)
{
return false;
}
}

加载页面失败


void _webView_LoadError(object sender, CefSharp.LoadErrorEventArgs e)
{
MessageBox.Show("加载页面失败,请退出客户端进行重试");
Quit();
}

关闭cefsharp


CefSharp.Cef.Shutdown();
if (_webView != null)
{
_webView.Dispose();
}

刷新页面


//刷新
_webView.GetBrowser().Reload();
总结

安装cefsharp可以使用nuget进行安装。


最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台