MessageBoxA Function
Displays a modal dialog box that can contain an application-defined icon, set of buttons, and a predefined set of reply buttons. The message box stays until the user closes it.
int MessageBoxA(
[in, optional] HWND hWnd,
[in, optional] LPCSTR lpText,
[in, optional] LPCSTR lpCaption,
[in] UINT uType
);
Parameters
| Parameter | Description |
|---|---|
hWnd |
A handle to the owner window of the dialog box. If this parameter is NULL, the message box has no owner window. |
lpText |
A null-terminated string that specifies the message to be displayed. |
lpCaption |
A null-terminated string that specifies the caption, or title, of the dialog box. If this parameter is NULL, the caption is "Error". |
uType |
A bitwise combination of flags that determines the contents and behavior of the dialog box. This parameter can be a combination of the following values:
|
Return Value
If the function succeeds, the return value is one of the following integer values corresponding to the button that the user clicked:
IDOK: The OK button was selected.IDCANCEL: The Cancel button was selected.IDYES: The Yes button was selected.IDNO: The No button was selected.
Remarks
The MessageBox function displays a modal dialog box. This means that the user must close the dialog box before continuing to interact with the application.
The MessageBoxA function is the ANSI version of this function. The Unicode version is MessageBoxW.
Tip: For simpler message boxes, consider using
MessageBoxEx which allows for more customization and localization.
Example
#include <windows.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
int msgboxID = MessageBoxA(
NULL,
"Do you want to continue?",
"Confirmation",
MB_ICONQUESTION | MB_YESNO | MB_DEFBUTTON2
);
switch (msgboxID)
{
case IDYES:
// User clicked Yes
MessageBoxA(NULL, "You chose to continue.", "Result", MB_OK);
break;
case IDNO:
// User clicked No
MessageBoxA(NULL, "You chose not to continue.", "Result", MB_OK);
break;
}
return 0;
}
Note: Ensure that the correct header file (
<windows.h>) is included in your project.