CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 1999
    Posts
    4

    AfxMessageBox doesn't work!

    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!

  2. #2
    Join Date
    Apr 1999
    Posts
    11

    Re: AfxMessageBox doesn't work!

    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


  3. #3
    Join Date
    May 1999
    Location
    Atlanta, GA, USA
    Posts
    443

    Re: Use the debugger

    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-


  4. #4
    Guest

    Re: AfxMessageBox doesn't work!

    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


  5. #5
    Guest

    Re: Use the debugger

    Try this:
    make your modal dlg into modeless dlg. The messagebox should popped up


  6. #6
    Guest

    Re: AfxMessageBox doesn't work!

    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.


  7. #7
    Join Date
    Apr 1999
    Posts
    6

    Re: AfxMessageBox doesn't work!

    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)

    ô¿ô
    «=»

  8. #8
    Join Date
    May 1999
    Location
    Arizona, U.S.A.
    Posts
    101

    Re: AfxMessageBox doesn't work!

    All you need to do it is comment out this line: m_pMainWnd = &dlg;
    and AfxMessageBox will work!

    Everything is Free Until You Have to Pay for it....

    Platform is Windows 2000/XP Professional, Visual C++ 6.0

  9. #9
    Join Date
    Mar 2001
    Location
    Singapore
    Posts
    107

    Re: AfxMessageBox doesn't work!

    how come commenting out m_pmainwnd=&dlg will cause AfxMessageBox to work???/
    explain...if so what is the statement used for???


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured