1. 文章来自于: http://www.codeproject.com/Tips/120013/Formatted-MessageBox-AfxMessageBox
2. 如果使用了MFC
void AfxMessageBoxFormatted(LPCTSTR pFormatString, ...)
{
va_list vl;
va_start(vl, pFormatString);<br>
CString strFormat;
strFormat.FormatV(pFormatString, vl); // This Line is important!<br>
// Display message box.
AfxMessageBox(strFormat);
}
//这样使用:
AfxMessageBoxFormatted(_T("Name is %s, Age is %d, and salary is %.2f"),
sName, nAge, nSalary);
3. 如果不用MFC
void MessageBoxFormatted(HWND hWnd, LPCTSTR pCaption, LPCTSTR pFormatString, ...)
{
va_list vl;
va_start(vl, pFormatString);<br>
TCHAR strFormat[1024]; // Must ensure size!<br>
// Generic version of vsprintf, works for both MBCS and Unicode builds
_vstprintf(strFormat, pFormatString, vl);<br>
// Or use following for more secure code
// _vstprintf_s(strFormat, sizeof(strFormat), pFormatString, vl)<br>
::MessageBox(hWnd, strFormat, pCaption,MB_ICONINFORMATION);
}
//这样使用
MessageBoxFormatted(NULL, // Or a valid HWND
_T("Information"),
_T("Name is %s, Age is %d, and salary is %.2f"),
sName, nAge, nSalary);
本文介绍了如何在MFC及非MFC环境下创建格式化的消息框。通过使用可变参数列表,可以方便地构建带有格式的字符串并显示为消息框。示例代码展示了如何将变量值插入到字符串中。
526

被折叠的 条评论
为什么被折叠?



