user476566
user476566

Reputation: 1349

Message box with cancel button only

Can we have a message box with cancel button only ? If so , any hints ? Is there a built in api method to get only messagebox with cancel button alone/

How to create custom modal dialog ? Any Links ?

Upvotes: 2

Views: 3175

Answers (3)

Remy Lebeau
Remy Lebeau

Reputation: 595320

You can use a thread-local CBT hook via SetWindowsHookEx() to customize the MessageBox() dialog however you want.

For instance, you can change the text of the "OK" button to say "Cancel" instead, eg:

HHOOK hHook = NULL;

LRESULT CALLBACK CBTHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    if (nCode < 0)
        return CallNextHookEx(hHook, nCode, wParam, lParam);

    if (nCode == HCBT_ACTIVATE)
    {
        HWND hWnd = reinterpret_cast<HWND>(wParam);
        SetWindowText(GetDlgItem(hWnd, IDOK), TEXT("Cancel"));
        return 0;
    }

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

{
    ...
    hHook = SetWindowsHookEx(WH_CBT, reinterpret_cast<HOOKPROC>(&CBTHookProc), NULL, GetCurrentThreadId());
    int iResult = MessageBox(..., MB_OK);
    if (iResult == IDOK) iResult = IDCANCEL;
    UnhookWindowsHookEx(hHook);
    ...
}

Or you can hide the standard "OK" button and let the dialog still use its native "Cancel" button:

HHOOK hHook = NULL;

LRESULT CALLBACK CBTHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    if (nCode < 0)
        return CallNextHookEx(hHook, nCode, wParam, lParam);

    if (nCode == HCBT_ACTIVATE)
    {
        HWND hWnd = reinterpret_cast<HWND>(wParam);
        ShowWindow(GetDlgItem(hWnd, IDOK), SW_HIDE);
        // optionally reposition the IDCANCEL child window as well....
        return 0;
    }

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

{
    ...
    hHook = SetWindowsHookEx(WH_CBT, reinterpret_cast<HOOKPROC>(&CBTHookProc), NULL, GetCurrentThreadId());
    int iResult = MessageBox(..., MB_OKCANCEL);
    UnhookWindowsHookEx(hHook);
    ...
}

Update: on Vista and later, you can (and should) use TaskDialog() or TaskDialogIndirect() instead of MessageBox(). Task dialogs are much more flexible, including the ability to let you specify which buttons are on the dialog, and even use custom buttons. So you can easily display a Cancel-only dialog without using any hooks at all, eg:

TaskDialog(..., TDCBF_CANCEL_BUTTON, ..., &iResult);

TASKDIALOGCONFIG TaskConfig = {0};
TaskConfig.cbSize = sizeof(TaskConfig);
TaskConfig.dwCommonButtons = TDCBF_CANCEL_BUTTON;
...
TaskDialogIndirect(&TaskConfig, &iResult, ...);

Upvotes: 6

Aravind.HU
Aravind.HU

Reputation: 9472

I don't think it is possible with MessabeBox. You can simple create your own dialog and add a single cancel button.

You can also have a look at this link

XMessageBox

It may solve your problem

Upvotes: 0

ddavison
ddavison

Reputation: 29032

For a message box that only displays a notification, I believe it's convention to use an "OK" box. Cancel seems a bit, redundant?

But i suppose if you wanted to do it, you could do it by defining your own message box object and specify the buttons yourself using "Cancel" as the text attribute.

C++ nor any other language has a built-in function for defining message boxes with just "Cancel" again, as "OK" is the convention.

Upvotes: 1

Related Questions