windows 使用 WM_USER、WM_APP 或 RegisterWindowMessage

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4406909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:46:44  来源:igfitidea点击:

Using WM_USER, WM_APP or RegisterWindowMessage

c++windowsmfc

提问by Cheok Yan Cheng

Currently, I have a Windows EXE application, with several loaded DLLs. DLLs need to communicate with my windows application through PostMessageand SendMessage.

目前,我有一个 Windows EXE 应用程序,其中包含多个加载的 DLL。DLL 需要通过PostMessage和与我的 Windows 应用程序进行通信SendMessage

The Windows EXE application + DLLs are all within a single process.

Windows EXE 应用程序 + DLL 都在一个进程中。

The message should be private among EXE and DLLs.

该消息在 EXE 和 DLL 之间应该是私有的。

I was wondering, should I use

我想知道,我应该使用

 - WM_USER based message
 - WM_APP based message
 - RegisterWindowMessage

and why?

为什么?

What happen if there is an external process (another exe), trying to FindWindow of my Windows application, and send the message with same ID?

如果有一个外部进程(另一个 exe)尝试查找我的 Windows 应用程序的 FindWindow,并使用相同的 ID 发送消息,会发生什么?

I wish not to respond, as I am only interested message from DLLs within my own process.

我不想回复,因为我只对来自我自己进程中的 DLL 的消息感兴趣。

回答by Chris Becke

WM_USER messages are typically used to implement control specific messages when developing a control. If you had developed an image editing control, and needed to allow users of the control to set the image, you might go:

WM_USER 消息通常用于在开发控件时实现特定于控件的消息。如果你开发了一个图像编辑控件,并且需要允许控件的用户设置图像,你可能会去:

#define IECM_SETIMAGE    (WM_USER+1) // image editor control message.

WM_APP messages are typically used to implement application level logic. If you want to send your application a specific message to perform an action...

WM_APP 消息通常用于实现应用程序级逻辑。如果要向应用程序发送特定消息以执行操作...

#define IEAM_SHOWTOOLBAR   (WM_APP+1) // image editor app message

Having both WM_APP and WM_USER ranges seems a little redundant - however there are two use cases where having two ranges is necessary:

同时拥有 WM_APP 和 WM_USER 范围似乎有点多余 - 但是有两个用例需要有两个范围:

  • it is possible to create a top level window from a control by simply making it overlapped or popup and giving it a menu and frame. It would then need to both respond as a control, and an application frame window.
  • Applications can subclass controls, and then use the WM_APP channel for sending application defined messages to the controls without conflicting with the controls normal WM_USER range of messages.
  • 通过简单地使其重叠或弹出并为其提供菜单和框架,可以从控件创建顶级窗口。然后它需要既作为控件响应,又作为应用程序框架窗口响应。
  • 应用程序可以子类化控件,然后使用 WM_APP 通道向控件发送应用程序定义的消息,而不会与控件正常的 WM_USER 消息范围发生冲突。

RegisterWindowMessage is used to create messages when you need a unique message id that is system wide - typically because you want to broadcast the message to windows that are not under your own control, and hence have their own meanings for messages IDs in the WM_APP and WM_USER ranges.

当您需要系统范围内的唯一消息 ID 时,RegisterWindowMessage 用于创建消息 - 通常是因为您想将消息广播到不受您自己控制的窗口,因此对于 WM_APP 和 WM_USER 中的消息 ID 具有自己的含义范围。