虽然能用CefSharp将web应用嵌入到窗体程序,但遇到<a >标签(_blank)会弹出一个窗口,而且在任意位置可以弹出右键菜单,需要解决这个问题才像一个真正的客户端软件。
不弹子窗体
控制弹窗的接口是ILifeSpanHandler,并实现OnBeforePopup方法。如下:
定义LifeSpanHandler类:
using CefSharp; using CefSharp.WinForms; namespace CefTest { internal class LifeSpanHandler : ILifeSpanHandler { //弹出前触发的事件 public bool OnBeforePopup(IWebBrowser webBrowser, IBrowser browser, IFrame frame, string targetUrl, string targetFrameName, WindowOpenDisposition targetDisposition, bool userGesture, IPopupFeatures popupFeatures, IWindowInfo windowInfo, IBrowserSettings browserSettings, ref bool noJavascriptAccess, out IWebBrowser newBrowser) { //使用源窗口打开链接,取消创建新窗口 newBrowser = null; var chromiumWebBrowser = (ChromiumWebBrowser)webBrowser; chromiumWebBrowser.Load(targetUrl); return true; } public void OnAfterCreated(IWebBrowser chromiumWebBrowser, IBrowser browser) { } public bool DoClose(IWebBrowser chromiumWebBrowser, IBrowser browser) { return true; } public void OnBeforeClose(IWebBrowser chromiumWebBrowser, IBrowser browser) { } } }
调用方式:
ChromeBrowser.LifeSpanHandler = new LifeSpanHandler();
禁用右键
禁用右键的接口是IContextMenuHandler,并实现OnBeforeContextMenu 方法。如下:
定义MenuHandler类:
using CefSharp; namespace CefTest { internal class MenuHandler : IContextMenuHandler { public void OnBeforeContextMenu(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model) { model.Clear(); } public bool OnContextMenuCommand(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters, CefMenuCommand commandId, CefEventFlags eventFlags) { return false; } public void OnContextMenuDismissed(IWebBrowser webBrowser, IBrowser browser, IFrame frame) { } public bool RunContextMenu(IWebBrowser webBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model, IRunContextMenuCallback callback) { return false; } } }
调用方式:
ChromeBrowser.MenuHandler = new MenuHandler();
来源:https://www.cnblogs.com/liweis/p/12315654.html