The windows message box is a simple modal dialog box that displays a message and may include some selection options. They are typically used to inform the user that an event has taken place. The MessageBox function takes 5 parameters: a parent handle, a title, a message, and a selection option. If the parent handle is NULL, the message box will be modeless. If a handle for a parent window is specified, the MessageBox can be Modal to the parent window.
To create a message box, use the API function call MessageBox(). The prototype for this function is
int MessageBox(HWND hWnd,LPCTSTR lpText,LPCTSTR lpCaption,UINT uType);
where
hWnd – is a handle to the owner window of the message box to be created. If this parameter is NULL, the message box has no owner window.
LpText – The message to be displayed.
LpCaption – Contains dialog box title. If this parameter is NULL, the default title is Error
UType – defines the contents and behaviour of the dialog box and will be a combination of a number of different flag value but some of the more common values are
MB_ABORTRETRYIGNORE- The message box contains three pushbuttons: Abort, Retry, and Ignore. MB_ICONEXCLAMATION-An exclamation-point icon appears in the message box. MB_ICONERROR-A stop-sign icon appears in the message box. MB_ICONINFORMATION – An icon consisting of a lowercase letter i in a circle appears in the message box. MB_ICONQUESTION-A question-mark icon appears in the message box MB_ICONSTOP- A stop-sign icon appears in the message box. MB_OK – The message box contains one pushbutton: OK. This is the default. MB_OKCANCEL – The message box contains two push buttons: OK and Cancel. MB_RETRYCANCEL – The message box contains two push buttons: Retry and Cancel. MB_YESNO – The message box contains two push buttons: Yes and No. MB_YESNOCANCEL – The message box contains three pushbuttons: Yes, No, and Cancel. |
The return value will depend on the type of message box selected but will be one of the following: IDABORT, IDCANCEL, IDCONTINUE, IDIGNORE, IDNO, IDOK, IDRETRY, IDYES
For further detailed reading
https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-messagebox
The following short program displays a very simple message box
#include <windows.h>
int APIENTRY WinMain( HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int nCmdShow )
{
MessageBox( NULL, TEXT("Hello, World!"), TEXT("Hi!"), MB_OK );
return 0;
}
Although the MessageBox API does not offer the same options for customisation as the standard controls, MessageBox messages can intercepted by registering a windows hook. To this end WM_CBT hook is perfect for changing the behaviour and appearance of a message box without adding too much overhead.
The following short program inserts a thread-specific CBT hook. The notification code HCBT_ACTIVATE, is sent whenever a new messagebox is activated and used to set up a timer which counts down from 10 to 0 before the messagebox is terminated –
#include <windows.h>
#include <tchar.h>
#define MY_TIMER 1
HWND staticbox;
TCHAR countdown[2]=TEXT("\0");
int nTimer;
HHOOK hMsgBoxHook;
//process countdown timer event
VOID CALLBACK MyTimerProc( HWND hWnd, UINT uTimerMsg, UINT uTimerID, DWORD dwTime )
{
static int timercount=9;
wsprintf(countdown,TEXT("%d"),timercount);
SetWindowText(staticbox, countdown);
if (timercount==0)
{
PostQuitMessage(0);
}
--timercount;
}
//customised windows procedure for handling message box bahaviour
LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam)
{
HWND hwnd;
HFONT font;
switch(nCode)
{
//detects creation of new messagebox window.
case HCBT_ACTIVATE:
// Get handle to the message box!
hwnd = (HWND)wParam;
//create messagebox contents
staticbox=CreateWindow(TEXT("static"), TEXT("10"),WS_CHILD | WS_VISIBLE ,5, 5, 35, 35,hwnd, (HMENU) 1, NULL, NULL);//static control
font = CreateFont(25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,TEXT("Times New Roman"));
SendMessage(staticbox,WM_SETFONT,(WPARAM)font,0);
nTimer = SetTimer( hwnd, MY_TIMER, 1000,(TIMERPROC)MyTimerProc );
return 0;
}
return CallNextHookEx(hMsgBoxHook, nCode, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int nShowCmd)
{
// Window hook allows the application to intercept message-box creation, and customise it
hMsgBoxHook = SetWindowsHookEx(WH_CBT, CBTProc, NULL, GetCurrentThreadId() );
// Display a standard message box
MessageBox(NULL, NULL, TEXT("Self terminating message box"),NULL);
// remove the window hook
UnhookWindowsHookEx(hMsgBoxHook);
return 0;
}