THE MESSAGE BOX

Let's take a breather here. We've been looking at ways to customize dialog boxes. Now let's look at an alternative to dialog boxes, which is the message box. We began using message boxes way back in Chapter 5, but we haven't yet examined them in detail.

The message box is an appropriate and easy-to-use alternative to a dialog box when you need a simple response from the user. The general syntax is:

nItem = MessageBox (hwndParent, lpszText, lpszCaption, nType) ;

The message box has a caption (the character string lpszCaption), one or more lines of text (lpszText), one or more buttons, and (optionally) a predefined icon. One of the buttons is a default. The nItem value returned from MessageBox indicates the button that was pressed.

The hwndParent parameter is generally the handle to the window that creates the message box. The input focus will be set to this window when the message box is destroyed. If you don't have a window handle available or you don't want the input focus to go to one of your windows, you can use NULL for the handle. If you use a message box within a dialog box, use the dialog box window handle (which we've been calling hDlg) for this parameter.

The lpszText parameter is a long pointer to NULL-terminated text that appears in the body of the message box. Windows breaks this text into several lines if necessary. You can also include tab characters in the text, and you can define your own line breaks using carriage returns or linefeeds or both. The lpszCaption string is generally the name of the application.

The nType parameter is a collection of flags joined by the C bitwise OR operator. The first group of flags specifies the push buttons to appear at the bottom of the message box: MB_OK (the default), MB_OKCANCEL, MB_YESNO, MB_YESNOCANCEL, MB_RETRYCANCEL, and MB_ABORTRETRYIGNORE. As you can see, these flags allow for a maximum of three buttons. The second group of flags specifies which of the buttons is the default: MB_DEFBUTTON1 (the default), MB_DEFBUTTON2, and MB_DEFBUTTON3.

The third group of flags specifies an icon to appear in the message box: MB_ICONINFORMATION, MB_ICONEXCLAMATION, MB_ICONSTOP, and MB_ICONQUESTION. There is no default. If you omit one of these flags, the message box has no icon. You should use the information icon for a status message, the exclamation point for a reminder, the question mark for a warning of the consequences of an action, and the stop icon for a signal of serious problems.

The fourth set of flags governs whether the message box is application modal, in which case the user can switch to another application without ending the message box, or system modal, which requires the user to end the message box before doing anything else. The flags are MB_APPLMODAL (the default) and MB_SYSTEMMODAL. Finally, you can use a fifth flag, MB_NOFOCUS, which displays the message box but does not give it the input focus.

Depending on which button is pressed, the message box returns one of the following identifiers: IDOK, IDCANCEL, IDYES, IDNO, IDRETRY, and IDABORT.