WPF等效于Application.AddMessageFilter(Windows窗体)

问题描述 投票:7回答:2

我在Application.AddMessageFilter()应用程序中使用WinForms(使用非托管代码时)。

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

请告知可以找到或实施的地方。

wpf windows winapi interop
2个回答
4
投票

在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
     }
}

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)
{
}
© www.soinside.com 2019 - 2024. All rights reserved.