4. Message box (MessageBox)
Message function creates, displays, and operates a message box or box. The message box contains a message and the title defined by the application, and any combination of predefined icons and buttons.
int __fastcall MessageBox (char * Text, char * Caption, int Flags);
Parameters
Text (Text)
Points to a null-terminated string that contains the message to display.
Caption (Legend)
Points to a null-terminated string used for the title of the dialog box. If this parameter is NULL, use the default title of error.
Flags (Flags)
Specifies a set of bit flags that determine the content and behavior of the dialog box. This parameter can be a combination of flags from the following groups.
1 .- Specify one of the following flags to indicate the buttons contained in the message box:
Flags | Meaning |
MB_ABORTRETRYIGNORE | The message box contains three buttons: Abort, Retry, and Ignore. |
MB_OK | The message box contains a button: OK. This is the default. |
MB_OKCANCEL | The message box contains two buttons: OK and Cancel. |
MB_RETRYCANCEL | The message box contains two buttons: Retry and Cancel. |
MB_YESNO | The message box contains two buttons: Yes and No. |
MB_YESNOCANCEL | The message box contains three buttons: Yes, No, and Cancel. |
2 .- Specify one of the following flags to display an icon in the message box:
Flags | Meaning |
MB_ICONEXCLAMATION | an exclamation point icon appears in the message box. |
MB_ICONWARNING | |
MB_ICONINFORMATION | an icon that is a lowercase i in a circle appears in the message box. |
MB_ICONASTERISK | |
MB_ICONQUESTION | A question mark icon appears in the message box. |
MB_ICONSTOP | a stop sign icon appears in the message box. |
MB_ICONERROR | |
MB_ICONHAND | |
3 Specify one of the following flags to indicate the default button:
Flags | Meaning |
MB_DEFBUTTON1 | The first button is the default button. |
MB_DEFBUTTON2 | The second button is the default button. |
MB_DEFBUTTON3 | The third button is the default button. |
MB_DEFBUTTON4 | The fourth button is the default button. |
MB_DEFBUTTON1 is the default unless specified MB_DEFBUTTON2, MB_DEFBUTTON3 or MB_DEFBUTTON4.
Return Value
The return value is zero if there is insufficient memory to create the message box.
If the function succeeds, the return value is one of the following menu items of the values returned by the dialog:
Value | Numeric value | Meaning |
IDABORT | 3 | The user chose the Cancel button. |
IDCANCEL | 2 | The user chose the Cancel button. |
IDIGNORE | 5 | The user chose the Ignore button. |
IDNO | 7 | The user selects the No button |
IDOK | 1 | The user chose the OK button. |
IDRETRY | 4 | The user chose the Retry button. |
IDYES | 6 | The user selected the Yes button |
If a message box has a Cancel button, the function returns the value IDCANCEL if the ESC key is pressed or the Cancel button is selected. If the message box has no Cancel button, pressing ESC has no effect.
4.1. Example of use of the MessageBox
In the following example, you enter the call to MessageBox inside an if statement. The message box is displayed when reaching the decision and waits for the user to act on it. When you close the MessageBox return value is captured and evaluated by the ruling. In this case, if you've pressed the accept button IDOK run the body of the sentence, otherwise it is discarded.
void __fastcall TForm1:: Button1Click (TObject * Sender)
{
if (Application-> MessageBox ("Save the file?", "Save As" MB_OKCANCEL) == IDOK)
{
/ / Save the file
}
}

MessageBox Example
The following example shows the combination of several indicators to set the buttons to be displayed along with an icon.
void __fastcall TForm1:: Button1Click (TObject * Sender)
{
if (Application-> MessageBox ("Save the file?", "Save As" MB_OKCANCEL + MB_ICONQUESTION) == IDOK)
{
/ / Save the file
}
}

Example of a combination of several flags in MessageBox