【发布时间】:2010-10-03 12:06:39
【问题描述】:
我在我的WinForms 应用程序中使用了Application.AddMessageFilter()(使用非托管代码时)。
现在我切换到WPF,但找不到此功能。
请告知在哪里可以找到或实施它。
【问题讨论】:
标签: wpf windows winapi interop
我在我的WinForms 应用程序中使用了Application.AddMessageFilter()(使用非托管代码时)。
现在我切换到WPF,但找不到此功能。
请告知在哪里可以找到或实施它。
【问题讨论】:
标签: wpf windows winapi interop
在 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
}
}
【讨论】:
如果你想监听一个窗口消息,你可以使用 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)
{
}
【讨论】: