Saturday, 9 March 2013

Creating a Property Sheet Class


Creating a Property Sheet Class

At this point, you've done all the resource editing and don't need to have so many
windows open. Choose Window, Close All from the menu bar and close the properties
box. You'll now create a property sheet class that displays the property pages already
created. Follow these steps:
1. Bring up ClassWizard and click the Add Class button. A tiny menu appears

below the button; choose New. The New Class dialog box appears.
2. In the Name box, type CPropSheet, select CPropertySheet in the Base Class box,
and then click OK.
3. ClassWizard creates the CPropSheet class. Click the MFC ClassWizard
Properties sheet's OK button to finalize the class.
Mow you have three new classes - CPage1, CPage2, and CPropSheet - in your program.
The first two classes are derived from MFC's CPropertyPage class, and the third is
derived from CPropertySheet. Although ClassWizard has created the basic source-code
files for these new classes, you still have to add code to the classes to make them work
the way you want. Follow these steps to complete the Property Sheet Demo application:
1. Click the ClassView tab to display the ClassView window. Expand the Propsheet
classes, as shown Figure 12.13.
2. Double-click CPropSheet to open the header file for your property sheet class.
Because the name of this class (CPropSheet) is so close to the name of the
application as a whole (PropSheet), you'll find CPropSheet in PropSheet1.h,
generated by ClassWizard when you created the new class.
3. Add the following lines near the middle of the file, right before the
CPropSheet class declaration:
#include "page1.h"
#include "page2.h"
These lines give the CPropSheet class access to the CPage1 and CPage2 classes so
that the property sheet can declare member variables of these property page
classes.
FIG. 12.13 The ClassView window lists the classes that make up your project.
4. Add the following lines to the CPropSheet class's //Attributes section, right
after the public keyword:
CPage1 m_page1;
CPage2 m_page2;
These lines declare the class's data members, which are the property pages that
will be displayed in the property sheet.
5. Expand the CPropSheet class in the ClassView pane, and double-click the first
constructor, CPropSheet. Add these lines to it:

AddPage(&m_page1);
AddPage(&m_page2);
This will add the two property pages to the property sheet whenever the sheet is
constructed.
6. The second constructor is right below the first; add the same lines there.
7. Double-click CPropsheetView in ClassView to edit the header file, and add the
following lines to the //Attributes section, right after the line CPropsheetDoc*
GetDocument();:
protected:
CString m_edit;
BOOL m_check;
These lines declare two data members of the view class to hold the selections
made in the property sheet by users.
8. Add the following lines to the CPropsheetView constructor:
m_edit = "Default";
m_check = FALSE;
These lines initialize the class's data members so that when the property sheet
appears, these default values can be copied into the property sheet's controls.
After users change the contents of the property sheet, these data members will
always hold the last values from the property sheet, so those values can be
restored to the sheet when needed.
9. Edit CPropsheetView::OnDraw() so that it resembles Listing 12.1. The new code
displays the current selections from the property sheet. At the start of the
program, the default values are displayed.
Listing 12.1 CPropsheetView::OnDraw()
void CPropsheetView::OnDraw(CDC* pDC)
{
CPropsheetDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
pDC->TextOut(20, 20, m_edit);
if (m_check)
pDC->TextOut(20, 50, "TRUE");
else
pDC->TextOut(20, 50, "FALSE");

}
10. At the top of PropsheetView.cpp, after the #include of propsheet.h, add
another include statement:
#include "propsheet1.h"
11. Bring up ClassWizard, click the Message Maps tab, and make sure that
CPropsheetView is selected in the Class Name box. In the Object IDs box, select
ID_PROPSHEET, which is the ID of the new item you added to the File menu. In the
Messages box, select COMMAND. Click Add Function to add a function that will
handle the command message generated when users choose this menu item. Name
the function OnPropsheet(), as shown in Figure 12.14.
FIG. 12.14 Use ClassWizard to add the OnPropsheet() member function.
The OnPropsheet() function is now associated with the Property Sheet command
that you previously added to the File menu. That is, when users select the
Property Sheet command, MFC calls OnPropsheet(), where you can respond to the
command.
12. Click the Edit Code button to jump to the OnPropsheet() function, and add the
lines shown in Listing 12.2.
Listing 12.2 CPropsheetView::OnPropsheet()
void CPropsheetView::OnPropsheet()
{
CPropSheet propSheet("Property Sheet", this, 0);
propSheet.m_page1.m_edit = m_edit;
propSheet.m_page2.m_checkbox = m_check;
int result = propSheet.DoModal();
if (result == IDOK)
{
m_edit = propSheet.m_page1.m_edit;
m_check = propSheet.m_page2.m_checkbox;
Invalidate();
}
}
The code segment in Listing 12.2, discussed in more detail later in this chapter,
creates an instance of the CPropSheet class and sets the member variables of each
of its pages. It displays the sheet by using the familiar DoModal function first
discussed in Chapter 2, "Dialogs and Controls." If users click OK, it updates the
view member variables to reflect the changes made on each page and forces a
redraw with a call to Invalidate().




No comments:

Post a Comment