Ask Learn
Preview
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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 silly example, I detect WM_DESTROY and put up a MessageBox:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
void Window1_Loaded(object sender, RoutedEventArgs e)
{
HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
source.AddHook(new HwndSourceHook(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_DESTROY)
{
MessageBox.Show("I saw a WM_DESTROY!");
handled = true;
}
return IntPtr.Zero;
}
private const int WM_DESTROY = 0x0002;
}
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign in