Loading

Win32: Message Boxes

Message Boxes

In this C++ Win32 lesson, we explain how to use message boxes to alert the user to a situation or condition. A message box can also get simple user feedback via button clicks. In this video, we use the code from Win32 Lesson 1 and change the "Help->About" handler to make it pop up a message box when the user selects "About" in the menu.

We begin by replacing the code in line 149 of the file "Win32Lesson1.cpp" with this call to the MessageBox() function:

::MessageBox(hWnd, _T("Message") , _T("Title"), MB_OK);

Now, when the user selects "Help" and "About" the program pops up this message box:

This message box serves as an immediate notice and blocks the user from any interaction with the main application window until the user clicks the "OK" button. Generally, this is the way we would like a message box to behave and we get this behavior by making the main window the owner of the message box. The window was designated as the owner when we passed in "hWnd" as the first parameter of the MessageBox() function. If we would not like to block the window we can pass in NULL as the first parameter. Then the message has no owner.

Notice that the middle of the message box above contains the word "Message." We set this when we passed in the string "Message" as the second parameter. Also, we set the text "Title" in the title bar when we passed in "Title" as the third parameter. These strings must be passed in as UNICODE. The macro _T() changes the strings to UNICODE for us. The final parameter is MB_OK and this causes the "OK" button to appear in the message box.

Our second message box is shown above. Replacing the previous message box code with this creates this message box above:

::MessageBox(hWnd, _T("Play again?"), _T("Game Over"), MB_YESNO);

Notice that we have changed the messages inside of the message box and in the title bar. Also, we have changed the button so that we now have a "Yes" and a "No" button.

The MessageBox() returns a value when the user clicks a button. Replace the previous MessageBox() call with this code to create handlers that respond to the a "Yes" or "No" click by popping up a new message box:

{
    int iMessage = ::MessageBox(hWnd, _T("Play again?"), _T("Game Over"),
        MB_YESNO);
    switch (iMessage) {
    case IDYES:
        {
            ::MessageBox(hWnd, _T("Yes"), _T("Yes") , MB_OK);
            break;
        }
        case IDNO:
        {
            ::MessageBox(hWnd, _T("No"), _T("No") , MB_OK);
            break;
        }
    }
}

Finally, we remark that we can change the message box styles by adding icons, for example, to the previous message boxes by using the bitwise "Or" operator: |. Various message box options exist for styling message boxes and they can be found in the file "WinUser.h" in lines 7282 to 7328. Set the code above to look like this:

{
    int iMessage = ::MessageBox(hWnd, _T("Play again?"), _T("Game Over"),
        MB_YESNO | MB_ICONQUESTION);
    switch (iMessage) {
    case IDYES:
        {
            ::MessageBox(hWnd, _T("Yes"), _T("Yes") , MB_OK |
                MB_ICONEXCLAMATION);
            break;
        }
    case IDNO:
        {
            ::MessageBox(hWnd, _T("No"), _T("No") , MB_OK |
                MB_ICONHAND);
            break;
        }
    }
}

Now, each of our three message boxes has an icon in it.