2009-01-26 95 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 
    } 
}