Skip to content

Creating user tabs or popup windows in Winforms

aimcosoftware edited this page May 23, 2023 · 1 revision

Implement your own Winforms.LifeSpanHandler

When creating your own ChromiumWebBrowser instance, set the LifeSpanHandler to your own handler. You can use a global lifespan handler, rather than one for each browser.

Dim HandlerInstance As New MyLifeSpanHandler()

Dim MyBrowser As New ChromiumWebBrowser()
MyBrowser.LifeSpanHandler = HandlerInstance

Handling OnBeforePopup in the LifespanHandler.

CEF will navigate the newBrowser and create a new renderer instance where needed. If the URL is on the the same site as the opener, it will use the same renderer.

Class MyLifeSpanHandler : Inherits WinForms.Handler.LifeSpanHandler

 Protected Overrides Function OnBeforePopup(chromiumWebBrowser As IWebBrowser, browser As IBrowser, frame As IFrame, targetUrl As String, targetFrameName As String, targetDisposition As WindowOpenDisposition, userGesture As Boolean, popupFeatures As IPopupFeatures, windowInfo As IWindowInfo, browserSettings As IBrowserSettings, ByRef noJavascriptAccess As Boolean, ByRef newBrowser As IWebBrowser) As Boolean

  'Get Winforms control to create on same thread
   CType(chromiumWebBrowser, ChromiumWebBrowser).Invoke(
   Sub()
    'Create browser without URL and set as popup
    Browser = New ChromiumWebBrowser()
    'Identify popup to use in DoClose
    Browser.Name = "MyPopup"
    Browser.SetAsPopup
    Browser.LifeSpanHandler = Me
    Browser.Dock = DockStyle.Fill

    'This is a simple test, you can get better detail from targetDisposition
    If popupFeatures.IsPopup Then
     'Create your popup form
     Dim Frm = New MyPopupFrm
     'Will need to check zero, offscreen and minimum bounds
     Frm.Bounds = New Rectangle(popupFeatures.X, popupFeatures.Y, popupFeatures.Width, popupFeatures.Height)
     Frm.Controls.Add(Browser)
     'Need to show form on this thread
     Frm.Show()
    Else
     'Add browser to your tab control 
     Dim Tab = New MyTabPage
     Tab.Controls.Add(Browser)
    End If
  End Sub)
  'Pass back in newBrowser and set as child
  newBrowser = Browser
  windowInfo.SetAsChild(newBrowser.Handle)
  'Return false (True cancels any popup)
  Return False
 End Function

 'Other functions in LifeSpanHandler...

Handling DoClose request to close the popup

When a popup is closed by CEF e.g. from script, the DoClose() event is raised in the LifeSpanHandler.

 'Function inside MyLifeSpanHandler
 Protected Overrides Function DoClose(WebBrowser As IWebBrowser, browser As IBrowser) As Boolean
  'Get ChromiumWebBrowser 
  Dim Browser = CType(WebBrowser, ChromiumWebBrowser)
  'Simple way to identify your popup
  If Browser.Name = "MyPopup" Then
   'Close the parent form or tab from it's thread. (You may have to dispose related objects).
   Browser.BeginInvoke(
    Sub() Browser.Parent.Dispose())
   'Cancel default close message from CEF
   Return True
  Else
   'Not our popup so allow close message from CEF
   Return False
  End If
 End Function