A .Net Helper class in the System.Windows.Interop namespace called HwndSource that can be used to hook into the window procedure (amongst other things).
From the MSDN documentation:
The HwndSource class implements its own window procedure. This window procedure is used to process important window messages, such as those related to layout, rendering, and input. However, you can also hook the window procedure for your own use. You can specify your own hook during construction by setting the HwndSourceParameters.HwndSourceHook property, or you can also use AddHook and RemoveHook to add and remove hooks after the window has been created.
Subclassing Window's WndProc
Every now and then, you'll find a windows message that has no WPF equivalent. HwndSource lets you use a WndProc to get window messages; what you may not know is that you can still use the Window class.
It's not obvious how to go from a Window to a HwndSource. the trick is to use WindowInteropHelper to get an hwnd, then use HwndSource.FromHwnd. Once you have that, you can use HwndSource.AddHook to subclass Window's WndProc.
In this example, We detect WM_QUERYENDSESSION and put up a MessageBox:
public partial class WpfWindow : Window
{
public WpfWindow ()
{
InitializeComponent();
}
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
source.AddHook(WndProc);
}
private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
// Handle whatever Win32 message it is we feel like handling
if (msg == WM_QUERYENDSESSION)
{
MessageBox.Show("WM_QUERYENDSESSION ... Got You !");
handled = true;
}
return IntPtr.Zero;
}
private const int WM_DESTROY = 0x0011;
}
ref:
Nick on Silverlight and WPF -
http://blogs.msdn.com/nickkramer/archive/2006/03/18/554235.aspx
Using a custom WndProc in WPF apps -
http://www.steverands.com/2009/03/19/custom-wndproc-wpf-apps/
http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework.windowsforms/topic33713.aspx