locked
AfxMessageBox position question RRS feed

  • Question

  • Hi..

    I have a modal dialog with two buttons on it.

    One button handler calls a function that pops up an AfxMessageBox and it shows up centered on the dialog.

    The other handler starts a thread that uses a callback within the dialog class that calls the same function that pops up an AfxMessageBox, yet it gets centered to the desktop rather than to the dialog.

    I can't seem to set the focus to the dialog from the thread.. even though its running in a callback.

    Any idea of how to get the message box to always center on the dialog?

    Thanks

    Andy

     

    Monday, October 3, 2011 9:48 PM

Answers

  • >The other handler starts a thread that uses a callback within the dialog class that calls the same function that pops up an AfxMessageBox, yet it gets centered to the desktop rather than to the dialog.

    Andy,

    You're hitting problems because you're doing UI from another thread.
    Although your current problem appears minor, you'll have all manner of
    difficulties should you progress down this line.

    Keep your life simple and trouble free and keep all your UI in the
    single thread.

    Dave

    • Marked as answer by _Andy__ Tuesday, October 4, 2011 3:32 PM
    Tuesday, October 4, 2011 8:21 AM

All replies

  •  

    You can use ::MessageBox(), Set HWND parameter to change the parent of the messagebox. 

     

     


    Knowledge is like light; It spreads only when you have clear and transparent mind.
    • Edited by D.Sreedhar Tuesday, October 4, 2011 9:45 AM
    Tuesday, October 4, 2011 5:27 AM
  • >The other handler starts a thread that uses a callback within the dialog class that calls the same function that pops up an AfxMessageBox, yet it gets centered to the desktop rather than to the dialog.

    Andy,

    You're hitting problems because you're doing UI from another thread.
    Although your current problem appears minor, you'll have all manner of
    difficulties should you progress down this line.

    Keep your life simple and trouble free and keep all your UI in the
    single thread.

    Dave

    • Marked as answer by _Andy__ Tuesday, October 4, 2011 3:32 PM
    Tuesday, October 4, 2011 8:21 AM
  • Yep - you're right.. was hoping that wasn't it since I was just using the AfxMessageBox..

    It's not pretty, but the following allowed a quick patch. Just added a windows message handler.

     

     
     
    ON_MESSAGE(MSG_MY_MESSAGE_BOX, OnMsgMyMessageBox )
    
    {
        // message is just a CString...
        char *pTemp = new char[message.GetLength()+1];
        strcpy_s( pTemp, (message.GetLength()+1), message );
        PostMessageA(MSG_MY_MESSAGE_BOX, (WPARAM)pTemp );
    }
    
    LRESULT MyDlg::OnMsgMyMessageBox(WPARAM wParam, LPARAM lParam)
    {
        AfxMessageBox( (char *)wParam );
        delete [] (char*)wParam;
        return(0);
    }
    
    



     

    Tuesday, October 4, 2011 3:31 PM