虽然能用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) { } } }
调用方式
using CefSharp;
using CefSharp.WinForms; namespace LawSever { public partial class frmweb : Form { public frmweb() { InitializeComponent(); InitBrowser(); }
public ChromiumWebBrowser browser; public void InitBrowser() {
Cef.Initialize(new CefSettings());
browser = new ChromiumWebBrowser("https://ai.12348.gov.cn/pc/"); browser.LifeSpanHandler = new LifeSpanHandler();//超链接不弹新窗体 this.panel1.Controls.Add(browser);//panel } }
禁用右键
禁用右键的接口是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();
标签:控件,CefSharp,chromiumWebBrowser,IBrowser,右键,IWebBrowser,public,browser From: https://www.cnblogs.com/hailexuexi/p/16791337.html