VS2010/MFC dialog box: message dialog box

Source: Internet
Author: User
Tags function prototype

message dialog box

In the process of using the Windows system, we often meet the message dialog box, prompting us to have an exception or ask questions. Because of the frequent use of message dialogs in software development, MFC provides two functions that directly generate a specific style of message dialog, without requiring us to create dialog resources and generate dialog classes for each use. These two functions are the member functions of the CWnd class, The MessageBox () and the global function AfxMessageBox ().

A. the use of the Cwnd::messagebox () function and the AfxMessageBox () function

The following explains the usage of two functions respectively.

1.cwnd::messagebox () function

The function prototype for Cwnd::messagebox () is as follows:

int MessageBox (
LPCTSTR LpszText,
LPCTSTR lpszcaption = NULL,
UINT NType = MB_OK
);

Parameter description:

LpszText: The message string that needs to be displayed.

Lpszcaption: The caption string for the message dialog box. The default value is null. The default caption is used when the value is null.

NType: The style and properties of the message dialog box. The default is MB_OK style, that is, only the OK button.

The value of ntype can be either one of the following two tables or any combination of each value. That is, you can specify a dialog box type, you can specify a dialog box icon, and you can set both.

NType value Parameter description
Mb_abortretry There are "terminate", "Retry" and "Ignore" buttons
Mb_ok There is a "OK" button
Mb_okcancel "OK" and "Cancel" buttons
Mb_retrycancel There are "Retry" and "Cancel" buttons
Mb_yesno "Yes" and "no" buttons
Mb_yesnocancel "Yes", "No" and "Cancel" buttons


dialog box type table

NType value Display icon
Mb_iconexclamtion
Mb_iconwarning
Mb_iconasterisk
Mb_iconinformation
Mb_iconquestion
Mb_iconhand
Mb_iconstop
Mb_iconerror

dialog box Icon table

If you want to set the value of Ntype to a combination of type and icon, you can take a value like this: Mb_okcancel | Mb_iconquestion. Bitwise take or on it.

2.AfxMessageBox () function

The function prototypes for AfxMessageBox () are:

int AfxMessageBox (
LPCTSTR LpszText,
UINT NType = MB_OK,
UINT nidhelp = 0
);

Parameter description:

LpszText: Same as Cwnd::messagebox () function

Ntype:cwnd::messagebox () function

NIDHELP: The context ID of the Help for this message. The default value is 0, and taking 0 indicates that you want to use the application's default Help context.

two. return values for Cwnd::messagebox () and AfxMessageBox ()

After we call the above two functions, we can pop up the modal message dialog box. When the message dialog box is closed, we can also get their return value. The return value of both is the ID of the button that the user clicked on the message dialog box, which can be the following value:

Idabort: Click the "Terminate" button.
IDCANCEL: Click the Cancel button.
Idignore: Click the Ignore button.
Idno: Click the No button.
IDOK: Click the OK button.
Idretry: Click the Retry button.
Idyes: Click the Yes button.

three. Application Examples

Let's take an example of the previous addition calculator program.

Do you remember that in the modal dialog and its pop-up process we modified the Cadditiondlg::onbnclickedaddbutton () function, after the point of the "calculation" button to pop a modal dialog box, asking whether the user is determined to do the addition calculation, The user selects "OK" or "Cancel" by the return value of the modal dialog box DoModal function. These features make it clear that the message dialog box is fully implemented, and the chicken Peck Meter uses the message dialog box instead of the original modal dialog box.

In the Non-modal dialog box creation and display, the chicken Peck Rice Annotated modal dialog box's related code, added the non-modal dialog box's creation and the display code, we before joins the message dialog box the non-modal dialog box code also comments or deletes, ensures that this function no longer produces the original modal dialog box or the Non-modal dialog box.

The modified Cadditiondlg::onbnclickedaddbutton () function is as follows:

C + + code
  1. void Cadditiondlg::onbnclickedaddbutton ()
  2. {
  3. //Todo:add your control notification handler code here
  4. INT_PTR Nres;
  5. //Display message dialog box
  6. Nres = MessageBox (_t ("Are you sure you want to do an addition calculation?") "), _t (" addition Calculator "), Mb_okcancel |   Mb_iconquestion);
  7. //Determine the return value of the message dialog box. If return is IDCANCEL, continue execution down
  8. if (IDCANCEL = = nres)
  9. return;
  10. //Save the data in each control to the appropriate variable
  11. UpdateData (TRUE);
  12. //Will be added and assigned to M_editsum by Addend and Addend
  13. M_editsum = M_editsummand + m_editaddend;
  14. update the corresponding control according to the values of each variable. And the edit box will display the value of the M_editsum
  15. UpdateData (FALSE);
  16. //Set Properties dialog Box for wizard dialog box
  17. //sheet.   SetWizardMode ();
  18. }

Compile run, click the "Calculate" button on the Run Results dialog box to pop up the following message dialog box:

You can also change the MessageBox function to the AfxMessageBox () function, while the parameters are modified accordingly, to run a look at the effect.

VS2010/MFC dialog box: message dialog box

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.