📄 ch12.htm
字号:
<P>
<DT></DT>
<DD>The Child style is necessary because the property page will be a child window
of the property sheet. The property sheet itself will provide the container for the
property pages.
<P>
</DL>
<P><A HREF="javascript:popUp('12uvc08.gif')"><B>FIG. 12.8</B></A><B> </B><I>A property
page uses styles different from those used in regular dialog boxes.</I></P>
<P>
<DL>
<DT><I></I></DT>
<DD><B>5. </B>Add an edit box to the property page, as shown in Figure 12.9. In most
applications you would change the resource ID from IDC_EDIT1, but for this demonstration
application, leave it unchanged.
<P>
<DT></DT>
<DD><B>6. </B>Create a second property page by following steps 1 through 5 again.
For this property page, use the ID IDD_PAGE2DLG, a caption of Page 2, and add a check
box rather than an edit control (see Figure 12.10).
<P>
</DL>
<P><A HREF="javascript:popUp('12uvc09.gif')"><B>FIG. 12.9</B></A><B> </B><I>A property
page can hold whatever controls you like.</I></P>
<P><A HREF="javascript:popUp('12uvc10.gif')"><B>FIG. 12.10</B></A><B> </B><I>The
second property page looks like this.</I></P>
<H3><I></I></H3>
<H3><A NAME="Heading6"></A>Associating Your Resources with Classes</H3>
<P>You now have all your resources created. Next, associate your two new property-page
resources with C++ classes so that you can control them in your program. You also
need a class for your property sheet, which will hold the property pages that you've
created. Follow these steps to create the new classes:</P>
<DL>
<DD><B>1. </B>Make sure that the Page 1 property page is visible in the dialog box
edit area and then double-click it. If you prefer, choose View, ClassWizard from
the menu bar. The MFC ClassWizard property sheet appears, displaying the Adding a
Class dialog box first discussed in Chapter 2, "Dialogs and Controls."
<P>
<DT></DT>
<DD><B>2. </B>Select the Create New Class option and then click OK. The New Class
dialog box appears.
<P>
<DT></DT>
<DD><B>3. </B>In the Name box, type <B>CPage1</B>. In the Base Class box, select
CPropertyPage. (Don't accidentally select CPropertySheet.) Then click OK to create
the class.
<P>
<DT></DT>
<DD>You've now associated the property page with an object of the CPropertyPage class,
which means that you can use the object to manipulate the property page as needed.
The CPropertyPage class will be especially important when you learn about wizards.
<P>
<DT></DT>
<DD><B>4. </B>Select the Member Variables tab of the MFC ClassWizard property sheet.
With IDC_EDIT1 highlighted, click the Add Variable button. The Add Member Variable
dialog box appears.
<P>
<DT></DT>
<DD><B>5. </B>Name the new member variable <B>m_edit</B>, as shown in Figure 12.11,
and then click OK. ClassWizard adds the member variable, which will hold the value
of the property page's control, to the new CPage1 class.
</DL>
<P><A HREF="javascript:popUp('12uvc11.gif')"><B>FIG 12.11</B></A><B> </B><I>ClassWizard
makes it easy to connect controls on a dialog box to member variables of the class
representing the dialog box.</I></P>
<DL>
<DD><B>6. </B>Click OK on the MFC ClassWizard properties sheet to finalize the creation
of the CPage1 class.
<P>
<DT></DT>
<DD><B>7. </B>Follow steps 1 through 6 for the second property sheet. Name the class
<B>CPage2</B> and add a Boolean member variable called m_check for the IDC_CHECK1
control, as shown in Figure 12.12.
<P>
</DL>
<P><A HREF="javascript:popUp('12uvc12.gif')"><B>FIG. 12.12</B></A><B> </B><I>The
second property page needs a Boolean member variable called m_checkbox.</I></P>
<H3><I></I></H3>
<H3><A NAME="Heading7"></A>Creating a Property Sheet Class</H3>
<P>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:</P>
<DL>
<DT></DT>
<DD><B>1. </B>Bring up ClassWizard and click the Add Class button. A tiny menu appears
below the button; choose New. The New Class dialog box appears.
<P>
<DT></DT>
<DD><B>2. </B>In the Name box, type <B>CPropSheet</B>, select CPropertySheet in the
Base Class box, and then click OK.
<P>
<DT></DT>
<DD><B>3. </B>ClassWizard creates the CPropSheet class. Click the MFC ClassWizard
Properties sheet's OK button to finalize the class.
<P>
</DL>
<P>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:</P>
<DL>
<DT></DT>
<DD><B>1. </B>Click the ClassView tab to display the ClassView window. Expand the
Propsheet classes, as shown Figure 12.13.
<P>
<DT></DT>
<DD><B>2. </B>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.
<P>
<DT></DT>
<DD><B>3. </B>Add the following lines near the middle of the file, right before the
CPropSheet class declaration:
<P>
</DL>
<BLOCKQUOTE>
<PRE>#include "page1.h"
#include "page2.h"</PRE>
</BLOCKQUOTE>
<PRE></PRE>
<DL>
<DT></DT>
<DD>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.
<P>
</DL>
<P><A HREF="javascript:popUp('12uvc13.gif')"><B>FIG. 12.13</B></A><B> </B><I>The
ClassView window lists the classes that make up your project.</I></P>
<DL>
<DD><B>4. </B>Add the following lines to the CPropSheet class's //Attributes section,
right after the public keyword:
<P>
</DL>
<BLOCKQUOTE>
<PRE>CPage1 m_page1;
CPage2 m_page2;</PRE>
</BLOCKQUOTE>
<PRE></PRE>
<DL>
<DT></DT>
<DD>These lines declare the class's data members, which are the property pages that
will be displayed in the property sheet.
<P>
<DT></DT>
<DD><B>5. </B>Expand the CPropSheet class in the ClassView pane, and double-click
the first constructor, CPropSheet. Add these lines to it:
<P>
</DL>
<BLOCKQUOTE>
<PRE>AddPage(&m_page1);
AddPage(&m_page2);</PRE>
</BLOCKQUOTE>
<PRE></PRE>
<DL>
<DT></DT>
<DD>This will add the two property pages to the property sheet whenever the sheet
is constructed.
<P>
<DT></DT>
<DD><B>6. </B>The second constructor is right below the first; add the same lines
there.
<P>
<DT></DT>
<DD><B>7. </B>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();:
<P>
</DL>
<BLOCKQUOTE>
<PRE>protected:
CString m_edit;
BOOL m_check;</PRE>
</BLOCKQUOTE>
<PRE></PRE>
<DL>
<DT></DT>
<DD>These lines declare two data members of the view class to hold the selections
made in the property sheet by users.
<P>
<DT></DT>
<DD><B>8. </B>Add the following lines to the CPropsheetView constructor:
<P>
</DL>
<BLOCKQUOTE>
<PRE>m_edit = "Default";
m_check = FALSE;</PRE>
</BLOCKQUOTE>
<PRE></PRE>
<DL>
<DT></DT>
<DD>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.
<P>
<DT></DT>
<DD><B>9. </B>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.
<P>
</DL>
<H4>Listing 12.1  CPropsheetView::OnDraw()</H4>
<PRE>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");
</PRE>
<PRE>}
</PRE>
<DL>
<DT></DT>
<DD><B>10. </B>At the top of PropsheetView.cpp, after the #include of propsheet.h,
add another include statement:
<P>
</DL>
<PRE>#include "propsheet1.h"
</PRE>
<DL>
<DT></DT>
<DD><B>11. </B>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.
<P>
</DL>
<P><A HREF="javascript:popUp('12uvc14.gif')"><B>FIG. 12.14</B></A><B> </B><I>Use
ClassWizard to add the OnPropsheet() member function.</I></P>
<P>
<DL>
<DD>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.
<P>
<DT></DT>
<DD><B>12.</B> Click the Edit Code button to jump to the OnPropsheet() function,
and add the lines shown in Listing 12.2.
<P>
</DL>
<H4>Listing 12.2  CPropsheetView::OnPropsheet()</H4>
<PRE>void CPropsheetView::OnPropsheet()
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -