[ Home | Syllabus | Course Notes | Assignments | Search]
This discussion is based on the Shapes application from
chapter 4.
It would be useful to build this program by following the directions in the
book. This is get you familiar with visual c++.
There are three ways to create menus:
If you examine the Menu in the resource view, you will notice the following
Open the "menu" folder. There is one menu,with identified "IDR_MAINFRAME" (where is this used in the program?)
double click "IDR_MAINFRAME" to open the menu for editing.
click "shape" to see pop up menu
double click the
"circle" entry, you should see
The "ID" is "ID_SHAPE_CIRCLE" - this is the id of a command message that is sent by the CMenu object created from the information in the resource file to ?? someone who has a message handler for it.
To illustrate the relationship among the menu, menu message, and message handlers, let's add a new shape. For simplicity, let's draw a blue line. Here is the steps required.
In the resource editor, select menu, open it, double click "IDR_MAINFRAME", click "Shape", you should see something like
Double click the blank rectangle below
"Square" to bring up new entry dialog, edit it to look like this
Add command handlers in CChildView. can either do by hand, or use the class wizard (see text p. 222). in the end you should have a message handler that looks like this (with appropriate entries in the header file and message_map (handled by the wizard)
void CChildView::OnShapeLine() { m_nShape = 3; Invalidate (); }
Now we need to change the OnPaint handler to draw a line if "m_nShape" is 3.
case 3: // Line CPen pen(PS_SOLID, 16, RGB (0, 0, 255)); CPen *oldPen = dc.SelectObject(&pen); points[0].x = cx - 45; points[0].y = cy; points[1].x = cx + 45; points[1].y = cy; dc.Polygon (points, 2); dc.SelectObject(oldPen); break;
Almost done - compile the program, try
out the new shape - notice anything different in the way the menu is handled
for the new shape vice the old shapes?
After you select the "Line" shape, that menu item is not checked.
Top update the menu to reflect the current state of the program (that it is
displaying a "Line") you need to add a "OnUpdateShapeLine"
message handler.
This message is sent by the menu object just before it is ready to create
that menu entry to see if you want to modify it (check it, disable, etc).
Let's add a new message handler which looks like this
void CChildView::OnUpdateShapeLine(CCmdUI* pCmdUI) { pCmdUI->SetCheck (m_nShape == 3); pCmdUI->Enable(m_nShape != 3); // disable it if already selected }
Only thing missing is the shortcut key accelerator, open the "resourceView" select "accelerator"' open folder and double click " IDR_MAINFRAME"
Add a the new entry by double clicking the empty
rectangle at the bottom, edit to look like this
Notice that the shortcut key sends the same command message as the menu item
does - therefore we don't need another message handler (of course we could
send a different message ID and write another message handler if we wanted
to)
NOTE: it is important that we route command messages to the CChildView from CFrameWnd since that is where we placed the message handlers. This is handled by the appwizard (click here to see)
Original source code for Shape application
Modified source code for Shape application