Click to See Complete Forum and Search --> : AfxMessageBox doesn't work!
Mark Roberts
April 7th, 1999, 03:49 PM
Greetings:
I have found a strange problem which can be reproduced very easily:
1. Create an MFC Dialog Based Application. Select all default settings.
2. In the main source file you will find that AppWizard had declared an instance of your dialog and then calls dlg.DoModal().
3. Immediately after the call to DoModal, place a simple call to AfxMessageBox. Use simple parameters: AfxMessageBox("Hello", MB_OK)
4. Run this program. You will find that the message box will NOT appear after you click on OK for the Dialog box. If you move the AfxMessageBox call to just before DoModal, it works. But not after.
Does anybody know why this is? Can anybody tell me how I can issue message boxes after DoModal has returned? I have also tried the standard MessageBox routine with the same results.
Thank You!
Mark Roberts
Toronto, Canada
Soylent green is made out of people!
Allen Yuh
April 7th, 1999, 04:17 PM
Yes, it's not working. I think because there is no CWnd object to be the parent of the MessageBox dialog. You can create your own messagebox to display the message after the DoModal. hope this will help.
Allen
Masaaki
April 7th, 1999, 04:49 PM
Hi.
If you put the breakpoint at the line of AfxMessageBox(),
you may find something.
That is, AfxMessageBox line can't be reached after DoModal()?
DoModal() may finish its execution?
If you put MessageBox inside OnInitDialog, you can show
MessageBox.
Hope for help.
-Masaaki Onishi-
April 7th, 1999, 07:44 PM
That's because AFXMessageBox reflies on the main window to produce the message box. It basically does this -> MessageBox(AfxGetMainWnd(),text,...); Since you place the AfxMessageBox after the DoModal(), the AfxGetMainWnd() will return a bad CWnd, since the dialog box is closed and destroyed. Just use a MessageBox(NULL,text,...); to do your box
April 8th, 1999, 09:50 AM
Try this:
make your modal dlg into modeless dlg. The messagebox should popped up
April 8th, 1999, 12:51 PM
Hey,
It works even if it placed before instantiating the dialog. Here is my code:
...
AfxMessageBox("hi");
CDlgtestDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
...
By that time I am calling msg box, m_pMainWnd is null because it's nothing but the handle to the dialog object. Even I am surprised to see this working.. Can u tell me why?
Regards,
Rangaraya Sharma.
James A. Johnson
April 17th, 1999, 09:35 PM
The reason it doesn't work is because there is no CWnd* object for the message box
to refer to. Usually, I put all critical messages for the start of the program in the
OnPaint function...(ie. openning files, initializing constructs and memory) that way,
if anything goes wrong, you can send out a message to the user using
AfxMessageBox() and then destroy the window.
If you want the MessageBox to show with no window, SW_HIDE the main window
in the OnPaint() function and call the MessageBox. It works for me.
The only other way to do it is to grab the HANDLE for the Windows Desktop and
let the AfxMessageBox refer to it. I don't recommend or endorse doing that though,
it can have strange side effects if done incorrectly...(like the screen goes blank and
the three finger salute to windows wont work...so...you are forced to push the
button...heh heh)
τΏτ
«=»
Charlie Curtis
December 28th, 1999, 12:46 PM
All you need to do it is comment out this line: m_pMainWnd = &dlg;
and AfxMessageBox will work!
mac_hjc
July 2nd, 2001, 10:06 AM
how come commenting out m_pmainwnd=&dlg will cause AfxMessageBox to work???/
explain...if so what is the statement used for???
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.