9

I was using Application.AddMessageFilter() in my WinForms applications (when working with unmanaged code).

Now I'm switching to WPF and can't find this functionality.

Please advice where it can be found or implemented.

Valentin V
  • 24,971
  • 33
  • 103
  • 152
  • 1
    [http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/97cc207c-49a7-4a49-9fc1-fdf3b5d904d2/](http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/97cc207c-49a7-4a49-9fc1-fdf3b5d904d2/) looks like a solution /edit: similar question here: [http://stackoverflow.com/questions/476084/c-twain-interaction](http://stackoverflow.com/questions/476084/c-twain-interaction) – Sebastian Jan 26 '09 at 07:50

2 Answers2

6

In WPF, you can use ComponentDispatcher.ThreadFilterMessage event.

ComponentDispatcher.ThreadFilterMessage += ComponentDispatcher_ThreadFilterMessage;
private void ComponentDispatcher_ThreadFilterMessage(ref MSG msg, ref bool handled)
{
     if (msg.message == 513)//MOUSE_LEFTBUTTON_DOWN
     {
         //todo
     }
}
Jim
  • 489
  • 5
  • 11
1

If you want to monitor a window message, you can use HwndSource.AddHook method. The following example shows how to use Hwnd.AddHook method. If you want to monitor a application scope message, you can try to use ComponentDispatcher class.

private void Button_Click(object sender, RoutedEventArgs e)
{
    Window wnd = new Window();
    wnd.Loaded += delegate
    {
        HwndSource source = (HwndSource)PresentationSource.FromDependencyObject(wnd);
        source.AddHook(WindowProc);
    };
    wnd.Show();
}
private static IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
}
LordWilmore
  • 2,829
  • 2
  • 25
  • 30