自定义扩展MessageBox(Formatted MessageBox/AfxMessageBox)

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);


你可能感兴趣的:(list,null,扩展)