Using Strings with MessageBox

Let's look at an example of a program that uses three character strings to display three error messages in a message box. A header file that we'll call PROGRAM.H defines three identifiers for these messages:

#define IDS_FILENOTFOUND 1

#define IDS_FILETOOBIG 2

#define IDS_FILEREADONLY 3

The resource script looks like this:

#include "program.h"

[other resource script]

STRINGTABLE

{

IDS_FILENOTFOUND, "File %s not found."

IDS_FILETOOBIG, "File %s too large to edit."

IDS_FILEREADONLY, "File %s is read-only."

}

The C source code file also includes this header file and defines a function to display a message box. (I'm assuming that szAppName is a global variable that contains the program name.)

#include "program.h"

[other program lines]

OkMessage (HWND hwnd, WORD wErrorNumber, char *szFileName)

{

char szFormat [40] ;

char szBuffer [60] ;

LoadString (hInst, wErrorNumber, szFormat, 40) ;

sprintf (szBuffer, szFormat, szFilename) ;

return MessageBox (hwnd, szBuffer, szAppName,

MB_OK | MB_ICONEXCLAMATION) ;

}To display a message box containing the ”file not found“ message, the program calls:

OkMessage (hwnd, IDS_FILENOTFOUND, szFileName) ;