Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork3k
Creating user tabs or popup windows in Winforms
aimcosoftware edited this pageMay 23, 2023 ·1 revision
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.
DimHandlerInstanceAsNewMyLifeSpanHandler()DimMyBrowserAsNewChromiumWebBrowser()MyBrowser.LifeSpanHandler=HandlerInstance
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.
ClassMyLifeSpanHandler:InheritsWinForms.Handler.LifeSpanHandlerProtectedOverridesFunctionOnBeforePopup(chromiumWebBrowserAsIWebBrowser,browserAsIBrowser,frameAsIFrame,targetUrlAsString,targetFrameNameAsString,targetDispositionAsWindowOpenDisposition,userGestureAsBoolean,popupFeaturesAsIPopupFeatures,windowInfoAsIWindowInfo,browserSettingsAsIBrowserSettings,ByRefnoJavascriptAccessAsBoolean,ByRefnewBrowserAsIWebBrowser)AsBoolean'Get Winforms control to create on same threadCType(chromiumWebBrowser,ChromiumWebBrowser).Invoke(Sub()'Create browser without URL and set as popupBrowser=NewChromiumWebBrowser()'Identify popup to use in DoCloseBrowser.Name="MyPopup"Browser.SetAsPopupBrowser.LifeSpanHandler=MeBrowser.Dock=DockStyle.Fill'This is a simple test, you can get better detail from targetDispositionIfpopupFeatures.IsPopupThen'Create your popup formDimFrm=NewMyPopupFrm'Will need to check zero, offscreen and minimum boundsFrm.Bounds=NewRectangle(popupFeatures.X,popupFeatures.Y,popupFeatures.Width,popupFeatures.Height)Frm.Controls.Add(Browser)'Need to show form on this threadFrm.Show()Else'Add browser to your tab controlDimTab=NewMyTabPageTab.Controls.Add(Browser)EndIfEndSub)'Pass back in newBrowser and set as childnewBrowser=BrowserwindowInfo.SetAsChild(newBrowser.Handle)'Return false (True cancels any popup)ReturnFalseEndFunction'Other functions in LifeSpanHandler...
When a popup is closed by CEF e.g. from script, theDoClose() event is raised in theLifeSpanHandler.
'Function inside MyLifeSpanHandlerProtectedOverridesFunctionDoClose(WebBrowserAsIWebBrowser,browserAsIBrowser)AsBoolean'Get ChromiumWebBrowserDimBrowser=CType(WebBrowser,ChromiumWebBrowser)'Simple way to identify your popupIfBrowser.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 CEFReturnTrueElse'Not our popup so allow close message from CEFReturnFalseEndIfEndFunction