Note: Advice in this article will only work for DotNetBrowser 1.
See the corresponding article for DotNetBrowser 2 here.
The following sample demonstrates how to handle popup windows opened via window.open()
JavaScript function. You can catch the moment when Chromium engine needs to display the popup window and decide yourself how this popup window must be displayed (in a separate window, in tab pane, it must be suppressed, etc.).
C#
Form1.cs
using DotNetBrowser; using DotNetBrowser.WinForms; using System; using System.Threading.Tasks; using System.Windows.Forms; namespace WinFormsCustomizePopupSample { public partial class Form1 : Form { public Form1() { InitializeComponent(); // Initialize BrowserView var browser = BrowserFactory.Create(); BrowserView browserView = new WinFormsBrowserView(browser); Controls.Add((Control)browserView); browser.PopupHandler = new CustomPopupHandler(this); browser.LoadURL("http://www.popuptest.com/popuptest1.html"); } } }
CustomPopupHandler.cs
using DotNetBrowser; using DotNetBrowser.WinForms; using System; namespace WinFormsCustomizePopupSample { class CustomPopupHandler : PopupHandler { private Control control; public CustomPopupHandler(Control control) { this.control = control; } public PopupContainer HandlePopup(PopupParams popupParams) { Form form = control.FindForm(); if (form != null) { return new CustomPopupContainer(form); } else { throw new InvalidOperationException("Main window can't be found"); } } } }
CustomPopupContainer.cs
using DotNetBrowser; using DotNetBrowser.Events; using DotNetBrowser.WinForms; using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Windows.Forms; namespace WinFormsCustomizePopupSample { class CustomPopupContainer : PopupContainer { private Form parentForm; private Form form; private WinFormsBrowserView browserView; private bool formClosed = false; /// <summary> /// Constructor creates WinForms Popup Container based on parent form. /// </summary> public CustomPopupContainer (Form parentForm) { this.parentForm = parentForm; } /// <summary> /// Creates a WinForms popup window with embedded Browser instance. /// </summary> /// <param name="browser">the newly created browser.</param> /// <param name="initialBounds">initial bounds of popup window.</param> public void InsertBrowser(Browser browser, Rectangle initialBounds) { parentForm.BeginInvoke((Action)(() => { Init(browser, initialBounds); })); } private void Init(Browser browser, Rectangle initialBounds) { browserView = new WinFormsBrowserView(browser); form = new Form(); if (!initialBounds.IsEmpty) { form.DesktopLocation = new Point(initialBounds.X, initialBounds.Y); form.ClientSize = new Size(initialBounds.Width, initialBounds.Height); browserView.UpdateSize(initialBounds.Width, initialBounds.Height); } else { form.Width = 800; form.Height = 600; } form.Closed += delegate { formClosed = true; form.Controls.Clear(); browserView.Dispose(); browser.Dispose(); }; browser.TitleChangedEvent += delegate(object sender, TitleEventArgs e) { form.BeginInvoke((Action)(() => { form.Text = e.Title; })); }; browser.DisposeEvent += delegate(object sender, DisposeEventArgs e) { form.BeginInvoke((Action)(() => { if (!formClosed) { form.Controls.Clear(); form.Hide(); form.Close(); formClosed = true; } })); }; form.Controls.Add(browserView); form.Show(); } } }
VB.NET
Form1.vb
Imports DotNetBrowser Imports DotNetBrowser.WinForms Public Class Form1 Public Sub New() Dim browserView As BrowserView InitializeComponent() browserView = New WinFormsBrowserView() Controls.Add(browserView) browserView.Browser.PopupHandler = New CustomPopupHandler(Me) browserView.Browser.LoadURL("http://www.teamdev.com") End Sub End Class
CustomPopupHandler.vb
Imports DotNetBrowser Public Class CustomPopupHandler Implements PopupHandler Private control As Control Public Sub New(control As Control) Me.control = control End Sub Public Function HandlePopup(popupParams As PopupParams) As PopupContainer Implements PopupHandler.HandlePopup Dim Form = control.FindForm() If (Not (Form Is Nothing)) Then Return New CustomPopupContainer(Form) Else Throw New InvalidOperationException("Main window can't be found") End If End Function End Class
CustomPopupContainer.vb
Imports DotNetBrowser Imports DotNetBrowser.WinForms Public Class CustomPopupContainer Implements PopupContainer Private form As Form Private parentForm As Form Private browserView As WinFormsBrowserView Private formClosed As Boolean = False Public Sub New(parentForm As Form) Me.parentForm = parentForm End Sub Public Sub InsertBrowser(browser As Browser, initialBounds As Rectangle) Implements PopupContainer.InsertBrowser parentForm.BeginInvoke(Sub() Init(browser, initialBounds)) End Sub Private Sub Init(browser As Browser, initialBounds As Rectangle) browserView = New WinFormsBrowserView(browser) form = New Form() If (Not (initialBounds.IsEmpty)) Then form.DesktopLocation = New Point(initialBounds.X, initialBounds.Y) form.ClientSize = New Size(initialBounds.Width, initialBounds.Height) browserView.UpdateSize(initialBounds.Width, initialBounds.Height) Else form.Width = 800 form.Height = 600 End If AddHandler form.Closed, Sub() formClosed = True form.Controls.Clear() browserView.Dispose() browser.Dispose() End Sub AddHandler browser.TitleChangedEvent, Sub(sender, e) form.BeginInvoke(Sub() form.Text = e.Title) End Sub AddHandler browser.DisposeEvent, Sub(sender, e) form.BeginInvoke(Sub() If (Not (formClosed)) Then form.Controls.Clear() form.Hide() form.Close() formClosed = True End If End Sub) End Sub AddHandler browser.FinishLoadingFrameEvent, Sub(sender, e) If (e.IsMainFrame) Then form.BeginInvoke(Sub() UpdateForm()) End If End Sub form.Controls.Add(browserView) form.Show() End Sub Sub UpdateForm() form.Text += " - Loaded" End Sub End Class