// 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);
}
}
}
}