Webocreation

Monday, November 23, 2009

Lab 9.1 Modifying Resources and Adding Dixes

Lab 9.1 Modifying Resources and Adding Dixes

In this lab, you will add a dialog box resource to an application, and include dialog data exchange (DDX) and dialog data validation (DDV). You will also use the resource editor to modify the menus and toolbar.

Estimated time to complete this lab: 60 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:

® Create a dialog box template.
® Build a new class based on that template.
® Remove unused menu and toolbar items.
® Add an error string to the string table.
® Write code to handle a modal dialog box within an application.
® Use the new dialog class in an application.

Prerequisites
There are no prerequisites for this lab.
Exercises

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

Exercise 1: Creating the Dialog Box Template

In this exercise, you will create a dialog box in which you choose the files to compare in the two panes of a splitter window.

® Exercise 2: Creating the Dialog Class and Providing for DDX and DDV

In this exercise, you will create a dialog class that is associated with your dialog box template. Then you will add member variables for all controls other than static text controls, and add simple DDX and DDV for the edit controls.

® Exercise 3: Modifying the Menus and Toolbar

In this exercise, you will use the Resource editor to modify the menus and toolbar for the Diff application.

® Exercise 4: Implementing a Modal Dialog Box in an Application

In this exercise, you will write the code to enable the resources you have created in the previous exercises.

® Exercise 5: Using the New Class in an Application

In this exercise, you will write the code to include the new class in an application.



  1. Exercise 1: Creating the Dialog Box Template


The code that forms the basis for this exercise is in \Labs\Ch09\Lab01\Baseline. Copy these files to your working directory.

In this exercise, you will create a dialog box in which you choose the files to compare in the two panes of a splitter window. This dialog box cascades to the Open dialog box from the Common Dialogs library, as shown in the following illustration.

x

This exercise has three parts:

1. Creating the dialog box template

2. Adding controls to the dialog box template

3. Testing the dialog box template and setting the tab order



Creating the Dialog Box Template

In Part 1 of this exercise, use the Dialog editor to create the basic resource.

u Create a new dialog box resource

— On the Insert menu, click Resource, and then select the dialog resource from the list box and then clicking new, or by double-clicking on Dialog.



u Change the title and ID of a dialog box using the resource editor

1. Display the Dialog Properties property sheet by right-clicking anywhere in the window, and then clicking Properties.

2. In the Caption text box on the General tab, type Open Files, the title for the dialog box.

3. In the ID text box, type IDD_OPENFILES.



Adding Controls to the Dialog Box Template

By default, the dialog box template comes with OK and Cancel buttons in the upper-right corner. You will add other common controls to produce a dialog box. The default buttons will be first and second in the tab order, followed by the other controls in the order they are added. For now, do not assign Group or Tab stop properties to any of the controls. Use the following table as a guide for adding the remaining controls. Resize the dialog box frame as needed to contain the required controls.


Note In the static text controls, the ampersand establishes ALT key access to the control that follows in the tab order. A static control cannot have focus, so focus automatically flows to the next control in the tab-order sequence. In this case, the shortcut keys in the static-text labels provide access to the associated edit-box controls.


Add these controls, including their caption strings and IDs.

Control type Caption string ID


Static text &Compare File: IDC_STATIC (default)

Edit box none IDC_EDIT_FILE1

Static text &With File: IDC_STATIC (default)

Edit box none IDC_EDIT_FILE2

Push button Browse... IDC_BUTTON_FILE1_BROWSE

Push button Browse... IDC_BUTTON_FILE2_BROWSE



u Open the IDD_OPENFILES dialog box resource

You will be adding a series of common controls to populate the dialog box. At any time in this process, you can resize the dialog-box frame to contain the controls.



u Add a common control to the dialog box template

These are general instructions. The type of tool you select on the Controls toolbar dictates the type of control that is drawn on the dialog box template.

1. On the Controls toolbar, click the control you want.

ToolTips provide information about the functionality of each tool, as does Visual C++ Help.


Note If the Controls toolbar is not visible, right-click a toolbar and click Controls on the shortcut menu.


2. In the Dialog editor window, click the client area to add a control of the type specified.

As an alternative, you can also simply drag a control from the Controls window to the destination in the client area.

3. Resize and reposition the control as needed.



u Set the caption text of a control

— Right-click the control and click Properties. In the property sheet for that control, type the caption in the Caption edit box.



u Locate and identify tools on the Controls toolbar

— Point to individual tool buttons. A ToolTip that identifies each button should appear as the mouse pointer pauses over each button.



u Add an edit control to the dialog box template

1. Place the Edit control below the Static Text control.

2. Change the ID of the Edit control to IDC_EDIT_FILE1.



u Add an &With File: Static Text control and an edit control below it

— Use IDC_EDIT_FILE2 as the Edit-control ID.



u Add two push buttons, placing one to the right of each edit control

— To the buttons, assign the caption Browse... .

The three dots after these captions will display another dialog box. Assign the IDs IDC_BUTTON_FILE1_BROWSE and IDC_BUTTON_FILE2_BROWSE.



u Align the controls

— Use commands on the Layout menu.



u Save the current file

Testing the Dialog Box Template and Setting the Tab Order

In this final part of the exercise, you will use test mode to check various aspects of control functionality. After you exit test mode, you will change the tab order.

Set a new tab order for the controls in the following order:

® Compare File

® Edit box (for Compare File)

® Browse button (for Compare File)

® With File

® Edit box (for With File)

® Browse button (for With File)

® OK button

® Cancel button



The following illustration shows the correct tab order.

x

u Test the dialog box resource

1. Enter test mode by clicking the test tool on the Dialog toolbar, or by entering Ctrl+T.

2. Press the tab key several times to cycle through the controls.

Notice the effect and ordering.

3. Select an edit control. Type a text string, such as Testing.

4. Click either the OK or Cancel button to exit the test mode.



u Set the tab order for the controls on the dialog box template

1. On the Layout menu, click Tab Order.

The property sheet is hidden. The Dialog editor now displays a number for each of the controls in your dialog box template. By default, the numbers indicate the order in which each control was added to the template.

&nb

No comments:

Post a Comment