PRB: Windows Flash and Disappear in Dialog-Based Applications

ID: Q138681


The information in this article applies to:
  • The Microsoft Foundation Classes (MFC), included with:
    • Microsoft Visual C++, 32-bit Editions, versions 2.0, 2.1, 2.2, 4.0, 5.0, 6.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.

Steps to Resolve Behavior

  1. Place a message map entry for the WM_NCDESTROY message in the CMyDialog class implementation file.
    
             ON_WM_NCDESTROY() 


  2. Add the OnNcDestory member function to the CMyDialog class and set the m_pMainWnd member variable of the application object to NULL.
    
             void CMyDialog::OnNcDestroy()
             {
                AfxGetApp()->m_pMainWnd = NULL;
                CDialog::OnNcDestroy();
             } 


Additional query words: messagebox

Keywords : kbcode kbMFC kbVC200 kbVC210 kbVC220 kbVC400 kbVC500 kbVC600
Version : Winnt:2.0, 2.1, 2.2, 4.0, 5.0, 6.0
Platform : winnt
Issue type : kbprb


Last Reviewed: July 27, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.