
::MessageBox does nothing in MFC AppWizard dialog-based app
Quote:
>Ok, maybe I posted a little too soon, but perhaps not. I found and fixed
>the problem but have a new question: WHY?
>The fix: comment out the App-Wizard generated line where it does this:
> //m_pMainWnd = &dlg;
>What on earth did MFC do to the poor app that caused ::MessageBox to fail
>after dlg.DoModal() completed??? Why did my fix work?
Welcome to The Magic of Windows.
The window pointed to by an app's "m_pMainWnd" is special. If you look
in WINCORE.CPP, you'll see the CWnd::OnNcDestroy() handler, which gets called
as the last step when a window gets destroyed. It checks to see whether the
window being destroyed is the one pointed to by m_pMainWnd. If it is, then
it's presumably time for the app to quit (since it's generally Very Bad Form
for a process to keep running when its main window is gone), so the
OnNcDestroy handler posts a quit message, signalling that the app should stop.
How is this messing up your MessageBox? Well, once an app has started
the process of quitting, any attempts by that app to create new windows will
fail. Likewise, any attempt to call MessageBox will fail to actually create
a dialog; you'll simply get back a default return code.
There are two ways to solve this problem:
*Move your user-interface code (like this MessageBox) to places that
will be called before the app starts to quit. For example, you might display
the MessageBox from the dialog's OnDestroy handler.
*Just create your own pointer to hold the dialog; don't store it in
m_pMainWnd. This will disable the automatic app-killing logic. It's now
your responsibility to make sure that you terminate the app correctly,
probably by calling AfxPostQuitMessage yourself after displaying the
MessageBox.
--
\o\ If you're interested in books and stories with transformation themes, \o\
/o/ please have a look at <URL:http://www.halcyon.com/phaedrus>. Thanks! /o/
\o\ FC1.21:FC(W/C)p6arw A- C->++ D>++ H+ M>+ P R T++++ W** Z+ Sm RLCT \o\
/o/ a cmn++++$ d e++ f+++ h- i++wf p-- sm# /o/