2009-01-26 97 views
6

我在我的WinForms应用程序中使用Application.AddMessageFilter()(使用非托管代码时)。WPF相当于Application.AddMessageFilter(Windows窗体)

现在我切换到WPF并且无法找到此功能。

请指教哪里可以找到或实施它。

+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 /)看起来像一个解决方案/编辑: 类似的问题在这里:[http://stackoverflow.com/questions/476084/c -twain相互作用](http://stackoverflow.com/questions/476084/c-twain-interaction) – Sebastian 2009-01-26 07:50:00

回答

0

如果要监视窗口消息,可以使用HwndSource.AddHook方法。以下示例显示如何使用Hwnd.AddHook方法。如果您想监视应用程序范围消息,则可以尝试使用ComponentDispatcher类。

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) 
{ 
} 
1

在WPF中,你可以使用ComponentDispatcher.ThreadFilterMessage事件。

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