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
- void Cadditiondlg::onbnclickedaddbutton ()
- {
- //Todo:add your control notification handler code here
- INT_PTR Nres;
- //Display message dialog box
- Nres = MessageBox (_t ("Are you sure you want to do an addition calculation?") "), _t (" addition Calculator "), Mb_okcancel | Mb_iconquestion);
- //Determine the return value of the message dialog box. If return is IDCANCEL, continue execution down
- if (IDCANCEL = = nres)
- return;
- //Save the data in each control to the appropriate variable
- UpdateData (TRUE);
- //Will be added and assigned to M_editsum by Addend and Addend
- M_editsum = M_editsummand + m_editaddend;
- update the corresponding control according to the values of each variable. And the edit box will display the value of the M_editsum
- UpdateData (FALSE);
- //Set Properties dialog Box for wizard dialog box
- //sheet. SetWizardMode ();
- }
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