如何在 WPF 中处理 WndProc 消息?

编程入门 行业动态 更新时间:2024-10-11 19:24:56
本文介绍了如何在 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?

推荐答案

实际上,据我所知,使用 HwndSource 和 HwndSourceHook 在 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 消息?

本文发布于:2023-11-11 05:27:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1577505.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:消息   如何在   WPF   WndProc

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!