Microsoft KB Archive/138681

= 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++ 2.1

 Microsoft Visual C++ 2.2

 Microsoft Visual C++ 4.0 Standard Edition</li></ul>

 Microsoft Visual C++ 5.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 6.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 5.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Standard Edition</li></ul>

 Microsoft Visual C++ .NET 2003 Standard Edition</li></ul>

 Microsoft Visual C++ .NET 2002 Standard Edition</li></ul>

 Microsoft Visual C++ 2005 Express Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q138681

<div class="symptoms_section">

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

<div class="cause_section">

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.

<div class="resolution_section">

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.

<div class="status_section">

STATUS
This behavior is by design.

<div class="moreinformation_section">

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
<ol> <li> Place a message map entry for the WM_NCDESTROY message in the CMyDialog class implementation file. ON_WM_NCDESTROY </li> <li> 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; } </li></ol>

Additional query words: messagebox

Keywords: kbtshoot kbcode kbdlg kbprb KB138681

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.