DESTROYING WINDOWS

(see MFC tech note 17)

Call DestroyWindow WM_CLOSE calls destroy window (could put a confirm in here then call destroywindow) DestroyWindow calls WM_DESTROY to destroy the client then the children Last message sent to a window being destroyed is WM_NCDESTROY The default handler OnNcDestroy will detach the HWND from the C++ object and call PostNcDestroy virtual function. This "non client" destroy can be used to delete the allocated memory object associated with the window. The children should clean up all non autoclean up objects from the heap. Classes derived from CFramWnd are auto cleanup. Do NOT in CHelloApp::~CHelloApp() call delete m_pMainWnd; Not needed it will be NULL by now !! You may see it when a PostQuitMessage has been issued to quit the app before the windows are destroyed. Then DestroyWindow called in the CWnd::~CWnd and the VTBL will not point to the correct derived. Then the main window and object will be destroyed and any derived but any OnDestroy or PostNcDestroy in the main derived class (CHelloWindow eg) will not be called!! The PostQuitMessage should only be called in response to a WM_DESTROY message, this posts a quit message WM_QUIT which causes GETMESSAGE to return 0 and the app ends. The exception to this is when MFC has been faked out with an incorrect m_hMainWnd then DestroyWindow(); // Probably a dialog window ::PostQuitMessage(0); // Exit application