如何在 WPF 中处理 WndProc 消息? [英] How to handle WndProc messages in WPF?

查看:28
本文介绍了如何在 WPF 中处理 WndProc 消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Windows 窗体中,我只需覆盖 WndProc,并在消息传入时开始处理.

In Windows Forms, I'd just override WndProc, and start handling messages as they came in.

有人可以向我展示如何在 WPF 中实现相同功能的示例吗?

Can someone show me an example of how to achieve the same thing in WPF?

推荐答案

实际上,据我所知,使用 HwndSourceHwndSourceHook 在 WPF 中确实可以实现这样的事情.请参阅 MSDN 上的此主题 为例.(相关代码如下)

Actually, as far as I understand such a thing is indeed possible in WPF using HwndSource and HwndSourceHook. See this thread on MSDN as an example. (Relevant code included below)

// 'this' is a Window
HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
source.AddHook(new HwndSourceHook(WndProc));

private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
    //  do stuff

    return IntPtr.Zero;
}

现在,我不太确定您为什么要在 WPF 应用程序中处理 Windows Messaging 消息(除非它是与另一个 WinForms 应用程序一起工作的最明显的互操作形式).WPF 中的设计思想和 API 的性质与 WinForms 非常不同,因此我建议您更多地熟悉 WPF,以确切了解为什么没有等效的 WndProc.

Now, I'm not quite sure why you'd want to handle Windows Messaging messages in a WPF application (unless it's the most obvious form of interop for working with another WinForms app). The design ideology and the nature of the API is very different in WPF from WinForms, so I would suggest you just familiarise yourself with WPF more to see exactly why there is no equivalent of WndProc.

这篇关于如何在 WPF 中处理 WndProc 消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