There are three parts to a message box: the icon, the message, and buttons. Applications specify the icons and buttons by using message-box style constants. Message text is specified by a null-terminated string.

To create a message box, the application calls WinMessageBox, which displays the message box and processes user input until the user selects a button in the message box. The WinMessageBox return value indicates which button the user selected.

The following code fragment illustrates how to create a message box with a default Yes button, a No button, and a question-mark (?) icon. This example assumes that you have defined a string resource with the MY_MESSAGESTR_ID identifier in the resource file.

    UCHAR  szMessageString[255];
    ULONG  ulResult;

    WinLoadString(hab, (HMODULE) NULL, MY_MESSAGESTR_ID,
        sizeof(szMessageString), szMessageString);

    ulResult = WinMessageBox(hwndFrame,  /* Parent    */
        hwndFrame,                       /* Owner     */
        szMessageString,                 /* Text      */
        (PSZ) NULL,                      /* caption   */
        MY_MESSAGEWIN,                   /* Window ID */
        MB_YESNO |
        MB_ICONQUESTION |
        MB_DEFBUTTON1);                  /* Style     */

     if (ulResult == MBID_YES) {

          /* Do yes case. */

     } else {

          /* Do no case.  */
     }

The WinMessageBox function returns predefined values indicating which button has been selected.

Notice that strings for message boxes should be defined as string resources to facilitate program translation for other countries. However, there is danger in using string resources in message boxes that are called in low-memory situations; loading a string resource in such situations could result in severe memory problems and cause an application to fail. One way to prevent this problem is to preload the string resource and make it nondiscardable so it will be available when the message box must be displayed.


[Back] [Next]