The windows in a default dialog-based application flash and disappear (138681)



The information in this article applies to:

  • The Microsoft Foundation Classes (MFC), when used with:
    • Microsoft Visual C++, 32-bit Editions 2.0
    • Microsoft Visual C++, 32-bit Editions 2.1
    • Microsoft Visual C++, 32-bit Editions 2.2
    • Microsoft Visual C++, 32-bit Editions 4.0
    • Microsoft Visual C++, 32-bit Enterprise Edition 5.0
    • Microsoft Visual C++, 32-bit Enterprise Edition 6.0
    • Microsoft Visual C++, 32-bit Professional Edition 5.0
    • Microsoft Visual C++, 32-bit Professional Edition 6.0
    • Microsoft Visual C++, 32-bit Learning Edition 6.0
    • Microsoft Visual C++ .NET (2003)
    • Microsoft Visual C++ .NET (2002)
    • Microsoft Visual C++ 2005 Express Edition

This article was previously published under Q138681

SYMPTOMS

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

CAUSE

In an MFC application, when the main window that is associated with the application is destroyed, a WM_QUIT message is posted to the application. This message is posted by calling AfxPostQuitMessage from the CWnd::OnNcDestroy function for the main window. AfxPostQuitMessage calls the PostQuitMessage function. The PostQuitMessage function 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 you call the PostQuitMessage function will be immediately destroyed. The effect is that the window flashes for a brief moment and then disappears. If the DoModal function is called to display another modal dialog box, control returns immediately from this function.

RESOLUTION

To resolve this problem, 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 the CWnd::OnNcDestroy function for the dialog object. One way of doing this is to override the OnNcDestroy function 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 the PostQuitMessage function from being called when the dialog box in a default dialog-based application is dismissed.

Steps to resolve the 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();
             }

Modification Type:MajorLast Reviewed:12/9/2005
Keywords:kbtshoot kbcode kbDlg kbprb KB138681 kbAudDeveloper