PRB: Windows Flash and Disappear in Dialog-Based Applications

Last reviewed: December 11, 1997
Article ID: Q138681
The information in this article applies to:
  • The Microsoft Foundation Classes (MFC) included with: Microsoft Visual C++, 32-bit Edition, versions 2.0, 2.1, 2.2, 4.0, 5.0

SYMPTOMS

In a default dialog-based application, windows created after returning from DoModal flash and disappear.

CAUSE

In an MFC application when the main window associated with the application is destroyed, a WM_QUIT message is posted to the application. This message is posted by calling AfxPostQuitMessage from CWnd::OnNcDestroy for the main window. AfxPostQuitMessage calls PostQuitMessage, which performs some processing and then posts the WM_QUIT message to the application.

The PostQuitMessage function indicates to Windows that a thread has made a request to terminate. Any window that is created after calling the PostQuitMessage function will be immediately destroyed. The effect is that the window flashes for a brief moment and then disappears. If DoModal is called to display another modal dialog box, control returns immediately from this function.

RESOLUTION

Change the line that sets the m_pMainWnd to point to dialog object, into a comment. Or set m_pMainWnd for the CWinApp-derived object to NULL before control gets to CWnd::OnNcDestroy for the dialog object. One way of doing this is to override OnNcDestroy for the CDialog-derived object. In the overridden function, set the m_pMainWnd to NULL before calling the base class.

STATUS

This behavior is by design.

MORE INFORMATION

The following code can be implemented in the CDialog-derived class to prevent PostQuitMessage() from being called when the dialog box in a default dialog-based application is dismissed.

Sample Code to Resolve Behavior

   void CMyDialog::OnNcDestroy()
   {
      AfxGetApp()->m_pMainWnd = NULL;
      CDialog::OnNcDestroy();
   }


Additional query words: messagebox
Keywords : MfcMisc kbcode
Technology : kbMfc
Version : Winnt:2.0, 2.1, 2.2, 4.0, 5.0
Platform : winnt
Issue type : kbprb


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: December 11, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.