|
| 1 | +// Copyright © 2015 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 System; |
| 6 | +using System.Runtime.InteropServices; |
| 7 | +using System.Text; |
| 8 | + |
| 9 | +namespace CefSharp.WinForms.Experimental |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// ChromiumWidgetHandleFinder is a helper class used to find the <see cref="ChromeRenderWidgetHostClassName"/> |
| 13 | + /// child Hwnd for the browser instance. |
| 14 | + /// </summary> |
| 15 | + public static class ChromiumRenderWidgetHandleFinder |
| 16 | + { |
| 17 | + /// <summary> |
| 18 | + /// Class Name of the Chrome_RenderWidgetHostHWND Child Window |
| 19 | + /// </summary> |
| 20 | + public const string ChromeRenderWidgetHostClassName = "Chrome_RenderWidgetHostHWND"; |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// EnumWindowProc delegate used by <see cref="EnumChildWindows(IntPtr, EnumWindowProc, IntPtr)"/> |
| 24 | + /// </summary> |
| 25 | + /// <param name="hwnd">A handle to a child window of the parent window specified in EnumChildWindows</param> |
| 26 | + /// <param name="lParam">The application-defined value given in EnumChildWindows</param> |
| 27 | + /// <returns>To continue enumeration, the callback function must return true; to stop enumeration, it must return false.</returns> |
| 28 | + private delegate bool EnumWindowProc(IntPtr hwnd, IntPtr lParam); |
| 29 | + |
| 30 | + [DllImport("user32")] |
| 31 | + [return: MarshalAs(UnmanagedType.Bool)] |
| 32 | + private static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr lParam); |
| 33 | + |
| 34 | + [DllImport("user32.dll", CharSet = CharSet.Auto)] |
| 35 | + private static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount); |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Chromium's message-loop Window isn't created synchronously, so this may not find it. |
| 39 | + /// If so, you need to wait and try again later. |
| 40 | + /// </summary> |
| 41 | + /// <param name="chromiumWebBrowser">ChromiumWebBrowser instance</param> |
| 42 | + /// <param name="chromerRenderWidgetHostHandle">Handle of the child HWND with the name <see cref="ChromeRenderWidgetHostClassName"/></param> |
| 43 | + /// <returns>returns true if the HWND was found otherwise false.</returns> |
| 44 | + public static bool TryFindHandle(IWebBrowser chromiumWebBrowser, out IntPtr chromerRenderWidgetHostHandle) |
| 45 | + { |
| 46 | + var host = chromiumWebBrowser.GetBrowserHost(); |
| 47 | + if (host == null) |
| 48 | + { |
| 49 | + throw new Exception("IBrowserHost is null, you've likely call this method before the underlying browser has been created."); |
| 50 | + } |
| 51 | + |
| 52 | + var hwnd = host.GetWindowHandle(); |
| 53 | + |
| 54 | + return TryFindHandle(hwnd, ChromeRenderWidgetHostClassName, out chromerRenderWidgetHostHandle); |
| 55 | + } |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// Helper function used to find the child HWND with the ClassName matching <paramref name="chromeRenderWidgetHostClassName"/> |
| 59 | + /// Chromium's message-loop Window isn't created synchronously, so this may not find it. |
| 60 | + /// If so, you need to wait and try again later. |
| 61 | + /// In most cases you should use the <see cref="TryFindHandle(ChromiumWebBrowser, out IntPtr)"/> overload. |
| 62 | + /// </summary> |
| 63 | + /// <param name="chromiumWebBrowserHandle"><see cref="ChromiumWebBrowser"/> control Handle</param> |
| 64 | + /// <param name="chromeRenderWidgetHostClassName">class name used to match</param> |
| 65 | + /// <param name="chromerRenderWidgetHostHandle">Handle of the child HWND with the name <see cref="ChromeRenderWidgetHostClassName"/></param> |
| 66 | + /// <returns>returns true if the HWND was found otherwise false.</returns> |
| 67 | + public static bool TryFindHandle(IntPtr chromiumWebBrowserHandle, string chromeRenderWidgetHostClassName, out IntPtr chromerRenderWidgetHostHandle) |
| 68 | + { |
| 69 | + var chromeRenderWidgetHostHwnd = IntPtr.Zero; |
| 70 | + |
| 71 | + EnumWindowProc childProc = (IntPtr hWnd, IntPtr lParam) => |
| 72 | + { |
| 73 | + var buffer = new StringBuilder(128); |
| 74 | + GetClassName(hWnd, buffer, buffer.Capacity); |
| 75 | + |
| 76 | + if (buffer.ToString() == chromeRenderWidgetHostClassName) |
| 77 | + { |
| 78 | + chromeRenderWidgetHostHwnd = hWnd; |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + return true; |
| 83 | + }; |
| 84 | + |
| 85 | + EnumChildWindows(chromiumWebBrowserHandle, childProc, IntPtr.Zero); |
| 86 | + |
| 87 | + chromerRenderWidgetHostHandle = chromeRenderWidgetHostHwnd; |
| 88 | + |
| 89 | + return chromerRenderWidgetHostHandle != IntPtr.Zero; |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
0 commit comments