::MessageBox does nothing in MFC AppWizard dialog-based app 
Author Message
 ::MessageBox does nothing in MFC AppWizard dialog-based app

Ok, I'm trying the following.
    I create an AppWizard MFC project with a dialog as main window.  No
fancy extras turned on, except 3d controls.
    I within my InitInstance, I call ::MessageBox(NULL, _T("FOO"),
_T("foo"), MB_OK) BEFORE doing anything with my CDialog-derived main window
class.  You see a message box as you'd expect.
    I then do the usual wizard-generated code that constructs the dialog,
sets the main window pointer, and calls DoModal().
    The dialog box comes up.  I have a button which calls MessageBox as
before, and again only this time passing the dialog's HWND as a parameter.
You see both message boxes, one after the other, as you'd expect.
(AfxMessageBox works in here too).
    After DoModal() exits, I call ::MessageBox(NULL, _T("FOO"), _T("foo"),
MB_OK) from within InitInstance but before I return FALSE.  Guess what?  No
message box!  It exits, however, with a code of 1, which is IDOK.
    Who pushed the button?  What's going on here anyway?

Platform:  WinNT 4.0 SP4 w/ Visual Studio 6 SP3.

Thanks.
And if possible, please Cc: replies to my e-mail.

Joel
--
To reply via e-mail, you know what will
need deleted from my address...



Sun, 18 Nov 2001 03:00:00 GMT  
 ::MessageBox does nothing in MFC AppWizard dialog-based app
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?

I hope there's someone out there who's not as mystified as I am...

Joel


Quote:
> Ok, I'm trying the following.
>     I create an AppWizard MFC project with a dialog as main window.  No
> fancy extras turned on, except 3d controls.
>     I within my InitInstance, I call ::MessageBox(NULL, _T("FOO"),
> _T("foo"), MB_OK) BEFORE doing anything with my CDialog-derived main
window
> class.  You see a message box as you'd expect.
>     I then do the usual wizard-generated code that constructs the dialog,
> sets the main window pointer, and calls DoModal().
>     The dialog box comes up.  I have a button which calls MessageBox as
> before, and again only this time passing the dialog's HWND as a parameter.
> You see both message boxes, one after the other, as you'd expect.
> (AfxMessageBox works in here too).
>     After DoModal() exits, I call ::MessageBox(NULL, _T("FOO"), _T("foo"),
> MB_OK) from within InitInstance but before I return FALSE.  Guess what?
No
> message box!  It exits, however, with a code of 1, which is IDOK.
>     Who pushed the button?  What's going on here anyway?

> Platform:  WinNT 4.0 SP4 w/ Visual Studio 6 SP3.

> Thanks.
> And if possible, please Cc: replies to my e-mail.

> Joel
> --
> To reply via e-mail, you know what will
> need deleted from my address...



Sun, 18 Nov 2001 03:00:00 GMT  
 ::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/



Sun, 18 Nov 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. How to hide a messagebox before it pops up, in a dialog based app

2. PBM: Message box not displayed after main dialog box is closed in MFC dialog-based app

3. cannot instantiate an activex control in mfc dialog based app

4. Problem using delete with dialog-based MFC app

5. Tell me where 2 find good transparency tutorial (mfc - dialog based apps)

6. hiding an mfc dialog based app

7. Help: How to send email from a MFC Dialog based App

8. I have an MFC Dialog based app...

9. dialog based MFC app. application file types

10. return a value when an MFC dialog-based app exits

11. Drawing in MFC dialog based app

12. MFC Dialog based app and SAPI 4.0

 

 
Powered by phpBB® Forum Software