Windows API:窗口保证收到的第一条消息是什么?

发布于 2024-08-11 03:49:55 字数 125 浏览 7 评论 0原文

我一直习惯于认为 WM_CREATE 是窗口收到的第一条消息。然而,当在顶级窗口上测试这个假设时,结果证明它是错误的。在我的测试中,WM_MINMAXINFO 作为第一条消息出现。

那么,窗口保证收到的第一条消息是什么?

I've been used to thinking that WM_CREATE is the first message a window receives. However, when testing this assumption on a top-level window, it turns out to be false. In my test, WM_MINMAXINFO turned up as the first message.

So, what is the first message a window is guaranteed to receive?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

迷乱花海 2024-08-18 03:49:55

WM_NCCREATE 实际上是第一条消息您的窗口将收到,该消息将在WM_CREATE之前到达。它与创建非客户区域(例如标题栏、系统菜单等)有关,因此有 NC 前缀。

WM_GETMINMAXINFO 在窗口大小之前发送 /position 已更改,并且可能在 WM_CREATE 之前到达(更多信息请参见下文)。

WM_CREATE 消息在 CreateWindow() 返回之前发送,因此您可以保证此时已执行每个窗口的初始化。在窗口创建之后、窗口变得可见之前 (WM_SHOWWINDOW),您的窗口过程将收到 WM_CREATE

实际上,MSDN 文档中有一个有趣的不一致之处 - 创建消息似乎取决于您是否调用 CreateWindow()CreateWindowEx(),但是它没有指定消息必须按分派顺序列出。

  • CreateWindow()WM_CREATEWM_GETMINMAXINFOWM_NCCREATE
  • CreateWindowEx()WM_NCCREATEWM_NCCALCSIZEWM_CREATE

我强烈怀疑 CreateWindow() 中描述的消息顺序应该有 WM_NCCREATE 首先,常规的 WM_CREATE 最后,这与通知文档和 CreateWindowEx() 参考一致(也与您所描述的一致)。

Raymond Chen 还提供了一些有关窗口创建/销毁的信息

它只是表明,即使看似简单的事情,你看得越多,也会变得复杂。

WM_NCCREATE is actually the very first message your window will receive, which will arrive before WM_CREATE. It is related to creating the non-client area (eg. title bar, system menu, etc), hence the NC prefix.

WM_GETMINMAXINFO is sent before the window size/position is changed, and may arrive before WM_CREATE (see below for more).

The WM_CREATE message is sent before CreateWindow() returns, so you can guarantee that per-window initialisation has been performed by that point. Your window proc will receive WM_CREATE after the window is created, but before the window becomes visible (WM_SHOWWINDOW).

Actually, there is an interesting inconsistency in the MSDN documentation - the creation messages seem to depend on whether you call CreateWindow() or CreateWindowEx(), however it does not specify that the messages are necessarily listed in order of dispatching.

  • CreateWindow(): WM_CREATE, WM_GETMINMAXINFO and WM_NCCREATE
  • CreateWindowEx(): WM_NCCREATE, WM_NCCALCSIZE, and WM_CREATE

I strongly suspect that the message order described in CreateWindow() should have WM_NCCREATE first, and the regular WM_CREATE last, which is consistent with the notification documentation and the CreateWindowEx() reference (and also consistent with what you describe).

Raymond Chen also has some interesting information on window creation/destruction.

It just goes to show, even seemingly simple things can get complex the more you look at them.

万劫不复 2024-08-18 03:49:55

你回答了你自己的问题。我也在 Windows XP SP3 上看到了 WM_GETMINMAXINFO,然后是 WM_NCCREATE、WM_NCCALCSIZE,最后是 WM_CREATE,然后 CreateWindowEx() 甚至返回了正在创建的窗口的句柄。什么垃圾?

一般的答案是,微软在对象的有序创建和销毁方面无能。他们对 Windows、COM 和设备驱动程序的理解都是错误的。总有一些第 22 条军规,对象被一半创建或一半销毁,需要一些迂回复杂的解决方案来生产可靠的产品。

You answered your own question. I too see WM_GETMINMAXINFO, on Windows XP SP3, followed by WM_NCCREATE, WM_NCCALCSIZE, and finally WM_CREATE before CreateWindowEx() has even returned the handle to the window being created. What garabage'

The general answer is that Microsoft is incompetent when it comes to orderly creation and destruction of objects. They get it wrong with windows, with COM, and with device drivers. There's always some catch-22 where an object is half-created or half-destroyed that requires some roundabout convoluted solution to produce a reliable product.

唱一曲作罢 2024-08-18 03:49:55

实验的结果比仅仅信任源代码要好,特别是因为源代码是由大量程序员编写的,而且没有人知道所有代码。也就是说:

我收到的第一条消息是 0x24 (WM_GETMINMAXINFO)。

我可以假设它永远是第一条消息吗?否,因为 Windows 版本之间的代码发生了变化,并且 Microsoft 尚未记录保证是第一个收到的消息。

底线:
不要假设 WM_CREATE 在另一条消息之前被调用。

Results by experimentation are better than just trusting the source, especially since the source is composed by a legion of programmers, and none know all the code. That said:

The fist message I receive is 0x24 (WM_GETMINMAXINFO).

Can I assume that it will always be the first message? No, since code change between versions of windows, and Microsoft has not documented a message guaranteed to be the first one received.

Bottom line:
Do not assume that WM_CREATE was called before another message.

不打扰别人 2024-08-18 03:49:55

您可以使用Visual Studio附带的spy++来查看应用程序或窗口启动时生成的消息。

You can use spy++ which comes with visual studio to see what messages are generated when the application or window is started.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文