From 870dbb527fd1009776d00435fd061495ca9ce1d4 Mon Sep 17 00:00:00 2001 From: Alex Maitland Date: Mon, 31 Jan 2022 12:54:18 +1000 Subject: [PATCH] WinForms - Add new IWinFormsChromiumWebBrowser interface - Common interface of ChromiumHostControl and ChromiumWebBrowser - WinForms Example Fix removing of Tab --- CefSharp.WinForms.Example/BrowserForm.cs | 18 ++++---- .../BrowserTabUserControl.cs | 6 +-- CefSharp.WinForms/Host/ChromiumHostControl.cs | 2 +- .../IWinFormsChromiumWebBrowser.cs | 42 +++++++++++++++++++ CefSharp.WinForms/IWinFormsWebBrowser.cs | 23 ++-------- 5 files changed, 55 insertions(+), 36 deletions(-) create mode 100644 CefSharp.WinForms/IWinFormsChromiumWebBrowser.cs diff --git a/CefSharp.WinForms.Example/BrowserForm.cs b/CefSharp.WinForms.Example/BrowserForm.cs index 09e2dd5912..7ce01cad7c 100644 --- a/CefSharp.WinForms.Example/BrowserForm.cs +++ b/CefSharp.WinForms.Example/BrowserForm.cs @@ -140,22 +140,18 @@ private void AboutToolStripMenuItemClick(object sender, EventArgs e) new AboutBox().ShowDialog(); } - public void RemoveTab(IntPtr windowHandle) + public void RemoveTab(ChromiumHostControl ctrl) { - var parentControl = FromChildHandle(windowHandle); - if (!parentControl.IsDisposed) + if (!ctrl.IsDisposed) { - if (parentControl.Parent is TabPage tabPage) + var tabPage = ctrl.GetParentOfType(); + + if(tabPage == null) { - browserTabControl.TabPages.Remove(tabPage); + throw new Exception("Unable to find parent TabPage"); } - else if (parentControl.Parent is Panel panel) - { - var browserTabUserControl = (BrowserTabUserControl)panel.Parent; - var tab = (TabPage)browserTabUserControl.Parent; - browserTabControl.TabPages.Remove(tab); - } + browserTabControl.TabPages.Remove(tabPage); } } diff --git a/CefSharp.WinForms.Example/BrowserTabUserControl.cs b/CefSharp.WinForms.Example/BrowserTabUserControl.cs index cbe2cc0a6a..174e72ad2c 100644 --- a/CefSharp.WinForms.Example/BrowserTabUserControl.cs +++ b/CefSharp.WinForms.Example/BrowserTabUserControl.cs @@ -20,7 +20,7 @@ namespace CefSharp.WinForms.Example { public partial class BrowserTabUserControl : UserControl { - public IChromiumWebBrowserBase Browser { get; private set; } + public IWinFormsChromiumWebBrowser Browser { get; private set; } private ChromiumWidgetNativeWindow messageInterceptor; private bool multiThreadedMessageLoopEnabled; @@ -109,9 +109,7 @@ public BrowserTabUserControl(Action openNewTab, string url, bool m { if (ctrl.FindForm() is BrowserForm owner) { - var windowHandle = popupBrowser.GetHost().GetWindowHandle(); - - owner.RemoveTab(windowHandle); + owner.RemoveTab(ctrl); } ctrl.Dispose(); diff --git a/CefSharp.WinForms/Host/ChromiumHostControl.cs b/CefSharp.WinForms/Host/ChromiumHostControl.cs index 8f3c7b5339..bfe8bf8b5d 100644 --- a/CefSharp.WinForms/Host/ChromiumHostControl.cs +++ b/CefSharp.WinForms/Host/ChromiumHostControl.cs @@ -16,7 +16,7 @@ namespace CefSharp.WinForms.Host /// [Docking(DockingBehavior.AutoDock), ToolboxBitmap(typeof(ChromiumHostControl)), Designer(typeof(ChromiumWebBrowserDesigner))] - public class ChromiumHostControl : ChromiumHostControlBase, IChromiumWebBrowserBase + public class ChromiumHostControl : ChromiumHostControlBase, IWinFormsChromiumWebBrowser { /// /// Get access to the core instance. diff --git a/CefSharp.WinForms/IWinFormsChromiumWebBrowser.cs b/CefSharp.WinForms/IWinFormsChromiumWebBrowser.cs new file mode 100644 index 0000000000..7a145f8a08 --- /dev/null +++ b/CefSharp.WinForms/IWinFormsChromiumWebBrowser.cs @@ -0,0 +1,42 @@ +// Copyright © 2022 The CefSharp Authors. All rights reserved. +// +// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. + +using CefSharp.WinForms.Host; +using System; +using System.ComponentModel; +using System.Windows.Forms; + +namespace CefSharp.WinForms +{ + /// + /// Winforms Specific Chromium browser implementation, differs from in that + /// this interface is implemented by both and + /// where is only implemented by + /// + public interface IWinFormsChromiumWebBrowser : IChromiumWebBrowserBase, IWin32Window, IComponent, ISynchronizeInvoke + { + /// + /// Occurs when the browser title changed. + /// It's important to note this event is fired on a CEF UI thread, which by default is not the same as your application UI + /// thread. It is unwise to block on this thread for any length of time as your browser will become unresponsive and/or hang.. + /// To access UI elements you'll need to Invoke/Dispatch onto the UI Thread. + /// + event EventHandler TitleChanged; + /// + /// Occurs when the browser address changed. + /// It's important to note this event is fired on a CEF UI thread, which by default is not the same as your application UI + /// thread. It is unwise to block on this thread for any length of time as your browser will become unresponsive and/or hang.. + /// To access UI elements you'll need to Invoke/Dispatch onto the UI Thread. + /// + event EventHandler AddressChanged; + + /// + /// Event called after the underlying CEF browser instance has been created. + /// It's important to note this event is fired on a CEF UI thread, which by default is not the same as your application UI + /// thread. It is unwise to block on this thread for any length of time as your browser will become unresponsive and/or hang.. + /// To access UI elements you'll need to Invoke/Dispatch onto the UI Thread. + /// + event EventHandler IsBrowserInitializedChanged; + } +} diff --git a/CefSharp.WinForms/IWinFormsWebBrowser.cs b/CefSharp.WinForms/IWinFormsWebBrowser.cs index 5bb0aa7f60..c4cc58ea6f 100644 --- a/CefSharp.WinForms/IWinFormsWebBrowser.cs +++ b/CefSharp.WinForms/IWinFormsWebBrowser.cs @@ -2,32 +2,15 @@ // // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. -using System; -using System.ComponentModel; -using System.Windows.Forms; - namespace CefSharp.WinForms { /// /// WinForms specific implementation, has events the /// implementation exposes. /// - /// - public interface IWinFormsWebBrowser : IWebBrowser, IWin32Window, IComponent, ISynchronizeInvoke + /// and + public interface IWinFormsWebBrowser : IWebBrowser, IWinFormsChromiumWebBrowser { - /// - /// Occurs when the browser title changed. - /// It's important to note this event is fired on a CEF UI thread, which by default is not the same as your application UI - /// thread. It is unwise to block on this thread for any length of time as your browser will become unresponsive and/or hang.. - /// To access UI elements you'll need to Invoke/Dispatch onto the UI Thread. - /// - event EventHandler TitleChanged; - /// - /// Occurs when the browser address changed. - /// It's important to note this event is fired on a CEF UI thread, which by default is not the same as your application UI - /// thread. It is unwise to block on this thread for any length of time as your browser will become unresponsive and/or hang.. - /// To access UI elements you'll need to Invoke/Dispatch onto the UI Thread. - /// - event EventHandler AddressChanged; + } }