AfxMessageBox and MessageBox differences
We know that AFX begins with a global function, which means that AfxMessageBox can be used in any program, and the MessageBox can only be used in CWnd and its derived classes.
AfxMessageBox
Function prototypes for AfxMessageBox: There are two forms
int0 );//第一种int AFXAPI AfxMessageBox( UINT nIDPrompt, UINT nType = MB_OK, UINT nIDHelp = (UINT) –1 );//第二种
description of the parameter
first parameter
In the first form: Lpsztext represents the text that is displayed inside the message box, and the caption of the message box is the application's executable file name (such as Hello).
In the second form, NIDPROMPT is the ID of the text string to display in the string table. The string is automatically loaded from the string table and displayed in a message box when the function is called.
a second parameter
The second parameter, Ntype, is a combination of the button style and icon style displayed in the message box, which can be combined with the | (or) operator.
There are several styles of buttons:
The abort, Retry, ignore buttons are displayed in the Mb_abortretryignore message box
MB_OK Show OK button
Mb_okcancel display OK, Cancel button
Mb_retrycancel Display Retry, Cancel button
Mb_yesno Show Yes, no button
Mb_yesnocancel Show Yes, No, Cancel button
Icon Style
Mb_iconinformation displays an I icon that indicates a hint
Mb_iconexclamation displays an exclamation point, indicating a warning
Mb_iconstop display hand icon indicating a warning or critical error
Mb_iconquestion Display question mark icon, indicating doubt
A function similar to AfxMessageBox, MessageBox, which is a class member function of CWnd:
MessageBox
The MessageBox prototype is as follows
intNULL,UINT nType =MB_OK );
Description of the parameter: similar to the parameters of the AfxMessageBox function prototype
LpszText: Message content to display
Lpszcaption: Is the message box caption (not by default)
NType: The style of the message box, similar to the above
A summary of the differences between two functions
The difference between the two functions:
- AfxMessageBox is a global function provided by the MFC library, providing a variety of overloaded forms, while the MessageBox is a standard Windows API function.
- The AfxMessageBox () function can be used in any class, and the MessageBox () function can only be used in the inheritance class of the CWnd class. In addition, the parameters of the AfxMessageBox () function are not rich in the parameters of the MessageBox () function, so the latter is more flexible than the former. For example: The MessageBox can set the caption arbitrarily, and AfxMessageBox cannot.
- AfxMessageBox cannot control the message box caption, the internal data output or warning that is commonly used when debugging a program; The MessageBox is more formal, often in the version of the application to be submitted, you can control the title content without having to use the unknown executable name as the title.
Example
AfxMessageBox("你是猪吗?",MB_YESNO|MB_ICONQUESTION);a"确定删除吗?""Warning!!",MB_YESNO|MB_ICONWARNING);if (a == IDYES)AfxMessageBox("Yes");elseAfxMessageBox("No");
The difference between the MessageBox function in the API and MFC, namely the MessageBox and:: MessageBox
MessageBox,:: MessageBox, AfxMessageBox The essential differences between the three functions are as follows:
Windows Api:messagebox ();
Cwnd::messagebox ();
Mfc:afxmessagebox ();
where Cwnd::messagebox () and AfxMessageBox () are encapsulated in the API function MessageBox ().
The API is defined as follows
int MessageBox( HWND hWnd, //ofwindow LPCTSTR lpText, //ofin message box //ofof message box UINT uType //of message box);
The definition in MFC is as follows
CWnd::MessageBoxint MessageBox( LPCTSTR lpszText, NULL, UINT nType = MB_OK);
So, in MFC only the first parameter (the handle HWND) is missing, while the third and four have default values, which is their difference, the other is the same
If you want to invoke the API's MessageBox function in the MFC form class, this should be called:
:: MessageBox (NULL, "message", "title", MB_OK);
description of whether a handle HWND is required
1) The MessageBox is an API, so it requires the HWND parameter to specify its owning form.
2) Cwnd::messagebox () is a member function of CWnd, which requires the MessageBox to be used only in CWnd classes and its subclasses; it does not require the HWND parameter, because CWND has already indicated its owning window. therefore Cwnd::messagebox () and:: MessageBox (HWND) act as equals.
3) AfxMessageBox () global function in MFC, no handle HWND parameter. You can use AfxMessageBox () anywhere you can use the MessageBox (), but AfxMessageBox cannot set the message box caption (its default caption is the application's executable file name).
Usage recommendations
1) used in non-CWnd class is the API MessageBox function, but it is very inconvenient to use, you must specify the title, handle and style, or choose Cwnd::messagebox () or AfxMessageBox, they are easy to use.
2) MessageBox is modal dialog box, you do not confirm when the program does not run down, it will block your current thread for, unless your program is multithreaded program, otherwise only wait for modal dialog box is confirmed.
3) If using MFC, please try to use AfxMessageBox, because this global dialog box is the safest and most convenient.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
AfxMessageBox and MessageBox differences