English 中文(简体)
Windows API: What is the first message a window is guaranteed to receive?
原标题:

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?

最佳回答

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.

问题回答

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.

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.

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





相关问题
How to read exact number of bytes from a stream (tcp) socket?

In winsock, both the sync recv and the async WSARecv complete as soon as there is data available in a stream socket, regardless of the size specified (which is only the upper limit). This means that ...

AcquireCredentialsHandle returns SEC_E_NO_CREDENTIALS

I created a self-signed certificate (created using OpenSSL) and installed it into the Certificate Store using the Certificates MMC snap-in (CertMgr.msc) on Windows Vista Ultimate. I have managed to ...

Calling Win32 EnumThreadWindows() in C#

I m trying to get a call to EnumThreadWindows working, but I always get a Wrong Parameter-Error, although my code is nearly the same as this example on pinvoke.net. I don t know why this doesn t work: ...

COM Basic links

folks can you provide me the tutorial link or .pdf for learning basic COM?. i do google it.. still i recommend answers of stackoverflow so please pass me.. Thanks

Handling multiple windows WIN32 API

HI I m trying to create an application in the Win32 environment containing more than one window. How do i do that? all the Win32 Tutorials on web i found only showed how to manage one window. How do i ...

Creating a thread in DllMain?

It seems that when a thread is created from within DllMain upon DLL_PROCESS_ATTACH it won t begin until all dll s have been loaded. Since I need to make sure the thread runs before I continue, I get a ...

热门标签