Popup Information

Another handy use of a message box during program development is to provide information to you while the program is executing. It would be ideal if you could use a message box much as you use printf in C programs for MS-DOS, with a formatting string and a variable number of arguments. And in fact, you can create a function that lets you do this:

void OkMsgBox (char *szCaption, char *szFormat, ...)

{

char szBuffer [256] ;

char *pArguments ;

pArguments = (char *) &szFormat + sizeof szFormat ;

vsprintf (szBuffer, szFormat, pArguments) ;

MessageBox (NULL, szBuffer, szCaption, MB_OK) ;

}

The vsprintf function is similar to sprintf except that it uses a pointer to a series of arguments (pArguments) rather than the arguments themselves. OkMsgBox sets pArguments to the arguments on the stack when OkMsgBox is called. The first parameter to OkMsgBox is the message box caption, the second parameter is a format string, and the third and subsequent parameters are values to be displayed. Let's say you want a message box to appear every time the window procedure gets a WM_SIZE message. Your code might look like this:

case WM_SIZE :

OkMsgBox ("WM_SIZE Message",

"wParam = %04X, lParam = %04X-%04X",

wParam, HIWORD (lParam), LOWORD (lParam)) ;

[other program lines]

return 0 ;

This displays the values of wParam and lParam within the message box.