Saturday, 9 March 2013

Running the Property Sheet Demo Application


Running the Property Sheet Demo Application

You've finished the complete application. Click the Build button on the Build minibar
(or choose Build, Build) to compile and link the application. Run it by choosing Build,
Execute or by clicking the Execute button on the Build minibar. When you do, you see
the window shown in Figure 12.15.
As you can see, the window displays two values - the default values for the controls in
the application's property sheet. You can change these values by using the property
sheet. Choose File, Property Sheet; the property sheet appears (see Figure 12.16). The
property sheet contains two pages, each of which holds a single control. When you
change the settings of these controls and click the property sheet's OK button, the
application's window displays the new values. Try it!
FIG. 12.15 When it first starts, the Property Sheet Demo application displays default values for the
property sheet's controls.
FIG. 12.16 The application's property sheet contains two pages.
Adding Property Sheets to Your Applications
To add a property sheet to one of your own applications, you follow steps very similar
to those you followed in the previous section to create the demo application:
1. Create a dialog box resource for each page in the property sheet. These
resources should have the Child and Thin styles and should have no system menu.
2. Associate each property page resource with an object of the CPropertyPage
class. You can do this easily with ClassWizard. Connect controls on the property
page to members of the class you create.
3. Create a class for the property sheet, deriving the class from MFC's
CPropertySheet class. You can generate this class by using ClassWizard.
4. In the property sheet class, add member variables for each page you'll be adding
to the property sheet. These member variables must be instances of the property
page classes that you created in step 2.
5. In the property sheet's constructor, call AddPage() for each page in the
property sheet.

6. To display the property sheet, call the property sheet's constructor and then
call the property sheet's DoModal() member function, just as you would with a
dialog box.
After you write your application and define the resources and classes that represent
the property sheet (or sheets - you can have more than one), you need a way to enable
users to display the property sheet when it's needed. In Property Sheet Demo, this is done
by associating a menu item with a message-response function. However you handle the
command to display the property sheet, the process of creating the property sheet is the
same. First, you must call the property sheet class's constructor, which Property Sheet
Demo does like this:
CPropSheet propSheet("Property Sheet", this, 0);
Here, the program creates an instance of the CPropSheet class. This instance (or object)
is called propSheet. The three arguments are the property sheet's title string, a pointer
to the parent window (which, in this case, is the view window), and the zero-based index
of the first page to display. Because the property pages are created in the property
sheet's constructor, creating the property sheet also creates the property pages.
After you create the property sheet object, you can initialize the data members that
hold the values of the property page's controls, which Property Sheet Demo does like
this:
propSheet.m_page1.m_edit = m_edit;
propSheet.m_page2.m_checkbox = m_check;
Now it's time to display the property sheet, which you do just as though it were a dialog
box, by calling the property sheet's DoModal() member function:
int result = propSheet.DoModal();
DoModal() does not take any arguments, but it does return a value indicating which
button users clicked to exit the property sheet. In a property sheet or dialog box, you'll
usually want to process the information entered into the controls only if users clicked
OK, which is indicated by a return value of IDOK. If users exit the property sheet by
clicking the Cancel button, the changes are ignored and the view or document member
variables aren't updated.

No comments:

Post a Comment