Differences between MessageBox and AfxMessageBox in VC

Source: Internet
Author: User

MessageBox () Usage
Message box is a common control with many attributes. This article lists some of its common methods and its application scenarios.
1. MessageBox ("this is the simplest message box! ");
2. MessageBox ("this is a message box with a title! "," Title ");
3. MessageBox ("this is a confirmation message box! "," Title ", MB_OKCANCEL );
4. MessageBox ("this is a warning message box! "," Title ", MB_ICONEXCLAMATION );
5. MessageBox ("this is a message box with two attributes! "," Title ", MB_ICONEXCLAMATION | MB_OKCANCEL );
6. if (MessageBox ("a common application", "title", MB_ICONEXCLAMATION | MB_OKCANCEL) = IDCANCEL)

Additional common attributes

Default System icon, which can be displayed in the message box

Button format
MB_ OK default
MB_OKCANCEL confirm cancel
Whether MB_YESNOCANCEL is canceled

Return Value
IDCANCEL unselect
IDNO no selected
IDOK determines to be selected
IDYES is selected

The above message box is used in the subclass of CWnd,
If not, MessageBox (NULL, "ddd", "ddd", MB_ OK) is required );
Or MessageBox (hWnd, "ddd", "ddd", MB_ OK );
HWnd is the handle of a window, or AfxMessageBox is used directly.

AfxMessageBox usage

Nt AfxMessageBox (
LPCTSTR lpszText,
UINT nType = MB_ OK,
UINT nIDHelp = 0
LpszText is the displayed content.
NType basic type:
MB_ABORTRETRYIGNORE The message box contains three pushbuttons: Abort, Retry, and Ignore.
MB_ OK The message box contains one pushbutton: OK.
MB_OKCANCEL The message box contains two pushbuttons: OK and Cancel.
MB_RETRYCANCEL The message box contains two pushbuttons: Retry and Cancel.
MB_YESNO The message box contains two pushbuttons: Yes and No.
MB_YESNOCANCEL The message box contains three pushbuttons: Yes, No, and Cancel.
Differences between AfxMessageBox () and MessageBox ()

A global function with afx can be used anywhere in the program. A subfunction without CWnd can only be used in a CWnd window class object.

AfxMessageBoxFunction prototype
IntAfxMessageBox(LPCTSTR lpszText, UINT nType = MB_ OK, UINT nIDHelp = 0 );
Int AFXAPIAfxMessageBox(UINT nIDPrompt, UINT nType = MB_ OK, UINT nIDHelp = (UINT)-1 );
In the first form, lpszText indicates the text displayed inside the message box. The title of the message box is the executable file name (such as Hello) of the application ). In the second form, nIDPrompt is the ID of the text string to be displayed in the string table. When the function is called, the string is automatically loaded from the string table and displayed in the message box. NType is a combination of the button style and Icon style displayed in the message box. You can use the | (OR) operator to combine various styles.

Button Style
MB_ABORTRETRYIGNORE The Abort, Retry, and Ignore buttons are displayed in the message box.
MB_ OK Show OK button
MB_OKCANCEL: Show OK and Cancel buttons
MB_RETRYCANCEL: Display Retry and Cancel buttons
MB_YESNO: Yes and No buttons are displayed.
The MB_YESNOCANCEL button indicates Yes, No, and Cancel.
Icon Style
MB_ICONINFORMATION displays an I icon, indicating a prompt
MB_ICONEXCLAMATION displays an exclamation point, indicating a warning.
MB_ICONSTOP: displays the hand icon, indicating a warning or serious error.
MB_ICONQUESTION: displays the question mark icon, indicating a question

AndAfxMessageBoxSimilar function MessageBox, which is a member function of the CWnd class:
Int MessageBox (maid, maid = NULL, UINT nType = MB_ OK );

Differences between the two functions:AfxMessageBoxIt is simpler than MessageBox because it is a global function, so it does not need a corresponding window class, but cannot control the message box title. It is often used for internal data output or warning during program debugging; messageBox is relatively formal. It is often used in the application version to be submitted. You can control the title content without using an executable file name with unknown meanings as the title.

