VC++裡面可以調用的消息框有三種;
一是Win32 API的MessageBox;
一是CWnd的成員,CWnd::MessageBox;
再就是AfxMessageBox();AfxMessageBox()是MFC中的全局函數,函數原型有兩種:
int AfxMessageBox( LPCTSTR lpszText, UINT nType = MB_OK, UINT nIDHelp = 0 );
int AFXAPI AfxMessageBox( UINT nIDPrompt, UINT nType = MB_OK, UINT nIDHelp = (UINT) –1 );
一般用AfxMessageBox()就可以了。
AfxMessageBox(L"消息XXXX");
AfxMessageBox(305, MB_YESNO | MB_ICONSTOP);
CString strTemp = L"消息111";
AfxMessageBox(strTemp, MB_YESNO | MB_ICONQUESTION);
AfxMessageBox(L"消息YYY", AFX_IDS_APP_TITLE);
CString str1;
str1.Format(L"%d",999777);
AfxMessageBox(str1);
CString str2;
str2.Format(L"%d---%s", 999555, L"127.0.0.1");
AfxMessageBox(str2);
AfxMessageBox(L"我要顯示", 0, 0);
//不在CWnd類或者子類中的應用(Win32原型)
::MessageBox(NULL, L"消息1", L"caption1", MB_OK);
::MessageBox(this->m_hWnd, L"消息2", L"caption2", MB_OK);
//在CWnd類或者子類中的應用,CWnd::MessageBox
MessageBox(L"這是一個最簡單的消息框!");
MessageBox(L"這是一個有标題的消息框!", L"标題");
MessageBox(L"這是一個确定 取消的消息框!", L"标題", MB_OKCANCEL);
MessageBox(L"這是一個警告的消息框!", L"标題", MB_ICONEXCLAMATION);
MessageBox(L"這是一個兩種屬性的消息框!", L"标題", MB_ICONEXCLAMATION | MB_OKCANCEL);
if (MessageBox(L"一種常用的應用", L"标題", MB_ICONEXCLAMATION | MB_OKCANCEL) == IDCANCEL)
return;
AfxMessageBox()有以下特性;
可以使用VC字元串資源中的ID顯示一個字元串;前面代碼使用了305;
可以在資源定義一個字元串作為AfxMessageBox的标題,ID一般是AFX_IDS_APP_TITLE;
顯示數值型使用 str1.Format(L"%d",999777); 使用CString類對象的Format()方法;
L字首一般指LPSTR和LPCSTR:LPSTR是指向以0結尾的ANSI字元串的指針,後者是const指針;