The windows in a default dialog-based application flash and disappear
Article ID: 138681
Article Last Modified on 11/21/2006
APPLIES TO
- Microsoft Foundation Class Library 4.2, when used with:
- Microsoft Visual C++ 2.0 Professional Edition
- Microsoft Visual C++ 4.0 Standard Edition
- Microsoft Visual C++ 5.0 Enterprise Edition
- Microsoft Visual C++ 6.0 Enterprise Edition
- Microsoft Visual C++ 5.0 Professional Edition
- Microsoft Visual C++ 6.0 Professional Edition
- Microsoft Visual C++ 6.0 Standard Edition
- Microsoft Visual C++ .NET 2003 Standard Edition
- Microsoft Visual C++ .NET 2002 Standard Edition
- 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.
Additional query words: messagebox
Keywords: kbtshoot kbcode kbdlg kbprb KB138681