Webocreation

Monday, November 23, 2009

Adding a Property Sheet to an Application

Lab 9.3: Adding a Property Sheet to an आप्प्लिकतिओन
In this lab you will learn how to create a property sheet and set the member variables for the property sheet class for later retrieval।

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:

® Use the Component Gallery to create a property sheet.
® Use the Dialog editor to build pages for the property sheet.
® Link pages together using CPropertyPage and CPropertySheet.
® Update an application and its registry from a property sheet.

Prerequisites
There are no prerequisites for this lab.
Exercises
The following exercises provide practice working with the concepts and techniques covered in this chapter.
® Exercise 1: Creating the Property Sheet Template
In this exercise, you will use the property sheet component, a tool in the Component Gallery, to create a property sheet.

® Exercise 2: Implementing the Property Sheet Classes
In this exercise, you will use DDV and DDX to set the member variables for the property sheet class for later retrieval in the calling function.

Exercise 1: Creating the Property Sheet Template

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


The Microsoft Foundation Class Library (MFC) provides property sheets and their pages with classes, CPropertySheet and CPropertyPage. The Developer Studio Gallery provides a wizard component that automates much of the effort of creating property sheets. This exercise will use this tool from the Developer Studio Gallery to create the property sheet for the application.

The following illustrations show the three property pages that you will be adding in the course of this lab.







Create the framework for the property sheet
1. On the Project menu, click Add to Project, and then click Components and Controls. In the Gallery dialog box, select Developer Studio Components.
2. Select the Property Sheet component, click Insert, and then click OK.
You will then be asked whether you want to create a property sheet or a wizard. Click Property Sheet and click Next.
3. In the next page, do not support previewing, and then click No to leave the property sheet as modal.
4. To choose the class that will access this property sheet, click CMainFrame, which is where the main menu resides.
5. Choose three pages.
6. In the next page, you will name the classes for the property sheet and its pages. Use the names in the following table.

Object Class File names

Property sheet CPreferences Prefer.h and Prefer.cpp

Page 1 CColorPage Pages.h and Pages.cpp
Page 2 CComparePage Pages.h and Pages.cpp
Page 3 CFontPage Pages.h and Pages.cpp

7. Click Finish. The Property Sheet wizard will create Prefer.h, Prefer.cpp, Pages.h, and Pages.cpp. It will also create three placeholder dialog resources for the three pages of your property sheet.

Modify the dialog box templates

If you need to review the Dialog editor, see Lab 9.1: Modifying Resources and Adding Dialog Boxes.
1. From the Dialog editor, select the IDD_PROPPAGE1 dialog box. Delete the static text box provided. Change the ID of the dialog box, and create controls according to the following table.

Control type ID Caption

Dialog IDD_PAGE_COLORS Colors

Static text IDC_STATIC Foreground
Static text IDC_STATIC Background
Combo box IDC_COMBO_COLOR_FORE None
Combo box IDC_COMBO_COLOR_BACK None

2. Select the IDD_PROPPAGE2 dialog box. Delete the static text box provided. Change the ID of the dialog box, and create controls according to the following table.

Control Type ID Caption

Dialog IDD_PAGE_COMPARE Compare

Slider IDC_TRACKBAR1 (not applicable) Set: Tickmarks, Autoticks, Border

Group box IDC_STATIC Read ahead optimization.
Static text IDC_STATIC None
Static text IDC_STATIC Full
Group box IDC_STATIC Constraints
Check box IDC_CHECK_IGNORE_CASE &Ignore Case
Check box IDC_CHECK_IGNORE_WHITE Ignore &Leading Whitespace

3. Select the IDD_PROPPAGE3 dialog box. Delete the static text provided. Change the ID of the dialog box, and create controls according to the following table.

Control Type ID Caption

Dialog IDD_PAGE_FONT Font

Edit box IDC_EDIT_FONT None
Edit box IDC_EDIT_SIZE None
Push button IDC_BUTTON_FONT Change Font...
Static text IDC_STATIC_FONT Font:
Static text IDC_STATIC_SIZE Size:


Change the resource identifiers for the property pages
1. Open Pages.h.
2. In each of the class definitions is a dialog data section. For CColorPage, this section is the following code:
// Dialog Data
//{{AFX_DATA(CColorPage)
enum { IDD = IDD_PROPPAGE1 };
// NOTE - ClassWizard will add data members here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_DATA

Ignore the warning and change IDD_PROPPAGE1 to IDD_PAGE_COLORS, the new identifier r this dialog box।
3. Repeat for the other two classes, changing IDD_PROPPAGE2 to IDD_PAGE_COMPARE and IDD_PROPPAGE3 to IDD_PAGE_FONT.

Change the caption of the property sheet

1. Open the string table resource.
2. IDS_PROPSHT_CAPTION is the caption string for the property sheet. Open its properties and change its caption to Preferences.

Add a menu item for the property sheet
1. Open the IDR_MAINFRAME menu resource.
2. Add a separator at the end of the View menu.
3. Add a new menu item, ID_VIEW_PREFERENCES with a caption &Preferences, to the end of the View menu.

Activate the property sheet

1. From the View menu, start ClassWizard. Click the Message Maps tab, and then select CMainFrame as the class name, ID_VIEW_PREFERENCES as the object ID, and COMMAND as the message.
2. Create a new command handler by adding a new function and accepting the default name, OnViewPreferences.
3. Edit the code for OnViewPreferences.
4. Call the CMainFrame::OnProperties function that is provided by the Property Sheet wizard.
5. Save all files. The complete function follows.
void CMainFrame::OnViewPreferences()
{
OnProperties();
}

