mirror of
https://github.com/cefsharp/CefSharp.git
synced 2026-12-04 19:25:01 +00:00
110 lines
3.5 KiB
C#
110 lines
3.5 KiB
C#
// Copyright © 2010-2017 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
|
|
{
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
internal class ChromeWidgetMessageInterceptor : NativeWindow
|
|
{
|
|
private Action<Message> forwardAction;
|
|
|
|
internal ChromeWidgetMessageInterceptor(Control browser, IntPtr chromeWidgetHostHandle, Action<Message> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
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;
|
|
}
|
|
}
|
|
}
|