windows 在 win32 中创建自定义消息类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1816498/
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 13:30:27  来源:igfitidea点击:

Creating custom message types in win32?

c++cwindowswinapi

提问by BeeBand

Is there a way to define and send custom message types in Win32, to be caught by your Main message handler? For example, my main message handler captures messages such as WM_PAINT, WM_RESIZE, WM_LBUTTONDOWN etc. Can I create my own WM_DOSOMETHING? If so, how would I send this message?

有没有办法在 Win32 中定义和发送自定义消息类型,以被您的主消息处理程序捕获?例如,我的主消息处理程序捕获诸如 WM_PAINT、WM_RESIZE、WM_LBUTTONDOWN 等消息。我可以创建自己的 WM_DOSOMETHING 吗?如果是这样,我将如何发送此消息?

Ah, I actually just discovered this was asked before here, however, it doesn't answer how I would actually send this message.

啊,我实际上刚刚发现这里之前有人问过这个问题,但是,它并没有回答我实际上将如何发送此消息。

回答by Bob Moore

Woah, let's just stop and think here...

哇,让我们停下来想一想......

First of all, Windows itself sends messages in the WM_USER+nrange, that's why WM_APPwas invented (I found this out the hard way). But it gets worse... there's nothing to stop badly behaved applications broadcastingWM_USER+nor WM_APP+nmessages, and because human beings stole the crystal of infinite stupidity from the Gods, this does indeed happen in the real world.

首先,Windows 本身在WM_USER+n范围内发送消息,这就是WM_APP发明的原因(我发现这一点很困难)。但更糟的是……没有什么可以阻止不良应用程序的广播WM_USER+nWM_APP+n消息,而且由于人类从众神那里偷走了无限愚蠢的水晶,这确实发生在现实世界中。

So, repeat after me, the only safe message is one I define myself and can only see myself. Use RegisterWindowMessage. And even then, be untrusting. When I need a string to define a RegisterWindowMessage, I use GUIDGEN to create the string and put a human-readable app-specific prefix on the resulting gobbledygook to help me differentiate multiple messages in the code.

所以,跟我重复一遍,唯一安全的信息是我定义自己并且只能看到我自己。使用注册窗口消息。即便如此,也要不信任。当我需要一个字符串来定义 RegisterWindowMessage 时,我使用 GUIDGEN 创建该字符串并在生成的 gobbledygook 上放置一个人类可读的应用程序特定前缀,以帮助我区分代码中的多个消息。

Bet on the stupidity of your fellow humans - it's always a winning bet.

赌你的人类同胞的愚蠢 - 这总是一个获胜的赌注。

If you want authoritative background on this whole topic, see here. No that's not my website, it's Joe Newcomer's.

如果您想了解整个主题的权威背景,请参阅此处。不,那不是我的网站,而是 Joe Newcomer 的。

回答by itowlson

Yes. Just declare a constant in the WM_USER range e.g.

是的。只需在 WM_USER 范围内声明一个常量,例如

#define WM_RETICULATE_SPLINES (WM_USER + 0x0001)

You can also register a message by name using the RegisterWindowMessage API.

您还可以使用 RegisterWindowMessage API 按名称注册消息。

You can then send these messages using SendMessage, PostMessage or any of their variants.

然后,您可以使用 SendMessage、PostMessage 或它们的任何变体发送这些消息。

回答by Anders

  • If you created the window class, you can use the WM_USERrange (or WM_APP)
  • If it is not your class, you can use WM_APP
  • If you want to broadcast the message to every top level window, register your own global message with RegisterWindowMessage
  • 如果您创建了窗口,则可以使用WM_USER范围(或WM_APP
  • 如果不是你的班级,你可以使用 WM_APP
  • 如果要将消息广播到每个顶级窗口,请使用 RegisterWindowMessage 注册您自己的全局消息