Webocreation

Monday, November 23, 2009

Adding Static Drop-down Menus

Lab 8.1: Adding Static Drop-down Menus
In this lab, you will add functionality to the Scribble application. Using the Menu editor, you will provide a top-level menu and submenu that enable the user to set the width of the drawing pen. You will then add a radio button indicator to show the state of a menu item.


The following illustration shows you what the complete lab should look like.



To see a demonstration of the lab solution, click this icon.
Estimated time to complete this lab: 30 minutes
To complete the exercises in this lab, you must have the required software. For detailed information about the labs and setup for the labs, see Labs in this course.

Objectives

After completing this lab, you will be able to:

® Edit a menu resource.
® Use ClassWizard to add COMMAND handlers.
® Implement handlers.
® Use ClassWizard to add UPDATE_COMMAND_UI handlers.
® Set radio button or check box indicators in menus.
® Add buttons to the toolbar.

Prerequisites

Before working on this lab, you should be familiar with the following:

® In this lab, you use the Scribble application contained in the product documentation. For more information, see the online Visual C++ Tutorials in \Samples\MFC Samples\Tutorials\Scribble.
® This exercise requires that you be able to use AppWizard to create a simple single document interface (SDI) application.

Exercises

The following exercises provide practice with the concepts and techniques covered in this chapter:

® Exercise 1: Implementing a Static Drop-down Menu
In this exercise, you will create and implement a menu that controls the pen width in the Scribble application.
® Exercise 2: Implementing State Indicators
In this exercise, you will add a radio button to indicate the pen width that is currently in effect in the Scribble application.
® Exercise 3: Adding Toolbar Buttons
In this exercise, you will implement toolbar support by adding two buttons to the toolbar and connecting them to work with the menu.


Exercise 1: Implementing a Static Drop-down Menu
The code that forms the basis for this exercise is in \Labs\Ch08\Lab01\Baseline. Copy these files to your working directory.

In this exercise, you will create and implement a menu that controls the pen width in the Scribble application.

Add a Pen menu with Width and Thick and Thin drop-down menus

1. Open the project workspace to ResourceView. Open the Menu folder, and then open the IDR_SCRIBBTYPE resource.

The following illustration shows what your interface should look like.



2. In the Menu editor, click the dotted box to the right of Help and drag it to the position between the View and Window items.
3. Double-click the dotted box to display the Menu Item Properties property sheet. Set the caption to &Pen, which will display as Pen. To keep the Properties property sheet on top, click the pushpin in the upper-left corner of the sheet.
4. Click the dotted box below the Pen menu. Set the caption of this menu item to &Width. Because this item has a submenu, select the Pop-up property.
5. Click the dotted box to the right of the Width menu item. Set this item's caption to Thi&n. The Menu editor assigns ID_PEN_WIDTH_THIN as this item's ID when you go to another menu item, so you can ignore the ID field. Set the prompt of this item to "Change pen style to a thin line."
6. Click the dotted box below the Thin menu item. Set the caption of this item to Thic&k. The Menu editor will assign ID_PEN_WIDTH_THICK as this item's ID when you select another menu item, so you can ignore the ID field. Set the message string of this item to "Change pen style to a thick line." The following illustration shows what the Menu Item Properties property sheet should look like.



7. Close the Menu Item Properties property sheet.

Use ClassWizard to add handlers for the Thick and Thin menu items

1. On the View menu, click ClassWizard, or press CTRL+W. Click the Message Maps tab.
2. Select CScribbleDoc as the class name, ID_PEN_WIDTH_THIN as the object ID, and COMMAND as the message. Click Add Function to add the function and accept the default OnPenWidthThin as the function name.
3. Repeat this procedure to add the default handler for ID_PEN_WIDTH_THICK.

Note Consider the following guidelines when implementing handlers:

— In general, put handlers in the command-target class where they have the widest scope needed.
— When attributes are shared by multiple views or frame windows, put them in the common document.
— If attributes are not shared, put them in the view(s) or window(s) that use them.

4. Click Edit Code to go to ScribDoc.cpp in the OnPenWidthThick handler. Replace the commented line:

// TODO: Add your command handler code here with:

ChangePen(THICK);

You have not yet defined ChangePen or THICK; you can do this in the next section.

5. Replace the comment in OnPenWidthThin with:

ChangePen(THIN);

6. Save Scribdoc.cpp. The complete functions follow.

void CScribbleDoc::OnPenWidthThin()
{
ChangePen(THIN);
}
void CScribbleDoc::OnPenWidthThick()
{
ChangePen(THICK);
}

Provide a function to update the pen width

Scribble has a member that holds the document's current pen, m_penCur, and a member that holds the current pen's width.

1. Right-click CScribbleDoc in ClassView, and add ChangePen as a protected function.
void CScribbleDoc::ChangePen(PENWIDTH penWidth)
2. Set the current width member to the passed width. Write code for ChangePen as follows.
m_nPenWidth = penWidth;
3. Because Windows GDI objects are a limited resource, destroy them when they are no longer needed. Scribble does not need the old pen once a new one has been created, so delete the current GDI pen associated with the m_penCur object.

m_penCur.DeleteObject();

4. Create a new GDI pen to associate with the m_penCur object.
m_penCur.CreatePen( PS_SOLID,
m_nPenWidth,
RGB(0,0,0));
5. Save Scribdoc.cpp. The complete function follows.
void CScribbleDoc::ChangePen(PENWIDTH penWidth)
{
m_nPenWidth = penWidth;
m_penCur.DeleteObject();
m_penCur.CreatePen( PS_SOLID,
m_nPenWidth,
RGB(0,0,0));
}

Integrate the changes into Scribble

While you can define a thick pen of five pixels and a thin pen of two pixels by hard-coding these values, this makes the code difficult to maintain. Instead, enumerate these values.

1. Open ScribDoc.h.
2. In the protected attributes section of the CScribbleDoc class declaration, define PENWIDTH.
enum PENWIDTH {THIN = 2, THICK = 5};
3. Change the type of m_nPenWidth from UINT to PENWIDTH.
PENWIDTH m_nPenWidth;
4. Save ScribDoc.h.
5. Open ScribDoc.cpp and find the InitDocument function.
6. Replace the following:
m_nPenWidth = 2;
with its enumerated equivalent:
m_nPenWidth = THIN;
7. Save ScribDoc.cpp.
8. Build and run Scribble.

The completed code for this exercise is in \Labs\Ch08\Lab01\Ex01

No comments:

Post a Comment