jave.web
jave.web

Reputation: 15032

Win api MessageBox alternative that has a set for text-align: center

As I found out by searching, it is not possible to create a MessageBox() with center-aligned text.

So is there some simple alternative providing the MessageBox() functionality (including program wait for closing/accepting the box) that has an option to center-align the text?

Thanks for suggestions/examples.

PS: On Windows7+, using C++ Windows API (compiled in MS Visual Studio 2012)

EDIT:
Some useful tips:
1) Express version of Visual Studio does NOT have a resource editor/file create option:

2) Visual Studio C++ how to display a dynamic message (i.e., string) in my About box?

Upvotes: 0

Views: 1976

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 595827

As I found out by searching, it is not possible to create a MessageBox() with center-aligned text.

It is not possible to create a center-aligned MessageBox() dialog, as the API does not expose an option for that. But it is possible to manipulate the standard MessageBox()` dialog with some slight trickery to force it to be center-aligned.

Use SetWindowsHookEx() to create a WH_CBT hook for the thread that is calling MessageBox() (no DLL needed). The hook callback allows you to discover the HWND of the dialog that MessageBox() creates. With that, you can manipulate it however you want. In this case, you can use FindWindowEx() to get the HWND of the STATIC control for the dialog's text and then apply the SS_CENTER style to it using SetWindowLong(). For example:

LRESULT CALLBACK CenterMsgBoxTextProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    if (nCode == HCBT_ACTIVATE)
    {
        HWND hDlg = (HWND) wParam;
        HWND hTxt = FindWindowEx(hDlg, NULL, TEXT("STATIC"), NULL);
        if (hTxt)
            SetWindowLong(hTxt, GWL_STYLE, GetWindowLong(hTxt, GWL_STYLE) | SS_CENTER);
    }

    return CallNextHookEx(NULL, nCode, wParam, lParam);
}

HHOOK hHook = SetWindowsHookEx(WH_CBT, (HOOKPROC) &CenterMsgBoxTextProc, NULL, GetCurrentThreadId());
MessageBox(...);
if (hHook) UnhookWindowsHookEx(hHook);

Alternatively, you can use SetWinEventHook() instead of SetWindowsHookEx():

void CALLBACK CenterMsgBoxTextProc(HWINEVENTHOOK hWinEventHook, DWORD event, HWND hwnd, LONG idObject, LONG idChild, DWORD dwEventThread, DWORD dwmsEventTime)
{
    HWND hTxt = FindWindowEx(hwnd, NULL, TEXT("STATIC"), NULL);
    if (hTxt)
        SetWindowLong(hTxt, GWL_STYLE, GetWindowLong(hTxt, GWL_STYLE) | SS_CENTER);
}

HWINEVENTHOOK hHook = SetWinEventHook(EVENT_OBJECT_CREATE, EVENT_OBJECT_CREATE, NULL, &CenterMsgBoxTextProc, GetCurrentProcessId(), GetCurrentThreadId(), 0);
MessageBox(NULL, TEXT("test"), TEXT("test"), MB_OK);
if (hHook) UnhookWinEvent(hHook);

Here is what the result looks like in both cases:

proof

Upvotes: 3

Felix Bytow
Felix Bytow

Reputation: 345

Afaik there is not. But it is actually quite easy to create such a dialog using Win32 resources and the DialogBox function.

Upvotes: 1

Related Questions