Formatted MessageBox


Click here to change the theme.

Here is a little something I have wanted to do and now I have. This combines CString::Format and CWnd::MessageBox into one function. So instead of:

CString Message;
Message.Format("The integer is: %i", i);
Message.MessageBox(Message, "Caption", MB_OK);

You can use this:

FormattedMessageBox("Caption", MB_OK, "The integer is: %i", i);

The disadvantage is that we must supply values for the caption and type instead of allowing the defaults to be used. If you want to use CWnd::MessageBox then you need to make the function a member of the window object. If you want to use AfxMessageBox instead then you can make FormattedMessageBox global also.

int CSomeWnd::FormattedMessageBox(LPCTSTR lpszCaption,
		UINT nType, LPCTSTR lpszFormat, ...) {
CString Message;
va_list argList;
va_start(argList, lpszFormat);
Message.FormatV(lpszFormat, argList);
va_end(argList);
return MessageBox(Message, lpszCaption, nType);
}