The EV_COMMAND macro requires the signature of the event-handling function to take no
parameters and return void. So the signature of the event-handling function for the
File Open menu choice looks like this:
void CmFileOpen();
Adding event identifiers
You need to add identifiers for each of these menu choices. Here's
the definition of the event identifiers:
#define CM_FILENEW 201
#define CM_FILEOPEN 202
#define CM_FILESAVE 203
#define CM_FILESAVEAS 204
#define CM_ABOUT 205
These identifiers are contained in the file
STEP06.RC. The ObjectWindows style places the definitions of identifiers in the resource
script file, instead of a header file. This cuts down on the number of source files
required for a project, and also makes it easier to maintain the consistency of identifier
values between the resources and the application source code.
The actual resource definitions in the resource file are contained in a
block contained in an #ifndef/#endif block, like so:
#ifdef RC_INVOKED
// Resource definitions here.
#endif
RC_INVOKED is defined by all resource compilers,
but not by C++ compilers. The resource information is never seen during C++ compilation.
Identifier definitions should be placed outside this #ifndef/#endif block,
usually at the beginning of the file.
Adding menu resources
For now, you want to add five menu choices to the application:
Each of these menu choices needs to
associated with the correct event identifier; that is, the File Open menu choice should
send the CM_FILEOPEN event.
The menu resource is attached to the application in the
InitMainWindow function. You need to call the main window's AssignMenu function. To get
the main window, you can call the GetMainWindow function. The InitMainWindow function
should look like this:
void InitMainWindow()
{
SetMainWindow(new TFrameWindow(0, "Drawing Pad", new TDrawWindow));
GetMainWindow()->AssignMenu("COMMANDS");
}
Adding response table entries
Each event identifier needs to be associated with its corresponding
handler. To do this, add the following lines to the response table:
EV_COMMAND(CM_FILENEW, CmFileNew),
EV_COMMAND(CM_FILEOPEN, CmFileOpen),
EV_COMMAND(CM_FILESAVE, CmFileSave),
EV_COMMAND(CM_FILESAVEAS, CmFileSaveAs),
EV_COMMAND(CM_ABOUT, CmAbout),
Adding event handlers
Now you need to add a function to handle each of the events you've
just added to the response table. Because these functions will eventually grow rather
large, you should declare them in the class declaration and define them outside the class
declaration.
The declarations of these function should look something like this:
void CmFileNew();
void CmFileOpen();
void CmFileSave();
void CmFileSaveAs();
void CmAbout();
Implementing the event handlers
The last step in implementing the event handlers is defining the
functions. For now, leave the implementation of these functions to a bare minimum. Most of
them can just pop up a message box saying that the function has not yet been implemented.
The functions that are set up this way are CmFileOpen, CmFileSave, CmFileSaveAs, and
CmAbout. Here's how these functions look:
void
TDrawWindow::CmFileOpen()
{
MessageBox("Feature not implemented", "File Open", MB_OK);
}
The only function that's implemented in this step
is the CmFileNew function. That's because it's very easy to set up. All that needs to be
done is to clear the array of points and erase the window. The CmFileNew function looks
like this:
void
TDrawWindow::CmFileNew()
{
Line->Flush();
Invalidate();
}
Where to find more information
Here's a guide to where you can find more information on the topics
introduced in this step: