What is the difference between WM_DESTROY and WM_NCDESTROY?

Raymond Chen

There are two window messages closely-associated with window destruction, the WM_DESTROY message and the WM_NCDESTROY message. What’s the difference?

The difference is that the WM_DESTROY message is sent at the start of the window destruction sequence, whereas the WM_NCDESTROY message is sent at the end. This is an important distinction when you have child windows. If you have a parent window with a child window, then the message traffic (in the absence of weirdness) will go like this:

hwnd = parent, uMsg = WM_DESTROY
hwnd = child, uMsg = WM_DESTROY
hwnd = child, uMsg = WM_NCDESTROY
hwnd = parent, uMsg = WM_NCDESTROY

Notice that the parent receives the WM_DESTROY before the child windows are destroyed, and it receives the WM_NCDESTROY message after they have been destroyed.

Having two destruction messages, one sent top-down and the other bottom-up, means that you can perform clean-up appropriate to a particular model when handling the corresponding message. If there is something that must be cleaned up top-down, then you can use the WM_DESTROY message, for example.

The WM_NCDESTROY is the last message your window will receive (in the absence of weirdness), and it is therefore the best place to do “final cleanup”. This is why our new scratch program waits until WM_NCDESTROY to destroy its instance variables.

These two destruction messages are paired with the analogous WM_CREATE and WM_NCCREATE messages. Just as WM_NCDESTROY is the last message your window receives, the WM_NCCREATE message is the first message, so that’s a good place to create your instance variables. Note also that if you cause the WM_NCCREATE message to return failure, then all you will get is WM_NCDESTROY; there will be no WM_DESTROY since you never got the corresponding WM_CREATE.

What’s this “absence of weirdness” I keep alluding to? We’ll look at that next time.

[Typos corrected, 9:30am]

0 comments

Discussion is closed.

Feedback usabilla icon