« March 2007 »
S M T W T F S
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
You are not logged in. Log in

Programming Tips
Thursday, 1 March 2007
A printf type of MessageBox function which takes variable arguments!
Mood:  bright
Topic: VC++

It's a hard chore for a programmer to display string and integers in a message box, mainly because they have to use CString or sprintf to get these integers in formatted manner...

For eg:

int nAge = 27, nSal=100;
CString csStr;
csStr.Format( _T( "Age: %d, nSal: %d" ), nAge, nSal );
AfxMessageBox( csStr );



How can we automate this whole process? Here is the answer in the form of a function called ShowMsg.

For eg:

    // Shows dialog
void __cdecl ShowMsg( LPCTSTR lpctszTitle_i, const DWORD dwIconType_i, LPCTSTR lpctszMessage_i, ... )
{
        va_list vaArgList;
        va_start( vaArgList, lpctszMessage_i );

        CString csMsg;
        csMsg.FormatV( lpctszMessage_i, vaArgList );

        MessageBox( AfxGetMainWnd()->GetSafeHwnd(),
                    csMsg,
                    lpctszTitle_i,
                    dwIconType_i );

        va_end( vaArgList );
 
}// End ShowMsg


And call this function like wise
ShowMsg( _T( "Title" ), 
MB_OK|MB_ICONINFORMATION,
_T( "My name is %s, my age is %d, my salary is %d" ),
_T( "Nibu babu thomas" ), 27, 100 );
You can customize this function based on your need.

Posted by Nibu babu thomas at 2:24 PM
Updated: Friday, 2 March 2007 2:52 PM

View Latest Entries