MessageBox()用法
消息框是個很常用的控件,屬性比較多,本文列出了它的一些常用方法,及指出了它的一些應用場合。
1.MessageBox("這是一個最簡單的消息框!");
2.MessageBox("這是一個有標題的消息框!","標題");
3.MessageBox("這是一個確定 取消的消息框!","標題", MB_OKCANCEL );
4.MessageBox("這是一個警告的消息框!","標題", MB_ICONEXCLAMATION );
5.MessageBox("這是一個兩種屬性的消息框!","標題", MB_ICONEXCLAMATION|MB_OKCANCEL );
6.if(MessageBox("一種常用的應用","標題",MB_ICONEXCLAMATION|MB_OKCANCEL)==IDCANCEL)
return;
附其它常用屬性
系統默認圖標,可在消息框上顯示
X錯誤 MB_ICONHAND, MB_ICONSTOP, and MB_ICONERROR
?詢問 MB_ICONQUESTION
!警告 MB_ICONEXCLAMATION and MB_ICONWARNING
i信息 MB_ICONASTERISK and MB_ICONINFORMATION
按鈕的形式
MB_OK 默認
MB_OKCANCEL 確定取消
MB_YESNO 是否
MB_YESNOCANCEL 是否取消
返回值
IDCANCEL 取消被選
IDNO 否被選
IDOK 確定被選
IDYES 是被選
以上消息框的用法是在CWnd的子類中的應用,
如果不是,則要MessageBox(NULL,"ddd","ddd",MB_OK);
或MessageBox(hWnd,"ddd","ddd",MB_OK);
hWnd為某窗口的句柄,或者直接用AfxMessageBox。
LPCTSTR lpszText,
UINT nType = MB_OK,
UINT nIDHelp = 0
);
lpszText為顯示內容。
nType 基本類型:
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.
AfxMessageBox()與MessageBox()的區別
帶afx的是全局函數,可以在程序任何地方使用,不帶的是CWnd的子函數,只能在CWnd窗口類對象里面使用AfxMessageBox的函數原型
int AfxMessageBox( LPCTSTR lpszText, UINT nType = MB_OK, UINT nIDHelp = 0 );
int AFXAPI AfxMessageBox( UINT nIDPrompt, UINT nType = MB_OK, UINT nIDHelp = (UINT) –1 );
在第一種形式中,lpszText表示在消息框內部顯示的文本,消息框的標題為應用程序的可執行文件名(如Hello)。在第二種形式中,nIDPrompt為要顯示的文本字符串在字符串表中的ID。函數調用時會自動從字符串表中載入字符串並顯示在消息框中。nType為消息框中顯示的按鈕風格和圖標風格的組合,可以采用|(或)操作符組合各種風格。按鈕風格
MB_ABORTRETRYIGNORE消息框中顯示Abort、Retry、Ignore按鈕
MB_OK顯示OK按鈕
MB_OKCANCEL 顯示OK、Cancel按鈕
MB_RETRYCANCEL 顯示Retry、Cancel按鈕
MB_YESNO 顯示Yes、No按鈕
MB_YESNOCANCEL 示Yes、No、Cancel按鈕
圖標風格
MB_ICONINFORMATION 顯示一個i圖標,表示提示
MB_ICONEXCLAMATION 顯示一個驚嘆號,表示警告
MB_ICONSTOP 顯示手形圖標,表示警告或嚴重錯誤
MB_ICONQUESTION 顯示問號圖標,表示疑問與AfxMessageBox類似的函數MessageBox,它是CWnd的類成員函數:
int MessageBox( LPCTSTR lpszText,LPCTSTR lpszCaption = NULL,UINT nType = MB_OK );兩個函數的區別:AfxMessageBox比 MessageBox簡單一些,因為它是一個全局函數所以不需要對應的一個窗口類,但是不能控制消息框標題,常用於調試程序時的內部數據輸出或警告;MessageBox比較正式,常用在要提交的應用程序版本中,可以控制標題內容而不必采用含義不明的可執行文件名為標題。
舉例:
AfxMessageBox(“Are you sure?”,MB_YESNO|MB_ICONQUESTION);
int a = MessageBox(TEXT( "是否確認刪除?" ), TEXT("Warning!!"),4);
if (a == 6)
AfxMessageBox("Yes");
else
AfxMessageBox("No");
(其中#define IDYES 6 #define IDNO 7)VC中可調用的函數大致可分三類:
1.類自己的函數,只對類自己的數據成員有作用;
2.AFX小組在設計 Application Framworks 時設計的全局函數,多冠在Afx前綴,在包含了MFC庫/框架的工程中可用;
3.Windows API的全局函數。對所有Windows平台下的程序設計都可以調用,如Vb,Vc,Dephi等等。你說的 MessageBox是屬於 CWnd 類的成員函數,只能在 CWnd 和CWnd的派生類的對象中調用;AfxMessageBox則可在任何地方調用。另外對應的還有: ::MessageBox()這個windows API的全局函數。上述中1和3一般有一個區別,就是1要比3少一個參數,即窗口句柄。大家知道,這個句柄是通過 this 指針曲折轉換得到的,不用程序員操心了。
如何修改AfxMessageBox的標題? 應用程序的exe的名稱可以隨意更改,但是如果程序內部彈出一個系統對話框,這時候就會發現對話框的標題和應用程序的名稱不一致了。怎么辦呢?
系統內部彈出的對話框一般調用的是AfxMessageBox()函數,那就檢查一下這個函數吧。一路跟下去,最后發現到了CWinApp類中。
int CWinApp::ShowAppMessageBox(CWinApp *pApp, LPCTSTR lpszPrompt, UINT nType, UINT nIDPrompt)紅字的地方是關鍵,最終{// disable windows for modal dialogDoEnableModeless(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 possibleDWORD* pdwContext = NULL;DWORD dwWndPid=0;GetWindowThreadProcessId(hWnd,&dwWndPid);if (hWnd != NULL && dwWndPid==GetCurrentProcessId() ){// use app-level context or frame level contextLRESULT lResult = ::SendMessage(hWnd, WM_HELPPROMPTADDR, 0, 0);if (lResult != 0)pdwContext = (DWORD*)lResult;}// for backward compatibility use app context if possibleif (pdwContext == NULL && pApp != NULL)pdwContext = &pApp->m_dwPromptContext;DWORD dwOldPromptContext = 0;if (pdwContext != NULL){// save old prompt context for restoration laterdwOldPromptContext = *pdwContext;if (nIDPrompt != 0)*pdwContext = HID_BASE_PROMPT+nIDPrompt;}// determine icon based on type specifiedif ((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 MB_ABORTRETRYIGNORE:case MB_RETRYCANCEL:// No default icon for these types, since they are rarely used.// The caller should specify the icon.break;}}#ifdef _DEBUGif ((nType & MB_ICONMASK) == 0)TRACE(traceAppMsg, 0, "Warning: no icon specified for message box.\n");#endifTCHAR 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 possibleif (pdwContext != NULL)*pdwContext = dwOldPromptContext;// re-enable windowsif (hWndTop != NULL)::EnableWindow(hWndTop, TRUE);DoEnableModeless(TRUE);return nResult;}pszAppName的值會指向當前應用程序的一個字符串m_pszAppName,這個值是開放的可以被動態修改的。如此一來,在應用程序初始化之前修改m_pszAppName這個值就會更改AfxMessageBox的標題了。 那這個m_pszAppName的值怎么修改呢?很簡單。 WCHAR* szAppName = new WCHAR[MAX_PATH];
lstrcpy(szAppName , L"TargetTitle");
AfxGetApp()->m_pszAppName = szAppName;