📄 ch12.htm
字号:
<li><P> Add the following lines to the <font color="#008000">CPropSheet</font> class's <font color="#008000">// Attributes</font> section, right after the <font color="#008000">public</font> keyword:</P>
<pre><font color="#008000"> CPage1 m_page1;</font></pre>
<pre><font color="#008000"> CPage2 m_page2;</font></pre>
<P> These lines declare the class's data members, which are the property pages that will be displayed in the property sheet.</P>
<li><P> Expand the CPropSheet class the ClassView pane, and double-click the first constructor. Add these lines to it:</P>
<pre><font color="#008000"> AddPage(&m_page1);</font></pre>
<pre><font color="#008000"> AddPage(&m_page2);</font></pre>
<P> This will add the two property pages to the property sheet whenever the sheet is constructed. The second constructor is right below the first: add the same lines there.</P>
<li><P> Double click CPropsheetView in ClassView to edit the header file, and add the following lines to the <font color="#008000">//Attributes</font> section, right after the line <font color="#008000">CPropsheetDoc* GetDocument();</font></pre>
<P>protected:</P>
<P> CString m_edit;</P>
<pre><font color="#008000"> BOOL m_check;</font></pre>
<P> These lines declare two data members of the view class to hold the selections made in the property sheet by the user.</P>
<li><P> Add the following lines to the CPropsheetView constructor:</P>
<pre><font color="#008000"> m_edit = "Default";</font></pre>
<pre><font color="#008000"> m_check = FALSE;</font></pre>
<P> 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 the user changes 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>
<li><P> Edit CPropsheetView::<font color="#008000">OnDraw()</font> 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>
<P><I>Listing 12.1 CPropsheetView::OnDraw()</I></P>
<pre><font color="#008000">void CPropsheetView::OnDraw(CDC* pDC)</font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000"> CPropsheetDoc* pDoc = GetDocument();</font></pre>
<pre><font color="#008000"> ASSERT_VALID(pDoc);</font></pre>
<pre><font color="#008000"> pDC->TextOut(20, 20, m_edit);</font></pre>
<pre><font color="#008000"> if (m_check)</font></pre>
<pre><font color="#008000"> pDC->TextOut(20, 50, "TRUE");</font></pre>
<pre><font color="#008000"> else</font></pre>
<pre><font color="#008000"> pDC->TextOut(20, 50, "FALSE");</font></pre>
<pre><font color="#008000">}</font></pre>
<li><P> Bring up ClassWizard, click the Message Maps tab, and make sure that CPropsheetView is selected in the Class Name box. Select IDD_PROPSHEET in the Object IDs box. This is the ID of the new item you added to the File menu. Click Add Function to add
a function that will handle the command message generated when a user choose this menu item. Name the function <font color="#008000">OnPropsheet()</font>, as shown in Figure 12.15.</P>
<P> The <font color="#008000">OnPropsheet()</font> function is now associated with the Property Sheet command that you previously added to the File menu. That is, when the user selects the Property Sheet command, MFC calls <font
color="#008000">OnPropsheet()</font>, where you can respond to the command.</P>
</ol>
<A HREF="Nfigs15.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch12/Nfigs15.gif"><b>Fig. 12.15</b></A>
<P><I>Use ClassWizard to add the </I><I>OnPropsheet()</I><I> member function.</I></P>
<ol start=10>
<li><P> Click the Edit Code button to jump to the <font color="#008000">OnPropsheet()</font> function, and then add the lines shown in Listing 12.2.</P>
<P><I>Listing 12.2 CPropsheetView::OnPropsheet()</I></P>
<pre><font color="#008000">void CPropsheetView::OnPropsheet() </font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000"> CPropSheet propSheet("Property Sheet", this, 0);</font></pre>
<pre><font color="#008000"> propSheet.m_page1.m_edit = m_edit;</font></pre>
<pre><font color="#008000"> propSheet.m_page2.m_checkbox = m_check;</font></pre>
<pre><font color="#008000"> int result = propSheet.DoModal();</font></pre>
<pre><font color="#008000"> if (result == IDOK)</font></pre>
<pre><font color="#008000"> {</font></pre>
<pre><font color="#008000"> m_edit = propSheet.m_page1.m_edit;</font></pre>
<pre><font color="#008000"> m_check = propSheet.m_page2.m_checkbox;</font></pre>
<pre><font color="#008000"> Invalidate();</font></pre>
<pre><font color="#008000"> }</font></pre>
<pre><font color="#008000"> </font></pre>
<pre><font color="#008000">}</font></pre>
<P> The code segment in Listing 12.2, which is discussed in more detail a little 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 using the familiar DoModal
function first discussed in <A HREF="index02.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index02.htm" target="text">Chapter 2</A>, "Dialog Boxes and Controls." If the user clicks OK, it updates the view member variables to reflect the changes made on each page, and forces a redraw with a call to
Invalidate.</P>
</ol>
<H3><B>Running the Property Sheet Demo Application</B></H3>
<P>You've now finished the complete application. Click the Build button on the Build mini-bar (or choose <U>B</U>uild, <U>B</U>uild) to compile and link the application. Run it by choosing <U>B</U>uild, E<U>x</U>ecute or clicking the Execute button on the
Build mini-bar. When you do, you see the window shown in Figure 12.16. As you can see, the window displays two values, which are the default values for the controls in the application's property sheet. You can change these values using the property sheet.
Choose <U>F</U>ile, <U>P</U>roperty Sheet—the property sheet appears on the screen (Figure 12.17). 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!</P>
<A HREF="Nfigs16.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch12/Nfigs16.gif"><b>Fig. 12.16</b></A>
<P><I>When it first starts, the Property Sheet Demo application displays </I><I>default values for the property sheet's controls.</I></P>
<A HREF="Nfigs17.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch12/Nfigs17.gif"><b>Fig. 12.17</b></A>
<P><I>The application's property sheet contains two pages.</I></P>
<H3><B>Adding Property Sheets to Your Applications</B></H3>
<P>In order to add a property sheet to one of your own applications, you will follow steps very similar to those you followed in the previous section to create the demo application. Those steps are:</P>
<ol>
<li><P> Create a dialog box resource for each page in the property sheet. These resources should have the <font color="#008000">Child</font> and <font color="#008000">Thin</font> styles and should have no system menu.</P>
<li><P> Associate each property page resource with an object of the <font color="#008000">CPropertyPage</font> class. You can do this easily with ClassWizard. Connect controls on the property page to members of the class you create.</P>
<li><P> Create a class for the property sheet, deriving the class from MFC's <font color="#008000">CPropertySheet</font> class. You can generate this class using ClassWizard.</P>
<li><P> 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.</P>
<li><P> In the property sheet's constructor, call <font color="#008000">AddPage()</font> for each page in the property sheet.</P>
<li><P> To display the property sheet, call the property sheet's constructor and then call the property sheet's <font color="#008000">DoModal()</font> member function, just as you would with a dialog box.</P>
</ol>
<P>After you have written your application and have defined the resources and classes that represent the property sheet (or sheets; you can have more than one), you need a way to enable the user 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, though, 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:</P>
<pre><font color="#008000">CPropSheet propSheet("Property Sheet", this, 0);</font></pre>
<P>Here, the program is creating an instance of the <font color="#008000">CPropSheet</font> class. This instance (or object) is called <font color="#008000">propSheet</font>. 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.</P>
<P>After you have the property sheet object created, you can initialize the data members that hold the values of the property page's controls, which Property Sheet Demo does like this:</P>
<pre><font color="#008000">propSheet.m_page1.m_edit = m_edit;</font></pre>
<pre><font color="#008000">propSheet.m_page2.m_checkbox = m_check;</font></pre>
<P>Now it's time to display the property sheet on the screen, which you do just as if it were a dialog box, by calling the property sheet's <font color="#008000">DoModal()</font> member function:</P>
<pre><font color="#008000">int result = propSheet.DoModal();</font></pre>
<p><font color="#008000">DoModal()</font> doesn't take any arguments, but it does return a value indicating which button the user clicked to exit the property sheet. In the case of a property sheet or dialog box, you'll usually want to process the
information entered into the controls only if the user clicked the OK button, which is indicated by a return value of <font color="#008000">IDOK</font>. If the user exits the property sheet by clicking the Cancel button, the changes are ignored and the
view or document member variables are not updated.</P>
<H3><B>Changing Property Sheets to Wizards</B></H3>
<P>Here's a piece of information that surprises most people: a wizard is just a special property sheet. Instead of tabs on each sheet that allow the user to fill in the information in any order, or to skip certain pages entirely, a wizard has Back, Next,
and Finish buttons to move the user through a process in a certain order. This forced sequence makes wizards terrific for guiding your application's users through the steps needed to complete a complex task. You've already seen how AppWizard in Visual C++
makes it easy to start a new project. You can create your own wizards that are suited to whatever application you want to build. In the following sections, you'll see how easy it is to convert a property sheet to a wizard.</P>
<P><B>Running the Wizard Demo Application</B></P>
<p><img src="cd_rom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/cd_rom.gif" hspace=10>
<P>In the CHAP13\WIZ folder of this book's CD-ROM, you'll find the Wizard Demo application. This application was built in much the same way as the Property Sheet Demo application that you created earlier in this chapter. This chapter will not present
step-by-step instructions to build Wizard Demo. You should be able to build it yourself if you wish, using the general steps presented earlier and the code snippets shown here.</P>
<P>When you run the Wizard Demo application, the main window appears, looking very much like the Property Sheet Demo main window. The <U>F</U>ile menu now includes a <U>W</U>izard item—choosing <U>F</U>ile <U>W</U>izard brings up the wizard shown in
Figure 12.18.</P>
<A HREF="Nfigs18.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch12/Nfigs18.gif"><b>Fig. 12.18</b></A>
<P><I>The Wizard Demo application displays a wizard rather than a property </I><I>sheet.</I></P>
<P>The wizard isn't too fancy, but it does demonstrate what you need to know in order to program more complex wizards. As you can see, this wizard has three pages. On the first page is an edit control and three buttons called <U>B</U>ack, Next, and
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -