public class LifespanHandler: ILifeSpanHandler
public event Action<string> popup_request;
//get url popup this.popup_request?.Invoke(targetUrl); //stop open popup newBrowser = null;
using CefSharp; using CefSharp.WinForms; namespace popup_cefsharp { public class LifespanHandler: ILifeSpanHandler { //event that receive url popup public event Action<string> popup_request; bool ILifeSpanHandler.OnBeforePopup(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, string targetFrameName, WindowOpenDisposition targetDisposition, bool userGesture, IPopupFeatures popupFeatures, IWindowInfo windowInfo, IBrowserSettings browserSettings, ref bool noJavascriptAccess, out IWebBrowser newBrowser) { //get url popup this.popup_request?.Invoke(targetUrl); //stop open popup newBrowser = null; return true; } bool ILifeSpanHandler.DoClose(IWebBrowser browserControl, IBrowser browser) { return false; } void ILifeSpanHandler.OnBeforeClose(IWebBrowser browserControl, IBrowser browser){} void ILifeSpanHandler.OnAfterCreated(IWebBrowser browserControl, IBrowser browser) {} } }
LifespanHandler life = new LifespanHandler(); chrome.LifeSpanHandler = life;
private void life_popup_request(string obj){}
life.popup_request += life_popup_request;
using CefSharp; using CefSharp.WinForms; namespace popup_cefsharp { public partial class frm_main : Form { public frm_main() { InitializeComponent(); } //variable ChromiumWebBrowser chrome, chrome_popup; private void initialize_browser() { try { CefSettings settings = new CefSettings(); Cef.Initialize(settings); //main browser chrome = new ChromiumWebBrowser(this.txt_url.Text.Trim()); LifespanHandler life = new LifespanHandler(); chrome.LifeSpanHandler = life; life.popup_request += life_popup_request; this.pan_container.Controls.Add(chrome); chrome.Dock = DockStyle.Fill; //second browser (popup browser) chrome_popup = new ChromiumWebBrowser(""); this.pan_container_popup.Controls.Add(chrome_popup); chrome_popup.Dock = DockStyle.Fill; } catch (Exception ex) { MessageBox.Show("Error in initializing the browser. Error: " + ex.Message); } } private void carregar_popup_new_browser(string url) { //open pop up in second browser chrome_popup.Load(url); } private void frm_main_FormClosing(object sender, FormClosingEventArgs e) { //close o object cef Cef.Shutdown(); Application.Exit(); } private void frm_main_Load(object sender, EventArgs e) { //initialize the browser this.initialize_browser(); } private void life_popup_request(string obj) { //function for open pop up in a new browser this.carregar_popup_new_browser(obj); } } }
private void life_popup_request(string obj) { chrome_popup = new ChromiumWebBrowser(url); this.Invoke((MethodInvoker)delegate() { this.pan_container_popup.Controls.Clear(); this.pan_container_popup.Controls.Add(chrome_popup); }); chrome_popup.Dock = DockStyle.Fill; }