The completed code for this exercise is in \Labs\Ch09\Lab03\Ex01.

Exercise 2: Implementing the Property Sheet Classes

Continue with the files you created in Exercise 1, or if you do not have a starting point for this exercise, the code that forms the basis for this exercise is in \Labs\Ch09\Lab03\Ex01.

You can think of each page of a property sheet as an individual dialog box. As with all dialog boxes, you can use dialog data validation (DDV) and dialog data exchange (DDX) to set member variables for later retrieval in the calling function.

In this lab, you will implement the CComparePage and CFontPage classes, you may also choose to implement an owner-draw combo box class to display colors for this page.

Rename the member variables for pages in CPreferences

1. Open Prefer.h.
2. Change the names of the member variables to more informative names.
CColorPage m_ColorPage;
CComparePage m_ComparePage;
CFontPage m_FontPage;

3. Save Prefer.h.
4. Open Prefer.cpp, and change the calls to CPropertySheet::AddPage to use the new names, as shown in the following code:
CPreferences::CPreferences(CWnd* pWndParent)
: CPropertySheet(IDS_PROPSHT_CAPTION, pWndParent)
{
AddPage(&m_ColorPage);
AddPage(&m_ComparePage);
AddPage(&m_FontPage);
}
5. Save Prefer.cpp.

Implement CComparePage
1. Open ClassWizard, select Member Variables tab, and choose CComparePage for the class name. Select the Control IDs shown in the following table one at a time. Click the Add Variable button to add the variable names shown for each of the Control IDs.

Control ID Type Variable name

IDC_CHECK_IGNORE_CASE BOOL m_bIgnoreCase

IDC_CHECK_IGNORE_WHITE BOOL m_bIgnoreWhiteSpace
IDC_TRACKBAR1 CSliderCtrl m_Slider
2. Add a protected member for the trackbar’s position in class CComparePage.
UINT m_uReadAheadOptLevel;
3. Add member functions in class CComparePage for setting and getting member variables.
// Attributes
public:
void SetIgnoreCase(BOOL bIgnore){ m_bIgnoreCase = bIgnore; }
void SetIgnoreWhiteSpace(BOOL bIgnore) { m_bIgnoreWhiteSpace = bIgnore; }
void SetReadAheadOptLevel(UINT nLevel) { m_uReadAheadOptLevel = nLevel; }
BOOL GetIgnoreCase() const { return m_bIgnoreCase; }
BOOL GetIgnoreWhiteSpace() const { return m_bIgnoreWhiteSpace; }
UINT GetReadAheadOptLevel() const { return m_uReadAheadOptLevel; }
4. Using ClassWizard, add a handler for WM_INITDIALOG. In this handler, set the range of the slider from 0 to 4.
BOOL CComparePage::OnInitDialog()
{
CPropertyPage::OnInitDialog();
m_Slider.SetRange(0, 4);
return TRUE;
}
5. In CComparePage::DoDataExchange, add a routine to set or get the position of the slider. CDataExchange.m_bSaveAndValidate is True if data is being read from the controls in the dialog box, and False if data is being written to the controls.
if (pDX->m_bSaveAndValidate)
{
m_uReadAheadOptLevel = m_Slider.GetPos();
}
else
{
m_Slider.SetPos(m_uReadAheadOptLevel);
}
6. Save Pages.h and Pages.cpp.

Implement CFontPage
1. Using ClassWizard, add member variables for two of the static controls in this template.
Control ID Type Variable name

IDC_STATIC_FONT CString m_FontName

IDC_STATIC_SIZE CString m_FontSize

2. Declare a variable to hold the character format.
CHARFORMAT m_CharacterFormat;
3. Declare functions to get and set the character format.
public:
void SetCharacterFormat(CHARFORMAT& CharFormat);
void GetCharacterFormat(CHARFORMAT& CharFormat) const;
4. Add the implementation for these functions in the file, Pages.cpp.
void CFontPage::SetCharacterFormat(CHARFORMAT& CharFormat)
{
m_CharacterFormat = CharFormat;
}

void CFontPage::GetCharacterFormat(CHARFORMAT& CharFormat) const

{
CharFormat = m_CharacterFormat;
}

5. Using ClassWizard, add a handler for BN_CLICKED on IDC_BUTTON_FONT. Go to the code that was created.
6. Create a common font dialog box.
CFontDialog dlg(m_CharacterFormat);
7. Show the dialog box.
if(dlg.DoModal() == IDOK)
8. Retrieve the character format into m_CharacterFormat.
dlg.GetCharFormat(m_CharacterFormat);
9. Update the display.
UpdateData(FALSE);
10. The complete function follows.
void CFontPage::OnButtonFont()
{
CFontDialog dlg(m_CharacterFormat);

if(dlg.DoModal() == IDOK)

{
dlg.GetCharFormat(m_CharacterFormat);
UpdateData(FALSE);
}
}
11. Add code to the beginning of CFontPage::DoDataExchange to convert m_CharacterFormat to the member variables, as shown in the following code:
if (!pDX->m_bSaveAndValidate) //updating the controls
{
// Update FaceName and Size CString variables that
// are associated with statics on the page.
m_FontName = m_CharacterFormat.szFaceName;
wsprintf(m_FontSize.GetBufferSetLength(32), "%ld Points", m_CharacterFormat.yHeight/20);
m_FontSize.ReleaseBuffer();
}
12. Save Pages.cpp and Pages.h.
13. Build and run Diff.exe.
The code for this completed exercise is in \Labs\Ch09\Lab03\Ex02.

No comments:

Post a Comment