Original URL: http://blog.csdn.net/holybin/article/details/28403109
First, MessageBox () usage
1. Function prototype
The MessageBox function has a different definition in the WIN32 API and MFC.
The WIN32 API is defined as follows:
[CPP]View Plaincopy
-
- int WINAPI MessageBox (
-
- hwnd hwnd, //Handle of owner window
-
- LPCTSTR Lptext, //address of the text in message box
-
- LPCTSTR lpcaption, //Address of the title of the message box
-
- UINT utype //style of message box
-
- );
MFC is defined as a member function of the CWnd class, as follows:
[CPP]View Plaincopy
- int Cwnd::messagebox (
- LPCTSTR LpszText,
- LPCTSTR lpszcaption = NULL,
- UINT nType = Mb_ok
- );
So in MFC only the first parameter is missing, and the third parameter and the fourth parameter have default values.
2, each of the parameters:
The HWND is the owning window of the message box. If this parameter is NULL, the message box does not have a window.
Lptext and Lpsztext are the contents of the message box.
Lpcaption and Lpszcaption are the headers of the message box.
Utype and Ntype Specify a set of bit flags that determine the content and behavior of the dialog box (buttons, icons, patterns, and other). This parameter can be a combination of flags in the following flag groups:
(1) button
MB_OK: Default value. There is a confirmation button on the inside.
Mb_yesno: Yes and no inside.
Mb_abortretryignore: Have abort (abort), Retry (retry), and ignore (skip)
Mb_yesnocancel: Message box contains three buttons: Yes,no and Cancel
Mb_retrycancel: Retry (retry) and cancel (cancel)
Mb_okcancel: Message box contains two buttons: OK and cancel
(2) icon
Mb_iconexclamation: An exclamation point appears in the message box
Mb_iconwarning: An exclamation point appears in the message box
Mb_iconinformation: An icon with small letter I in a circle appears in the message box
Mb_iconasterisk: An icon with small letter I in a circle appears in the message box
Mb_iconquestion: A problem marker icon appears in the message box
Mb_iconstop: A Stop message icon appears in the message box
Mb_iconerror: A Stop message icon appears in the message box
Mb_iconhand: A Stop message icon appears in the message box
(3) Morphology
Mb_applmodal: The user must respond to a message box before continuing to work in the window identified by the HWND parameter. However, users can move to other threads ' windows and work in those windows. Depending on the hierarchy of Windows in the application, the user moves to other windows within the thread. All child windows of the parent message box are automatically invalidated, but the popup window is not. If neither Mb_systemmodal nor Mb_taskmooal is specified, the Mb_applmodal is the default.
Mb_systemmodal: Except for message boxes with Wb_ex_topmost types, Mb_applmodal and Mb_systemmodal. Using a system modal message box to change a wide variety of users, major corruption errors require immediate attention (for example, memory overflow). If the window is not associated with the HWND, this flag has no effect on the user's interaction with the window.
Mb_taskmodal: If the parameter HWND is NULL, the Mb_taskmodall and Mb_applmodal are the same except that all windows that belong to the current thread's high level are invalidated. This flag is used when the calling application or library does not have a window handle that can be obtained. Instead of shelving other threads, you still need to block other windows from being entered into the calling thread.
(4) Other
Mb_default_desktop_only: The current desktop receiving input must be a default desktop. Otherwise, the function call fails. The default desktop is a desktop that the user has logged and that the application runs on later.
Mb_help: Add a Help button to the message box. Select the Help button or press F1 to generate a Help event.
Mb_right: Text for right adjustment
Mb_rtlreading: Displays messages and uppercase text in a right-to-left order in the Hebrew and Arabic systems.
Mb_setforeground: The message box becomes the foreground window. In the internal system, call the SetForegroundWindow function for the message.
Mb_topmosi: The message box uses the Ws_ex_topmost window type to create the mb_service_notification.
3. Return value (return value-User Select button):
IDOK (1): OK
IDCANCEL (2): CANCEL
Idabort (3): ABORT
Idretry (4): RETRY
Idignore (5): IGNORE
Idyes (6): YES
Idno (7): NO
4, the use of a variety of methods, such as:
(1) application in CWnd class or subclass (MFC prototype)
[CPP]View Plaincopy
- 1.messagebox ( "This is one of the simplest message boxes! ");
- 2.MessageBox (" This is a message box with a caption!) ",
- 3.messagebox ( "This is a OK cancel message box!" ",
- 4.MessageBox ( Span class= "string" > "This is a warning message box!" ",
- 5.messagebox ( "This is a message box with two properties!) ", " title ", mb_iconexclamation| mb_okcancel );
- 6. "a common application", "title", mb_iconexclamation| Mb_okcancel) ==idcancel)
- RETURN;  
(2) applications not in CWnd classes or subclasses (Win32 prototypes)
[CPP]View Plaincopy
- :: MessageBox (NULL, "msg", "caption", MB_OK);
- :: MessageBox (HWnd, "msg", "caption", MB_OK);
Where the HWND is a handle to a window, or directly with the AfxMessageBox () function.
Second, the use of AfxMessageBox ()
1, it is the global function in MFC, the function prototype has two kinds:
[CPP]View Plaincopy
- int AfxMessageBox ( lpctstr lpszText, uint nType = MB_OK, uint nidhelp = 0);
[CPP]View Plaincopy
- int AFXAPI AfxMessageBox ( uint nidprompt, uint nType = MB_OK, uint NIDHELP = (uint) –1);
2, each of the parameters:
The first lpsztext represents the text that is displayed inside the message box, the caption of the message box is the application's executable file name (such as Hello), and the second, 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. Ntype is a combination of the button style and icon style displayed in the message box, which can be combined with the | (or) operator in a variety of styles.
(1) button style
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
(2) 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
3. Return value:
There are 8 return values, if memory is not enough, return 0, otherwise return one of the following values, respectively, corresponding button is clicked:
Idabort the Abort button was selected.
IDCANCEL the Cancel button was selected.
Idignore the Ignore button was selected.
Idno the No button was selected.
IDOK the OK button was selected.
Idretry the Retry button was selected.
Idyes the Yes button was selected.
Note: If the message box has a Cancel button, pressing the ESC key or clicking the Cancel button will return idcancel; If the message box does not have a Cancel button, pressing the ESC key will not work.
4. Use:
When used, the AfxMessageBox (LPCTSTR lpszText) can be called directly;
This default style is AfxMessageBox (LPCTSTR lpsztext,mb_ok| Mb_iconexclamation);
You can also specify the style by Ntype.
Third. the difference between AfxMessageBox () and the MessageBox ()
Difference One:
The MessageBox () is a standard WIN32 API function that can be used directly; AfxMessageBox is the global function provided by the MFC library, provides a variety of overloaded forms, and requires MFC framework support (with AFX framework-based functions).
So in the case of the Win32 SDK, only the MessageBox is used (the MessageBox uses the prototype in Win 32, the HWND is set to null), and in MFC, two can be used (at this point the MessageBox uses the prototype in MFC, Used in CWnd classes or subclasses), but it is best to use AfxMessageBox for the following reasons:
(1) In MFC can be used in the MessageBox () can use AfxMessageBox (), which means that the latter can be used instead of the former;
(2) AfxMessageBox This global function is the safest and easiest, because it is a global function and therefore does not require a corresponding window class.
Difference Two:
MessageBox is more formal, commonly used in the application version to be submitted, you can control the content of the title, without the use of the Unknown executable name title (from this point, the MessageBox is more flexible than afxmessagebox, it can arbitrarily set the title, and AfxMessageBox cannot).
AfxMessageBox cannot control the message box caption, the internal data output or warning (more often used for debugging) when debugging a program.
Difference Three:
The MessageBox in Win 32 prototype, if the first parameter HWND is set to NULL, the generated non-modal dialog box; AfxMessageBox generates a modal dialog box that does not run down when you do not confirm, it blocks your current thread, Unless your program is a multithreaded program, only the Wait modal dialog box is confirmed.
Attached: VC can be called functions can be broadly divided into three categories
1, the class own function, only has the function to the class own data member. For example, the prototype of the MessageBox in MFC, which is a member function belonging to the CWnd class, can only be called in objects of the derived classes of CWnd and CWnd;
2, AFX Team Design Application Framworks when the global function, multi-crowns in the AFX prefix, in the MFC library/framework contains the project is available. For example, AfxMessageBox can be called from anywhere.
3. Global functions for Windows API. The programming of all Windows platforms can be called, such as Vb,vc,dephi and so on. For example, the prototype of the MessageBox in Win32:: MessageBox (), which is the global function of the Win32 API.
Here are the different prototypes of the MessageBox in 1 and 3:1, the prototype of the MessageBox is less than 3 of a parameter, that is, the window handle HWND, the handle is a zigzag through the this pointer, do not need our concern.
The usage and difference between MessageBox and AfxMessageBox in "turn" VC