The MFC library directly supports hundreds of Windows
message-handling functions. In addition, you can define your own messages. You will see
plenty of message-handling examples in later chapters, including handlers for
menu items, child window controls, and so forth. In the meantime, five special
Windows messages deserve special attention: WM_CREATE, WM_CLOSE,
WM_QUERYENDSESSION, WM_DESTROY, and WM_NCDESTROY.
The WM_CREATE Message
This is the first message that Windows sends to a view. It is sent when the
window's Create function is called by the framework, so the window creation is
not finished and the window is not visible. Therefore, your
OnCreate handler cannot call Windows functions that depend on the window being completely
alive. You can call such functions in an overridden
OnInitialUpdate function, but you must be aware that in an SDI application
OnInitialUpdate can be called more than once in a view's lifetime.
The WM_CLOSE Message
Windows sends the WM_CLOSE message when the user closes a window from the system menu and when a parent window is closed. If you implement the OnClose message map function in your derived view class, you can control the closing process. If, for example, you need to prompt the user to save changes to a file, you do it in OnClose. Only when you have determined that it is safe to close the window do you call the base class OnClose function, which continues the close process. The view object and the corresponding window are both still active.
When you're using the full application framework, you probably won't use the WM_CLOSE message handler. You can override the CDocument::SaveModified virtual function instead, as part of the application framework's highly structured program exit procedure.
Windows sends the WM_QUERYENDSESSION message to all running
applications when the user exits Windows. The OnQueryEndSession
message map function handles it. If you write a handler for WM_CLOSE, write one for
WM_QUERYENDSESSION too.
The WM_DESTROY Message
Windows sends this message after the WM_CLOSE message, and the
OnDestroy message map function handles it. When your program receives this message,
it should assume that the view window is no longer visible on the screen but
that it is still active and its child windows are still active. Use this message
handler to do cleanup that depends on the existence of the underlying window. Be
sure to call the base class OnDestroy function. You cannot "abort" the window
destruction process in your view's
OnDestroy function. OnClose is the place to do that.
The WM_NCDESTROY Message
This is the last message that Windows sends when the window is being destroyed. All child windows have already been destroyed. You can do final processing in OnNcDestroy that doesn't depend on a window being active. Be sure to call the base class OnNcDestroy function.
Do not try to destroy a dynamically allocated window object in OnNcDestroy. That job is reserved for a special CWnd virtual function, PostNcDestroy, that the base class OnNcDestroy calls. MFC Technical Note #17 in the online documentation gives hints on when it's appropriate to destroy a window object.