diff --git a/CefSharp.WinForms.Example/BrowserTabUserControl.cs b/CefSharp.WinForms.Example/BrowserTabUserControl.cs index 629b09da3..3b531eb46 100644 --- a/CefSharp.WinForms.Example/BrowserTabUserControl.cs +++ b/CefSharp.WinForms.Example/BrowserTabUserControl.cs @@ -12,6 +12,7 @@ using CefSharp.Example; using CefSharp.Example.Handlers; using CefSharp.Example.JavascriptBinding; using CefSharp.WinForms.Example.Handlers; +using CefSharp.WinForms.Experimental; namespace CefSharp.WinForms.Example { @@ -19,7 +20,7 @@ namespace CefSharp.WinForms.Example { public IWinFormsWebBrowser Browser { get; private set; } private IntPtr browserHandle; - private ChromeWidgetMessageInterceptor messageInterceptor; + private ChromiumWidgetNativeWindow messageInterceptor; private bool multiThreadedMessageLoopEnabled; public BrowserTabUserControl(Action openNewTab, string url, bool multiThreadedMessageLoopEnabled) @@ -277,9 +278,11 @@ namespace CefSharp.WinForms.Example while (true) { IntPtr chromeWidgetHostHandle; - if (ChromeWidgetHandleFinder.TryFindHandle(browserHandle, out chromeWidgetHostHandle)) + if (ChromiumRenderWidgetHandleFinder.TryFindHandle(Browser, out chromeWidgetHostHandle)) { - messageInterceptor = new ChromeWidgetMessageInterceptor((Control)Browser, chromeWidgetHostHandle, message => + messageInterceptor = new ChromiumWidgetNativeWindow((Control)Browser, chromeWidgetHostHandle); + + messageInterceptor.OnWndProc(message => { const int WM_MOUSEACTIVATE = 0x0021; const int WM_NCLBUTTONDOWN = 0x00A1; @@ -289,7 +292,7 @@ namespace CefSharp.WinForms.Example if (message.Msg == WM_DESTROY) { SetupMessageInterceptor(); - return; + return false; } if (message.Msg == WM_MOUSEACTIVATE) @@ -324,6 +327,8 @@ namespace CefSharp.WinForms.Example // Console.WriteLine("WM_MOUSELEAVE"); // break; //} + + return false; }); break; diff --git a/CefSharp.WinForms.Example/CefSharp.WinForms.Example.csproj b/CefSharp.WinForms.Example/CefSharp.WinForms.Example.csproj index 13bc4d616..c61540f14 100644 --- a/CefSharp.WinForms.Example/CefSharp.WinForms.Example.csproj +++ b/CefSharp.WinForms.Example/CefSharp.WinForms.Example.csproj @@ -84,7 +84,6 @@ BrowserTabUserControl.cs - Component diff --git a/CefSharp.WinForms.Example/ChromeWidgetMessageInterceptor.cs b/CefSharp.WinForms.Example/ChromeWidgetMessageInterceptor.cs deleted file mode 100644 index 015e11f0b..000000000 --- a/CefSharp.WinForms.Example/ChromeWidgetMessageInterceptor.cs +++ /dev/null @@ -1,109 +0,0 @@ -// Copyright © 2015 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.Runtime.InteropServices; -using System.Text; -using System.Windows.Forms; - -namespace CefSharp.WinForms.Example -{ - /// - /// Intercepts Windows messages sent to the ChromiumWebBrowser control's widget sub-window. - /// - /// It is necessary to listen to the widget sub-window because it receives all Windows messages - /// and forwards them to CEF, rather than the ChromiumWebBrowser.Handle. - /// - /// The supplied Action delegate is fired upon each message. - /// - internal class ChromeWidgetMessageInterceptor : NativeWindow - { - private Action forwardAction; - - internal ChromeWidgetMessageInterceptor(Control browser, IntPtr chromeWidgetHostHandle, Action forwardAction) - { - AssignHandle(chromeWidgetHostHandle); - - browser.HandleDestroyed += BrowserHandleDestroyed; - - this.forwardAction = forwardAction; - } - - private void BrowserHandleDestroyed(object sender, EventArgs e) - { - ReleaseHandle(); - - var browser = (Control)sender; - - browser.HandleDestroyed -= BrowserHandleDestroyed; - forwardAction = null; - } - - protected override void WndProc(ref Message m) - { - base.WndProc(ref m); - - if (forwardAction != null) - { - forwardAction(m); - } - } - } - - internal static class ChromeWidgetHandleFinder - { - private delegate bool EnumWindowProc(IntPtr hwnd, IntPtr lParam); - - [DllImport("user32")] - [return: MarshalAs(UnmanagedType.Bool)] - private static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr lParam); - - [DllImport("user32.dll", CharSet = CharSet.Auto)] - public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount); - - private class ClassDetails - { - public IntPtr DescendantFound { get; set; } - } - - private static bool EnumWindow(IntPtr hWnd, IntPtr lParam) - { - const string chromeWidgetHostClassName = "Chrome_RenderWidgetHostHWND"; - - var buffer = new StringBuilder(128); - GetClassName(hWnd, buffer, buffer.Capacity); - - if (buffer.ToString() == chromeWidgetHostClassName) - { - var gcHandle = GCHandle.FromIntPtr(lParam); - - var classDetails = (ClassDetails)gcHandle.Target; - - classDetails.DescendantFound = hWnd; - return false; - } - - return true; - } - - /// - /// Chrome's message-loop Window isn't created synchronously, so this may not find it. - /// If so, you need to wait and try again later. - /// - public static bool TryFindHandle(IntPtr browserHandle, out IntPtr chromeWidgetHostHandle) - { - var classDetails = new ClassDetails(); - var gcHandle = GCHandle.Alloc(classDetails); - - var childProc = new EnumWindowProc(EnumWindow); - EnumChildWindows(browserHandle, childProc, GCHandle.ToIntPtr(gcHandle)); - - chromeWidgetHostHandle = classDetails.DescendantFound; - - gcHandle.Free(); - - return classDetails.DescendantFound != IntPtr.Zero; - } - } -} diff --git a/CefSharp.WinForms/CefSharp.WinForms.csproj b/CefSharp.WinForms/CefSharp.WinForms.csproj index 78ae967e8..190f73f47 100644 --- a/CefSharp.WinForms/CefSharp.WinForms.csproj +++ b/CefSharp.WinForms/CefSharp.WinForms.csproj @@ -58,6 +58,8 @@ + + diff --git a/CefSharp.WinForms/Experimental/ChromiumRenderWidgetHandleFinder.cs b/CefSharp.WinForms/Experimental/ChromiumRenderWidgetHandleFinder.cs new file mode 100644 index 000000000..fb88aaff1 --- /dev/null +++ b/CefSharp.WinForms/Experimental/ChromiumRenderWidgetHandleFinder.cs @@ -0,0 +1,93 @@ +// Copyright © 2015 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.Runtime.InteropServices; +using System.Text; + +namespace CefSharp.WinForms.Experimental +{ + /// + /// ChromiumWidgetHandleFinder is a helper class used to find the + /// child Hwnd for the browser instance. + /// + public static class ChromiumRenderWidgetHandleFinder + { + /// + /// Class Name of the Chrome_RenderWidgetHostHWND Child Window + /// + public const string ChromeRenderWidgetHostClassName = "Chrome_RenderWidgetHostHWND"; + + /// + /// EnumWindowProc delegate used by + /// + /// A handle to a child window of the parent window specified in EnumChildWindows + /// The application-defined value given in EnumChildWindows + /// To continue enumeration, the callback function must return true; to stop enumeration, it must return false. + private delegate bool EnumWindowProc(IntPtr hwnd, IntPtr lParam); + + [DllImport("user32")] + [return: MarshalAs(UnmanagedType.Bool)] + private static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr lParam); + + [DllImport("user32.dll", CharSet = CharSet.Auto)] + private static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount); + + /// + /// Chromium's message-loop Window isn't created synchronously, so this may not find it. + /// If so, you need to wait and try again later. + /// + /// ChromiumWebBrowser instance + /// Handle of the child HWND with the name + /// returns true if the HWND was found otherwise false. + public static bool TryFindHandle(IWebBrowser chromiumWebBrowser, out IntPtr chromerRenderWidgetHostHandle) + { + var host = chromiumWebBrowser.GetBrowserHost(); + if (host == null) + { + throw new Exception("IBrowserHost is null, you've likely call this method before the underlying browser has been created."); + } + + var hwnd = host.GetWindowHandle(); + + return TryFindHandle(hwnd, ChromeRenderWidgetHostClassName, out chromerRenderWidgetHostHandle); + } + + /// + /// Helper function used to find the child HWND with the ClassName matching + /// Chromium's message-loop Window isn't created synchronously, so this may not find it. + /// If so, you need to wait and try again later. + /// In most cases you should use the overload. + /// + /// control Handle + /// class name used to match + /// Handle of the child HWND with the name + /// returns true if the HWND was found otherwise false. + public static bool TryFindHandle(IntPtr chromiumWebBrowserHandle, string chromeRenderWidgetHostClassName, out IntPtr chromerRenderWidgetHostHandle) + { + var chromeRenderWidgetHostHwnd = IntPtr.Zero; + + EnumWindowProc childProc = (IntPtr hWnd, IntPtr lParam) => + { + var buffer = new StringBuilder(128); + GetClassName(hWnd, buffer, buffer.Capacity); + + if (buffer.ToString() == chromeRenderWidgetHostClassName) + { + chromeRenderWidgetHostHwnd = hWnd; + return false; + } + + return true; + }; + + EnumChildWindows(chromiumWebBrowserHandle, childProc, IntPtr.Zero); + + chromerRenderWidgetHostHandle = chromeRenderWidgetHostHwnd; + + return chromerRenderWidgetHostHandle != IntPtr.Zero; + } + } +} + diff --git a/CefSharp.WinForms/Experimental/ChromiumWidgetNativeWindow.cs b/CefSharp.WinForms/Experimental/ChromiumWidgetNativeWindow.cs new file mode 100644 index 000000000..9efc7e921 --- /dev/null +++ b/CefSharp.WinForms/Experimental/ChromiumWidgetNativeWindow.cs @@ -0,0 +1,69 @@ +// Copyright © 2015 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; + +namespace CefSharp.WinForms.Experimental +{ + /// + /// Provides a convenient implement + /// that can be used without having to create your own class + /// + public class ChromiumWidgetNativeWindow : NativeWindow + { + private Func wndProcHandler; + + /// + /// ChromiumWidgetMessageInterceptor constructor + /// + /// Control is used to handled the event so + /// we can automatically call . If null then you are responsible + /// for calling + /// Hwnd to intercept messages for. + public ChromiumWidgetNativeWindow(Control control, IntPtr chromeWidgetHostHandle) + { + AssignHandle(chromeWidgetHostHandle); + + if (control != null) + { + control.HandleDestroyed += BrowserHandleDestroyed; + } + } + + private void BrowserHandleDestroyed(object sender, EventArgs e) + { + ReleaseHandle(); + + var control = (Control)sender; + + control.HandleDestroyed -= BrowserHandleDestroyed; + wndProcHandler = null; + } + + /// + /// Register a Func which is used to intercept + /// calls. should return true if the message + /// was handled, otherwise false. + /// + /// Func to be used to intercept messages, null to clear an existing function. + public void OnWndProc(Func wndProcHandler) + { + this.wndProcHandler = wndProcHandler; + } + + /// + protected override void WndProc(ref Message m) + { + var handler = wndProcHandler; + + var handled = handler?.Invoke(m); + + if (handled == false) + { + base.WndProc(ref m); + } + } + } +}