Resource Files

Of course, the other new feature introduced in the Graphics example is the top-level menu. Application frameworks (such as MFC) and visual development tools (such as C++Builder, Delphi, and Visual Basic) automatically handle the creation of the menu and its "connections" to the corresponding event handlers. Otherwise, we implement the menu through a resource file that defines the menu items and their associated identifiers. When we select or click on a menu item, Windows sends the program a WM_COMMAND message that includes the menu item's identifier. The program's WM_COMMAND message handler in WindowProc() retrieves this identifier and then calls the corresponding action function (or exits). Here is the resource file used in the Graphics example:


;--------------------------
; Graphics.rc resource file
;--------------------------
#include "Graphics.h"
FGgraphics MENU
BEGIN
   MENUITEM "&Points",     IDM_Points
   MENUITEM "&Lines",      IDM_Lines
   MENUITEM "&Rectangles", IDM_Rectangles
   MENUITEM "&Circles",    IDM_Circles
   MENUITEM "&Ellipses",   IDM_Ellipses
   MENUITEM "P&olygons",   IDM_Polygons
   MENUITEM "P&aint",      IDM_Paint
   MENUITEM "E&xit",       IDM_Exit
END

The IDM_xxx identifiers are defined in Graphics.h, a header file included by both the resource file and the C/C++ source code file:


//-----------------------
// Graphics.h header file
//-----------------------
#define IDM_Points     1
#define IDM_Lines      2
#define IDM_Rectangles 3
#define IDM_Circles    4
#define IDM_Ellipses   5
#define IDM_Polygons   6
#define IDM_Paint      7
#define IDM_Exit       8

A separate resource compiler translates a resource script (RC) file to a resource object (RES) file. Some resource compilers automatically bind the resources in the RES file to the EXE file, while others merely produce the RES file and require separate binding. You do not need to keep the RES file once you've bound it to the EXE file. PowerBASIC requires the extra step of converting the RES file to a PBR file using the compiler's PBRES utility. You then specify the PBR file in a #RESOURCE metastatement in your PowerBASIC program.

If you're compiling from an IDE, it's only necessary to add the RC file name to the list of source and library files in the project. The IDE then automatically binds the menu resource to the EXE file. Some IDE's also include special tools for constructing and managing resource files; consult your compiler manual for details.

If you're compiling from the DOS command line, you must explicitly invoke the resource compiler. The following table shows how to do this for each supported compiler that offers command line compilation:

Platform

Resource compiler command-line usage

Borland C++

BRC32 filename.RC after linking

Delphi

BRC32 filename.RC after compiling

PowerBASIC

RC filename.RC then PBRES filename.RES

Symantec C++

RCC filename.RC before compiling, then include filename.RES on the SC command

Visual C++

RC filename.RC after compiling, then include filename.RES on the LINK command

Watcom C/C++

WRC /bt=nt filename.RC after compiling

Besides the inclusion of the Graphics.h header file, we must also change the value of the lpszMenuName field when WinMain() sets up window class structure. Two lines above the call to RegisterClassEx(), we make the lpszMenuName field point to the application name string:

wndclass.lpszMenuName = szAppName;

We must use this same application name for the menu name in the resource file's MENU command. Recall that Windows programs that do not use menus set this field to NULL.

<< Prev

Next >>

Contents
Fastgraph Home Page

 

copyright 2001 Ted Gruber Software, Inc.