文章目录
使用键盘F5刷新、F12打开控制台浏览器默认下载弹窗禁用GPU配置跨域问题
使用键盘F5刷新、F12打开控制台
public class CEFKeyBoardHander : IKeyboardHandler{ public bool OnKeyEvent(IWebBrowser browserControl, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey) { if (type == KeyType.KeyUp && Enum.IsDefined(typeof(Keys), windowsKeyCode)) { var key = (Keys)windowsKeyCode; switch (key) { case Keys.F12: browser.ShowDevTools(); break; case Keys.F5: if (modifiers == CefEventFlags.ControlDown) { //MessageBox.Show("ctrl+f5"); browser.Reload(true); //强制忽略缓存 } else { //MessageBox.Show("f5"); browser.Reload(); } break; } } return false; } public bool OnPreKeyEvent(IWebBrowser browserControl, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey, ref bool isKeyboardShortcut) { return false; }}
WebBrowser = new ChromiumWebBrowser();this.Controls.Add(WebBrowser);WebBrowser.KeyboardHandler = new CEFKeyBoardHander();
浏览器默认下载弹窗
public class CefDownloadHandler : IDownloadHandler{ public event EventHandler<DownloadItem> OnBeforeDownloadFired; public event EventHandler<DownloadItem> OnDownloadUpdatedFired; public bool CanDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, string url, string requestMethod) { //可以支持全部,也可以根据配置去自定义支持的文件类型 //return "xls|xlsx".Split('|').ToList().Contains(url.Substring(url.LastIndexOf(".") + 1).ToLower()); return true; } public void OnBeforeDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback) { OnBeforeDownloadFired?.Invoke(this, downloadItem); if (!callback.IsDisposed) { using (callback) { callback.Continue(downloadItem.SuggestedFileName, showDialog: true); } } } public void OnDownloadUpdated(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback) { OnDownloadUpdatedFired?.Invoke(this, downloadItem); }}
WebBrowser = new ChromiumWebBrowser();WebBrowser.DownloadHandler = new CefDownloadHandler();
禁用GPU配置
var setting = new CefSettings();setting.CefCommandLineArgs.Add("disable-gpu", "1")
跨域问题
setting.CefCommandLineArgs.Add("--disable-web-security", "");
待续未完……