MessageBox

2.x

  int MessageBox(hwndParent, lpszText, lpszTitle, fuStyle)    
  HWND hwndParent; /* handle of parent window */
  LPCSTR lpszText; /* address of text in message box */
  LPCSTR lpszTitle; /* address of title of message box */
  UINT fuStyle; /* style of message box */

The MessageBox function creates, displays, and operates a message-box window. The message box contains an application-defined message and title, plus any combination of the predefined icons and push buttons described in the fuStyle parameter.

Parameters

hwndParent

Identifies the parent window of the message box to be created. If this parameter is NULL, the message box will have no parent window.

lpszText

Points to a null-terminated string containing the message to be displayed.

lpszTitle

Points to a null-terminated string to be used for the dialog box title. If this parameter is NULL, the default title Error is used.

fuStyle

Specifies the contents and behavior of the dialog box. This parameter can be a combination of the following values:

Value Meaning

MB_ABORTRETRYIGNORE The message box contains three push buttons: Abort, Retry, and Ignore.
MB_APPLMODAL The user must respond to the message box before continuing work in the window identified by the hwndParent parameter. However, the user can move to the windows of other applications and work in those windows. MB_APPLMODAL is the default if neither MB_SYSTEMMODAL nor MB_TASKMODAL is specified.
MB_DEFBUTTON1 The first button is the default. Note that the first button is always the default unless MB_DEFBUTTON2 or MB_DEFBUTTON3 is specified.
MB_DEFBUTTON2 The second button is the default.
MB_DEFBUTTON3 The third button is the default.
MB_ICONASTERISK Same as MB_ICONINFORMATION.
MB_ICONEXCLAMATION An exclamation-point icon appears in the message box.
MB_ICONHAND Same as MB_ICONSTOP.
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 push button: OK.
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_SYSTEMMODAL All applications are suspended until the user responds to the message box. Unless the application specifies MB_ICONHAND, the message box does not become modal until after it is created; consequently, the parent window and other windows continue to receive messages resulting from its activation. System-modal message boxes are used to notify the user of serious, potentially damaging errors that require immediate attention (for example, running out of memory).
MB_TASKMODAL Same as MB_APPLMODAL except that all the top-level windows belonging to the current task are disabled if the hwndParent parameter is NULL. This flag should be used when the calling application or library does not have a window handle available but still needs to prevent input to other windows in the current application without suspending other applications.
MB_YESNO The message box contains two push buttons: Yes and No.
MB_YESNOCANCEL The message box contains three push buttons: Yes, No, and Cancel.

Return Value

The return value is zero if there is not enough memory to create the message box. Otherwise, it is one of the following menu-item values returned by the dialog box:

Value Meaning

IDABORT Abort button was selected.
IDCANCEL Cancel button was selected.
IDIGNORE Ignore button was selected.
IDNO No button was selected.
IDOK OK button was selected.
IDRETRY Retry button was selected.
IDYES Yes button was selected.

If a message box has a Cancel button, the IDCANCEL value will be returned if either the ESC key is pressed or the Cancel button is selected. If the message box has no Cancel button, pressing ESC has no effect.

Comments

When a system-modal message box is created to indicate that the system is low on memory, the strings pointed to by the lpszText and lpszTitle parameters should not be taken from a resource file, because an attempt to load the resource may fail.

When an application calls the MessageBox function and specifies the MB_ICONHAND and MB_SYSTEMMODAL flags for the fuStyle parameter, Windows displays the resulting message box regardless of available memory. When these flags are specified, Windows limits the length of the message-box text to three lines. Windows does not automatically break the lines to fit in the message box, however, so the message string must contain carriage returns to break the lines at the appropriate places.

If a message box is created while a dialog box is present, use the handle of the dialog box as the hwndParent parameter. The hwndParent parameter should not identify a child window, such as a control in a dialog box.

Following are the various system icons that can be used in a message box:

See Also

FlashWindow, MessageBeep