WinForms - Remove WS_EX_NOACTIVATE style on initial page Load

- When LoadingStateChanged occurs we remove the ExStyle WS_EX_NOACTIVATE flag
- Previous change to the FocusHandler was reverted in 917480da3b

Updated to match the cefclient example
https://github.com/chromiumembedded/cef/blob/9df4a54308a88fd80c5774d91c62da35afb5fd1b/tests/cefclient/browser/root_window_win.cc#L1088

Resolves #2776
This commit is contained in:
amaitland
2020-04-06 10:48:53 +10:00
parent 917480da3b
commit defc2c3af6
3 changed files with 39 additions and 4 deletions
+13
View File
@@ -45,4 +45,17 @@ namespace CefSharp
SetParent(childHwnd, newParentHwnd);
}
void NativeMethodWrapper::RemoveExNoActivateStyle(IntPtr browserHwnd)
{
HWND hwnd = static_cast<HWND>(browserHwnd.ToPointer());
auto exStyle = GetWindowLongPtr(hwnd, GWL_EXSTYLE);
if (exStyle & WS_EX_NOACTIVATE)
{
//Remove WS_EX_NOACTIVATE
SetWindowLongPtr(hwnd, GWL_EXSTYLE, exStyle & ~WS_EX_NOACTIVATE);
}
}
}
+1
View File
@@ -19,5 +19,6 @@ namespace CefSharp
static bool IsFocused(IntPtr handle);
static void SetWindowPosition(IntPtr handle, int x, int y, int width, int height);
static void SetWindowParent(IntPtr child, IntPtr newParent);
static void RemoveExNoActivateStyle(IntPtr browserHwnd);
};
}
+25 -4
View File
@@ -24,6 +24,10 @@ namespace CefSharp.WinForms
Designer(typeof(ChromiumWebBrowserDesigner))]
public class ChromiumWebBrowser : Control, IWebBrowserInternal, IWinFormsWebBrowser
{
//TODO: If we start adding more consts then extract them into a common class
//Possibly in the CefSharp assembly and move the WPF ones into there as well.
private const uint WS_EX_NOACTIVATE = 0x08000000;
/// <summary>
/// The managed cef browser adapter
/// </summary>
@@ -57,6 +61,11 @@ namespace CefSharp.WinForms
/// </summary>
private bool initialAddressLoaded;
/// <summary>
/// If true the the WS_EX_NOACTIVATE style will be removed so that future mouse clicks
/// inside the browser correctly activate and focus the window.
/// </summary>
private bool removeExNoActivateStyle;
/// <summary>
/// Browser initialization settings
/// </summary>
private IBrowserSettings browserSettings;
@@ -688,10 +697,6 @@ namespace CefSharp.WinForms
/// </example>
protected virtual IWindowInfo CreateBrowserWindowInfo(IntPtr handle)
{
//TODO: If we start adding more consts then extract them into a common class
//Possibly in the CefSharp assembly and move the WPF ones into there as well.
const uint WS_EX_NOACTIVATE = 0x08000000;
var windowInfo = new WindowInfo();
windowInfo.SetAsChild(handle);
@@ -729,6 +734,10 @@ namespace CefSharp.WinForms
{
var windowInfo = CreateBrowserWindowInfo(Handle);
//We actually check if WS_EX_NOACTIVATE was set for instances
//the user has override CreateBrowserWindowInfo and not called base.CreateBrowserWindowInfo
removeExNoActivateStyle = (windowInfo.ExStyle & WS_EX_NOACTIVATE) == WS_EX_NOACTIVATE;
initialAddressLoaded = !string.IsNullOrEmpty(Address);
managedCefBrowserAdapter.CreateBrowser(windowInfo, browserSettings as BrowserSettings, requestContext as RequestContext, Address);
@@ -794,6 +803,18 @@ namespace CefSharp.WinForms
CanGoForward = args.CanGoForward;
IsLoading = args.IsLoading;
if (removeExNoActivateStyle && browser != null)
{
removeExNoActivateStyle = false;
var host = this.GetBrowserHost();
var hwnd = host.GetWindowHandle();
//Remove the WS_EX_NOACTIVATE style so that future mouse clicks inside the
//browser correctly activate and focus the browser.
//https://github.com/chromiumembedded/cef/blob/9df4a54308a88fd80c5774d91c62da35afb5fd1b/tests/cefclient/browser/root_window_win.cc#L1088
NativeMethodWrapper.RemoveExNoActivateStyle(hwnd);
}
var handler = LoadingStateChanged;
if (handler != null)
{