Files
cefsharp/CefSharp.WinForms/WebBrowserExtensions.cs
T
Alex Maitland 0e242a8706 Upgrade to CEF 95.7.10+g00d4ad5+chromium-95.0.4638.54 / Chromium 95.0.4638.54
Remove CefBrowserSettings.universal_access_from_file_urls and CefBrowserSettings.file_access_from_file_urls.
https://bitbucket.org/chromiumembedded/cef/commits/ff33d7f72144

Remove CefBrowserSettings.application_cache
https://bitbucket.org/chromiumembedded/cef/commits/370a7749e950ef6784c29a666d07a0b67148dd5f

Remove ignore_certificate_errors setting
https://bitbucket.org/chromiumembedded/cef/commits/aabe4ce466766f8bd9907e7fa30546a3afada214

RequestContextSettings.IgnoreCertificateErrors was removed, the CefSettings.IgnoreCertificateErrors property
now set the command line arg

Use CefRect for CefWindowInfo bounds
https://bitbucket.org/chromiumembedded/cef/commits/8587cc4e4bbc187aa989152e521318818b3f41d3

Additional overload has been added to pass in a Rect directly to SetAsChild

Rename SendFocusEvent to SetFocus
https://bitbucket.org/chromiumembedded/cef/commits/49a4611e28d7bbc221cff816d7c4a96c55ba2721

SendFocusEvent will now call SetFocus internally, the project internally has been updated to use SetFocus for WPF

Replace CefRequestCallback with CefCallback
https://bitbucket.org/chromiumembedded/cef/commits/3e7f1bb529442a6c7674262d9219df57964e9524

No changes to the API for this, I've kept the same interface and just mapped internally

Resolves #3857, #3667, #3668
2021-10-22 14:45:12 +10:00

134 lines
6.6 KiB
C#

// Copyright © 2019 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 System;
using System.Windows.Forms;
using CefSharp.WinForms.Host;
using CefSharp.WinForms.Internals;
namespace CefSharp.WinForms
{
/// <summary>
/// Helper extensions for performing common CefSharp related WinForms tasks
/// </summary>
public static class WebBrowserExtensions
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool DestroyWindow(IntPtr hWnd);
/// <summary>
/// Manually call https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-destroywindow
/// passing in the handle returned from <see cref="IBrowserHost.GetWindowHandle"/>.
/// This method can be used to manually close the underlying CefBrowser instance.
/// This will avoid the WM_Close message that CEF sends by default to the top level window.
/// (Which closes your application). This method should generally only be used in the WinForms version.
/// </summary>
/// <param name="chromiumWebBrowser">the ChromiumWebBrowser instance</param>
/// <returns>If the function succeeds, the return value is true.</returns>
/// <example>
/// <code>
/// //Invoke on the CEF UI Thread
/// Cef.UIThreadTaskFactory.StartNew(() =>
/// {
/// var closed = chromiumWebBrowser.DestroyWindow();
/// });
/// </code>
/// </example>
public static bool DestroyWindow(this IWebBrowser chromiumWebBrowser)
{
if (!Cef.CurrentlyOnThread(CefThreadIds.TID_UI))
{
throw new InvalidOperationException("This method can only be called on the CEF UI thread." +
"Use Cef.UIThreadTaskFactory to marshal your call onto the CEF UI Thread.");
}
if (chromiumWebBrowser.IsDisposed)
{
return false;
}
var browser = chromiumWebBrowser.GetBrowser();
if (browser == null)
{
return false;
}
var handle = browser.GetHost().GetWindowHandle();
return DestroyWindow(handle);
}
/// <summary>
/// Open DevTools using <paramref name="parentControl"/> as the parent control. If inspectElementAtX and/or inspectElementAtY are specified then
/// the element at the specified (x,y) location will be inspected.
/// For resize/moving to work correctly you will need to use the <see cref="CefSharp.WinForms.Handler.LifeSpanHandler"/> implementation.
/// (Set <see cref="ChromiumWebBrowser.LifeSpanHandler"/> to an instance of <see cref="CefSharp.WinForms.Handler.LifeSpanHandler"/>)
/// </summary>
/// <param name="chromiumWebBrowser"><see cref="ChromiumWebBrowser"/> instance</param>
/// <param name="parentControl">Control used as the parent for DevTools (a custom control will be added to the <see cref="Control.Controls"/> collection)</param>
/// <param name="inspectElementAtX">x coordinate (used for inspectElement)</param>
/// <param name="inspectElementAtY">y coordinate (used for inspectElement)</param>
/// <returns>Returns the <see cref="Control"/> that hosts the DevTools instance if successful, otherwise returns null on error.</returns>
public static Control ShowDevToolsDocked(this IWebBrowser chromiumWebBrowser, Control parentControl, string controlName = nameof(ChromiumHostControl) + "DevTools", DockStyle dockStyle = DockStyle.Fill, int inspectElementAtX = 0, int inspectElementAtY = 0)
{
if (chromiumWebBrowser.IsDisposed || parentControl == null || parentControl.IsDisposed)
{
return null;
}
return chromiumWebBrowser.ShowDevToolsDocked((ctrl) => { parentControl.Controls.Add(ctrl); }, controlName, dockStyle, inspectElementAtX, inspectElementAtY);
}
/// <summary>
/// Open DevTools using your own Control as the parent. If inspectElementAtX and/or inspectElementAtY are specified then
/// the element at the specified (x,y) location will be inspected.
/// For resize/moving to work correctly you will need to use the <see cref="CefSharp.WinForms.Handler.LifeSpanHandler"/> implementation.
/// (Set <see cref="ChromiumWebBrowser.LifeSpanHandler"/> to an instance of <see cref="CefSharp.WinForms.Handler.LifeSpanHandler"/>)
/// </summary>
/// <param name="chromiumWebBrowser"><see cref="ChromiumWebBrowser"/> instance</param>
/// <param name="addParentControl">
/// Action that is Invoked when the DevTools Host Control has been created and needs to be added to it's parent.
/// It's important the control is added to it's intended parent at this point so the <see cref="Control.ClientRectangle"/>
/// can be calculated to set the initial display size.</param>
/// <param name="inspectElementAtX">x coordinate (used for inspectElement)</param>
/// <param name="inspectElementAtY">y coordinate (used for inspectElement)</param>
/// <returns>Returns the <see cref="Control"/> that hosts the DevTools instance if successful, otherwise returns null on error.</returns>
public static Control ShowDevToolsDocked(this IWebBrowser chromiumWebBrowser, Action<ChromiumHostControl> addParentControl, string controlName = nameof(ChromiumHostControl) + "DevTools", DockStyle dockStyle = DockStyle.Fill, int inspectElementAtX = 0, int inspectElementAtY = 0)
{
if (chromiumWebBrowser.IsDisposed || addParentControl == null)
{
return null;
}
var host = chromiumWebBrowser.GetBrowserHost();
if (host == null)
{
return null;
}
var control = new ChromiumHostControl()
{
Name = controlName,
Dock = dockStyle
};
control.CreateControl();
//It's now time for the user to add the control to it's parent
addParentControl(control);
//Devtools will be a child of the ChromiumHostControl
var rect = control.ClientRectangle;
var windowInfo = new WindowInfo();
var windowBounds = new CefSharp.Structs.Rect(rect.X, rect.Y, rect.Width, rect.Height);
windowInfo.SetAsChild(control.Handle, windowBounds);
host.ShowDevTools(windowInfo, inspectElementAtX, inspectElementAtY);
return control;
}
}
}