From defc2c3af6ec418c3221acfc0bdca3e06d5b89f2 Mon Sep 17 00:00:00 2001 From: amaitland Date: Mon, 6 Apr 2020 10:48:53 +1000 Subject: [PATCH] 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 917480da3b36cb64d8ba77eb7825d078a848561c Updated to match the cefclient example https://github.com/chromiumembedded/cef/blob/9df4a54308a88fd80c5774d91c62da35afb5fd1b/tests/cefclient/browser/root_window_win.cc#L1088 Resolves #2776 --- CefSharp.Core/NativeMethodWrapper.cpp | 13 +++++++++++ CefSharp.Core/NativeMethodWrapper.h | 1 + CefSharp.WinForms/ChromiumWebBrowser.cs | 29 +++++++++++++++++++++---- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/CefSharp.Core/NativeMethodWrapper.cpp b/CefSharp.Core/NativeMethodWrapper.cpp index ef9612457..fc88312e1 100644 --- a/CefSharp.Core/NativeMethodWrapper.cpp +++ b/CefSharp.Core/NativeMethodWrapper.cpp @@ -45,4 +45,17 @@ namespace CefSharp SetParent(childHwnd, newParentHwnd); } + + void NativeMethodWrapper::RemoveExNoActivateStyle(IntPtr browserHwnd) + { + HWND hwnd = static_cast(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); + } + } } diff --git a/CefSharp.Core/NativeMethodWrapper.h b/CefSharp.Core/NativeMethodWrapper.h index 1ee0ca94f..da6b12041 100644 --- a/CefSharp.Core/NativeMethodWrapper.h +++ b/CefSharp.Core/NativeMethodWrapper.h @@ -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); }; } diff --git a/CefSharp.WinForms/ChromiumWebBrowser.cs b/CefSharp.WinForms/ChromiumWebBrowser.cs index 9cb5535e4..1c893f181 100644 --- a/CefSharp.WinForms/ChromiumWebBrowser.cs +++ b/CefSharp.WinForms/ChromiumWebBrowser.cs @@ -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; + /// /// The managed cef browser adapter /// @@ -57,6 +61,11 @@ namespace CefSharp.WinForms /// private bool initialAddressLoaded; /// + /// 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. + /// + private bool removeExNoActivateStyle; + /// /// Browser initialization settings /// private IBrowserSettings browserSettings; @@ -688,10 +697,6 @@ namespace CefSharp.WinForms /// 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) {