AfxMessageBox("Are you sure ?", MB_YESNO | MB_ICONQUESTION );
Int a = MessageBox (TEXT ("are you sure you want to delete it? "), TEXT (" Warning !! "), 4 );
If (a = 6)
AfxMessageBox("Yes ");
AfxMessageBox("No ");
(Where # define IDYES 6 # define IDNO 7)

Functions that can be called in VC can be roughly divided into three types:

1. Functions of the class itself, which only serve the data members of the class itself;

2. The global functions designed by the AFX team during Application Framworks design. They are prefixed with Afx and are available in projects that contain the MFC Library/framework;

3. Global functions of Windows APIs. You can call programming on all Windows platforms, such as Vb, Vc, and Dephi. MessageBox is a member function of the CWnd class. It can be called only in the objects of the derived classes of CWnd and CWnd. AfxMessageBox can be called anywhere. In addition, it corresponds to the global function of the windows API: MessageBox. In the above, there is a general difference between 1 and 3, that is, 1 is less than 3, that is, the window handle. As you know, this handle is obtained through the twists and turns of this pointer, so you don't have to worry about it.

How can I modify the title of AfxMessageBox?The exe name of the application can be changed at will. However, if a system dialog box is displayed, the title of the dialog box is different from that of the application. What should we do?
The internal pop-up dialog box generally calls the AfxMessageBox () function. Check this function. All the way down, and finally found in the CWinApp class.
Int CWinApp: ShowAppMessageBox (CWinApp * pApp, LPCTSTR lpszPrompt, UINT nType, UINT nIDPrompt)
{// Disable windows for modal dialog DoEnableModeless (FALSE); HWND hWndTop; HWND hWnd = CWnd: GetSafeOwner _ (NULL, & hWndTop); // re-enable the parent window, so that focus is restored // correctly when the dialog is dismissed. if (hWnd! = HWndTop) EnableWindow (hWnd, TRUE); // set help context if possible DWORD * pdwContext = NULL; DWORD dwWndPid = 0; GetWindowThreadProcessId (hWnd, & dwWndPid); if (hWnd! = NULL & dwWndPid = GetCurrentProcessId () {// use app-level context or frame level context LRESULT lResult =: SendMessage (hWnd, WM_HELPPROMPTADDR, 0, 0 ); if (lResult! = 0) pdwContext = (DWORD *) lResult;} // for backward compatibility use app context if possible if (pdwContext = NULL & pApp! = NULL) pdwContext = & pApp-> m_dwPromptContext; DWORD dwOldPromptContext = 0; if (pdwContext! = NULL) {// save old prompt context for restoration later dwOldPromptContext = * pdwContext; if (nIDPrompt! = 0) * pdwContext = HID_BASE_PROMPT + nIDPrompt;} // determine icon based on type specified if (nType & MB_ICONMASK) = 0) {switch (nType & MB_TYPEMASK) {case MB_ OK: case MB_OKCANCEL: nType | = MB_ICONEXCLAMATION; break; case MB_YESNO: case MB_YESNOCANCEL: nType | = MB_ICONQUESTION; break; case when: case MB_RETRYCANCEL: // No default icon for these types, since they are rarely used. // The caller shoshould specify the icon. break ;}# ifdef _ DEBUG if (nType & MB_ICONMASK) = 0) TRACE (traceAppMsg, 0, "Warning: no icon specified for message box. \ n "); # endif TCHAR szAppName [_ MAX_PATH]; szAppName [0] = '\ 0'; LPCTSTR pszAppName; if (pApp! = NULL) pszAppName = pApp-> m_pszAppName; else {pszAppName = szAppName; DWORD dwLen = GetModuleFileName (NULL, szAppName, _ MAX_PATH); if (dwLen = _ MAX_PATH) szAppName [_ MAX_PATH-1] = '\ 0';} int nResult =: AfxCtxMessageBox (hWnd, lpszPrompt, pszAppName, nType ); // restore prompt context if possible if (pdwContext! = NULL) * pdwContext = dwOldPromptContext; // re-enable windows if (hWndTop! = NULL): EnableWindow (hWndTop, TRUE); DoEnableModeless (TRUE); return nResult ;}
The red letter is the key.The value of pszAppName directs to a string m_pszAppName of the current application. This value is open and can be dynamically modified. In this way, modifying the m_pszAppName value before application initialization changes the title of AfxMessageBox. How can I modify the m_pszAppName value? Very simple.
WCHAR * szAppName = new WCHAR [MAX_PATH];
Lstrcpy (szAppName, L "TargetTitle ");
AfxGetApp ()-> m_pszAppName = szAppName;

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.