Message Box Function

In the previous example, we used the message box function to display data to the user. You have probably seen pop-up message boxes countless times in various Windows programs. These are such an integral part of Windows programming that it will be vital that you have a good understanding of them. The first think you must remember is that before you can utilize the message box function, you must include the following line of code.

#include <stdafx.h> . 

The format of the message box function is simple, and is shown in the following example.

AfxMessageBox( text, type, help id)

The text is simply whatever text you wish to appear in your message box. The type determines what kind of buttons you want to appear on your message box as well as any icons you may wish to display on the message box. The message box buttons are summarized in Table 15.3.

Table 15.3: Message Box Types




The message box contains: Abort, Retry, and Ignore.


The message box contains just the OK button.


The message box contains OK and Cancel.


The message box contains Retry and Cancel.


The message box contains two pushbuttons: Yes and No.


The message box contains three pushbuttons: Yes, No, and Cancel.


An exclamation-point icon appears in the message box


An icon consisting of an i in a circle appears in the message box.


A question-mark icon appears in the message box.


A stop-sign icon appears in the message box.


The user must respond to the message box before continuing work in the current window.


All applications are suspended until the user responds to the message box.


The first button is the default.


The second button is the default.


The third button is the default.

It is not necessary that you commit all these possible message box settings to memory. You can refer to this table whenever you need to, and you will see several of these settings used in projects throughout this chapter and the next chapter as well. You will, undoubtedly, find the message box to be a very versatile function. It is an excellent way to provide the user with information, and to get their input.

The message box function you saw in the preceding examples simply presented information to the user. The question that we did not address in that example was the following: How do you respond when the user presses a button? How do you even know which button they pressed, assuming you used one of the options to display multiple buttons? When the user clicks on a button, the function returns an integer that tells you which button was pressed. There are a number of constants that will identify the button pressed. Those constants are listed here.

  • IDYES—The Yes button was pressed.

  • IDNO—The No button was pressed.

  • IDABORT—The user pressed the abort button.

  • IDCANCEL—The user pressed the cancel button.

  • IDIGNORE—The Ignore button was pressed.

  • IDOK—The OK button was pressed.

  • IDRETRY—The Retry button was pressed.

         int ireturn;        UpdateData(TRUE);        ireturn =AfxMessageBox(m_mytext,MB_YESNO);        if(ireturn == IDYES)        {      //do something       }

Thus, you can see that you can use the message box function to handle a great deal of user interaction in your Visual C++ programs.

C++ Programming Fundamentals
C++ Programming Fundamentals (Cyberrookies)
ISBN: 1584502371
EAN: 2147483647
Year: 2005
Pages: 197
Authors: Chuck Easttom © 2008-2017.
If you may any questions please contact us: