2

Using Visual Studio C++ with MFC. How do I center a MessageBox to it's parent window? Currently it centers to the desktop.

3
  • 2
    Use PostMessage() before the MessageBox call. In your message handler, use EnumWindows to find the message box back and MoveWindow where you want it. Jun 9, 2011 at 22:17
  • The message box has class #32770 which should help you find it Jun 9, 2011 at 22:21
  • There is a way to change the location, but its way too complicated for such a small task. Just create your own CDialog.
    – Tom Tom
    Apr 16, 2019 at 9:06

4 Answers 4

4

You need to install a hook and change the dialog box position on creation.

int MessageBoxCentered(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType)
{
    // Center message box at its parent window
    static HHOOK hHookCBT{};
    hHookCBT = SetWindowsHookEx(WH_CBT,
        [](int nCode, WPARAM wParam, LPARAM lParam) -> LRESULT
        {
            if (nCode == HCBT_CREATEWND)
            {
                if (((LPCBT_CREATEWND)lParam)->lpcs->lpszClass == (LPWSTR)(ATOM)32770)  // #32770 = dialog box class
                {
                    RECT rcParent{};
                    GetWindowRect(((LPCBT_CREATEWND)lParam)->lpcs->hwndParent, &rcParent);
                    ((LPCBT_CREATEWND)lParam)->lpcs->x = rcParent.left + ((rcParent.right - rcParent.left) - ((LPCBT_CREATEWND)lParam)->lpcs->cx) / 2;
                    ((LPCBT_CREATEWND)lParam)->lpcs->y = rcParent.top + ((rcParent.bottom - rcParent.top) - ((LPCBT_CREATEWND)lParam)->lpcs->cy) / 2;
                }
            }

            return CallNextHookEx(hHookCBT, nCode, wParam, lParam);
        },
        0, GetCurrentThreadId());

    int iRet{ MessageBox(hWnd, lpText, lpCaption, uType) };

    UnhookWindowsHookEx(hHookCBT);

    return iRet;
}
3

::AfxMessageBox() appears on the center of the MainFrame for me. Which is basically a call to ::MessageBox() with a handle to the MainFrame as the first parameter. Isn't that working for you?

1
  • Doesn't work for me. It's possible it may be centering to the "MainFrame" of the application but I need it to center over my CDialog-derived dialog (which is where I'm calling MessageBox/AfxMessageBox from).
    – User
    Jun 14, 2011 at 0:23
0

You can't. That's why a lot of people write their own MessageBox classes.

0

Who said "can't"?

Try this:

This is for Win32 API, written in C. Translate it as you need...

case WM_NOTIFY:{
  HWND X=FindWindow("#32770",NULL);
  if(GetParent(X)==H_frame){int Px,Py,Sx,Sy; RECT R1,R2;
    GetWindowRect(hwnd,&R1); GetWindowRect(X,&R2);
    Sx=R2.right-R2.left,Px=R1.left+(R1.right-R1.left)/2-Sx/2;
    Sy=R2.bottom-R2.top,Py=R1.top+(R1.bottom-R1.top)/2-Sy/2;
    MoveWindow(X,Px,Py,Sx,Sy,1);
  }
} break;

Add that to the WndProc code... You can set position as you like, in this case it just centres over the main program window. It will do this for any messagebox, or file open/save dialog, and likely some other native controls. I'm not sure, but I think you may need to include COMMCTRL or COMMDLG to use this, at least, you will if you want open/save dialogs.

I experimented with looking at the notify codes and hwndFrom of NMHDR, then decided it was just as effective, and far easier, not to. If you really want to be very specific, tell FindWindow to look for a unique caption (title) you give to the window you want it to find.

This fires before the messagebox is drawn onscreen, so if you set a global flag to indicate when action is done by your code, and look for a unique caption, you be sure that actions you take will only occur once (there will likely be multiple notifiers). I haven't explored this in detail, but I managed get CreateWindow to put an edit box on a messagebox dialog. It looked as out of place as a rat's ear grafted onto the spine of a cloned pig, but it works. Doing things this way may be far easier than having to roll your own.

Crow.

EDIT: Small correction to handle the problem raised by Raymond Chen. Make sure that parent handles agree throughout, and this should work ok. It does for me, even with two instances of the same program...

3
  • 2
    Note that this will find a message box, not necessarily your message box. May 25, 2012 at 20:32
  • Not a problem... Replace if(X) with if(GetParent(X)==H_frame) and the file dialogs and messageboxes in the program are also parented by H_frame or whatever handle name, whose value will be unique, so even multiple instances of the same program will co-exist safely.
    – user1418124
    May 26, 2012 at 0:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.