Skip to content

Commit 870dbb5

Browse files
committedFeb 10, 2022
WinForms - Add new IWinFormsChromiumWebBrowser interface
- Common interface of ChromiumHostControl and ChromiumWebBrowser - WinForms Example Fix removing of Tab
1 parent 7906919 commit 870dbb5

File tree

5 files changed

+55
-36
lines changed

5 files changed

+55
-36
lines changed
 

‎CefSharp.WinForms.Example/BrowserForm.cs

+7-11
Original file line numberDiff line numberDiff line change
@@ -140,22 +140,18 @@ private void AboutToolStripMenuItemClick(object sender, EventArgs e)
140140
new AboutBox().ShowDialog();
141141
}
142142

143-
public void RemoveTab(IntPtr windowHandle)
143+
public void RemoveTab(ChromiumHostControl ctrl)
144144
{
145-
var parentControl = FromChildHandle(windowHandle);
146-
if (!parentControl.IsDisposed)
145+
if (!ctrl.IsDisposed)
147146
{
148-
if (parentControl.Parent is TabPage tabPage)
147+
var tabPage = ctrl.GetParentOfType<TabPage>();
148+
149+
if(tabPage == null)
149150
{
150-
browserTabControl.TabPages.Remove(tabPage);
151+
throw new Exception("Unable to find parent TabPage");
151152
}
152-
else if (parentControl.Parent is Panel panel)
153-
{
154-
var browserTabUserControl = (BrowserTabUserControl)panel.Parent;
155153

156-
var tab = (TabPage)browserTabUserControl.Parent;
157-
browserTabControl.TabPages.Remove(tab);
158-
}
154+
browserTabControl.TabPages.Remove(tabPage);
159155
}
160156
}
161157

‎CefSharp.WinForms.Example/BrowserTabUserControl.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace CefSharp.WinForms.Example
2020
{
2121
public partial class BrowserTabUserControl : UserControl
2222
{
23-
public IChromiumWebBrowserBase Browser { get; private set; }
23+
public IWinFormsChromiumWebBrowser Browser { get; private set; }
2424
private ChromiumWidgetNativeWindow messageInterceptor;
2525
private bool multiThreadedMessageLoopEnabled;
2626

@@ -109,9 +109,7 @@ public BrowserTabUserControl(Action<string, int?> openNewTab, string url, bool m
109109
{
110110
if (ctrl.FindForm() is BrowserForm owner)
111111
{
112-
var windowHandle = popupBrowser.GetHost().GetWindowHandle();
113-
114-
owner.RemoveTab(windowHandle);
112+
owner.RemoveTab(ctrl);
115113
}
116114

117115
ctrl.Dispose();

‎CefSharp.WinForms/Host/ChromiumHostControl.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace CefSharp.WinForms.Host
1616
/// <seealso cref="Control" />
1717
[Docking(DockingBehavior.AutoDock), ToolboxBitmap(typeof(ChromiumHostControl)),
1818
Designer(typeof(ChromiumWebBrowserDesigner))]
19-
public class ChromiumHostControl : ChromiumHostControlBase, IChromiumWebBrowserBase
19+
public class ChromiumHostControl : ChromiumHostControlBase, IWinFormsChromiumWebBrowser
2020
{
2121
/// <summary>
2222
/// Get access to the core <see cref="IBrowser"/> instance.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright © 2022 The CefSharp Authors. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
4+
5+
using CefSharp.WinForms.Host;
6+
using System;
7+
using System.ComponentModel;
8+
using System.Windows.Forms;
9+
10+
namespace CefSharp.WinForms
11+
{
12+
/// <summary>
13+
/// Winforms Specific Chromium browser implementation, differs from <see cref="IWinFormsWebBrowser"/> in that
14+
/// this interface is implemented by both <see cref="ChromiumWebBrowser"/> and <see cref="ChromiumHostControl"/>
15+
/// where <see cref="IWinFormsWebBrowser"/> is only implemented by <see cref="ChromiumWebBrowser"/>
16+
/// </summary>
17+
public interface IWinFormsChromiumWebBrowser : IChromiumWebBrowserBase, IWin32Window, IComponent, ISynchronizeInvoke
18+
{
19+
/// <summary>
20+
/// Occurs when the browser title changed.
21+
/// 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
22+
/// thread. It is unwise to block on this thread for any length of time as your browser will become unresponsive and/or hang..
23+
/// To access UI elements you'll need to Invoke/Dispatch onto the UI Thread.
24+
/// </summary>
25+
event EventHandler<TitleChangedEventArgs> TitleChanged;
26+
/// <summary>
27+
/// Occurs when the browser address changed.
28+
/// 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
29+
/// thread. It is unwise to block on this thread for any length of time as your browser will become unresponsive and/or hang..
30+
/// To access UI elements you'll need to Invoke/Dispatch onto the UI Thread.
31+
/// </summary>
32+
event EventHandler<AddressChangedEventArgs> AddressChanged;
33+
34+
/// <summary>
35+
/// Event called after the underlying CEF browser instance has been created.
36+
/// 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
37+
/// thread. It is unwise to block on this thread for any length of time as your browser will become unresponsive and/or hang..
38+
/// To access UI elements you'll need to Invoke/Dispatch onto the UI Thread.
39+
/// </summary>
40+
event EventHandler IsBrowserInitializedChanged;
41+
}
42+
}

‎CefSharp.WinForms/IWinFormsWebBrowser.cs

+3-20
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,15 @@
22
//
33
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
44

5-
using System;
6-
using System.ComponentModel;
7-
using System.Windows.Forms;
8-
95
namespace CefSharp.WinForms
106
{
117
/// <summary>
128
/// WinForms specific implementation, has events the
139
/// <see cref="ChromiumWebBrowser" /> implementation exposes.
1410
/// </summary>
15-
/// <seealso cref="CefSharp.IWebBrowser" />
16-
public interface IWinFormsWebBrowser : IWebBrowser, IWin32Window, IComponent, ISynchronizeInvoke
11+
/// <seealso cref="IWebBrowser" /> and <seealso cref="IChromiumWebBrowserBase"/>
12+
public interface IWinFormsWebBrowser : IWebBrowser, IWinFormsChromiumWebBrowser
1713
{
18-
/// <summary>
19-
/// Occurs when the browser title changed.
20-
/// 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
21-
/// thread. It is unwise to block on this thread for any length of time as your browser will become unresponsive and/or hang..
22-
/// To access UI elements you'll need to Invoke/Dispatch onto the UI Thread.
23-
/// </summary>
24-
event EventHandler<TitleChangedEventArgs> TitleChanged;
25-
/// <summary>
26-
/// Occurs when the browser address changed.
27-
/// 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
28-
/// thread. It is unwise to block on this thread for any length of time as your browser will become unresponsive and/or hang..
29-
/// To access UI elements you'll need to Invoke/Dispatch onto the UI Thread.
30-
/// </summary>
31-
event EventHandler<AddressChangedEventArgs> AddressChanged;
14+
3215
}
3316
}

0 commit comments

Comments
 (0)
Please sign in to comment.