What is the difference between WM_DESTROY and WM_NCDESTROY?

Raymond Chen

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

The difference is thatthe WM_DESTROY messageis 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_DESTROYbefore the child windows are destroyed,and it receives the WM_NCDESTROY message after they havebeen destroyed.

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

The WM_NCDESTROY is the last message your window willreceive (in the absence of weirdness), and it is therefore the bestplace to do “final cleanup”.This is whyour new scratch programwaits until WM_NCDESTROY to destroy its instancevariables.

These two destruction messages are paired with the analogousWM_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 thatif 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 thecorresponding WM_CREATE.

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

[Typos corrected, 9:30am]