Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Subclassing WPF window to handle custom messages
using System;
using System.Windows;
using System.Windows.Interop;
static class SubclassWindow
// Call this from the constructor of your main window. SourceInitialized event is raised long after the constructor.
public static void initialize( Window wnd )
wnd.SourceInitialized += ( object sender, EventArgs e ) => sourceInitialized( wnd );
static void sourceInitialized( Window window )
IntPtr handle = ( new WindowInteropHelper( window ) ).Handle;
HwndSource.FromHwnd( handle ).AddHook( new HwndSourceHook( windowProcHook ) );
static IntPtr windowProcHook( IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled )
// Call C++ DLL here. Set handled to true if that window message was handled by the C++ code.
if( interop.handleWindowMessage( hwnd, msg, wParam, lParam ) )
handled = true;
return IntPtr.Zero;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.