⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch12.htm

📁 VC使用所有细节的逻列
💻 HTM
📖 第 1 页 / 共 4 页
字号:

<li><P> Double-click the <font color="#008000">About Propsheet...</font> item in the <U>H</U>elp menu to bring up its properties dialog box. Change the caption to <font color="#008000">&amp;About </font><font color="#008000">Property Sheet Demo</font>. 
Pin the properties dialog box in place by clicking on the pushpin in the upper left corner.</P>

<li><P> On the application's <U>F</U>ile menu, delete all menu items except Exit.</P>

<li><P> Select the blank menu item at the end of the File menu, and change the caption to <font color="#008000">&amp;</font><font color="#008000"><U>P</U></font><font color="#008000">roperty Sheet... </font>and the command ID to <font 
color="#008000">ID_PROPSHEET</font>, as shown in Figure 12.5. Then use your mouse to drag the new command above the E<U>x</U>it command, so that it's the first command in the <U>F</U>ile menu.</P>

</ol>

<A HREF="Nfigs05.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch12/Nfigs05.gif"><b>Fig. 12.5</b></A>

<P><I>Add a Property Sheet command to the File menu.</I></P>

<ol start=7>

<li><P> Click the + next to Accelerator in the ResourceView window and highlight the <font color="#008000">IDR_MAINFRAME</font> accelerator ID. Press your Delete key to delete all accelerators from the application.</P>

<li><P> Click the + next to Dialog Resource in the ResourceView window. Double-click the <font color="#008000">IDD_ABOUTBOX</font> dialog box ID to bring up the dialog box editor.</P>

<li><P> Modify the dialog box by clicking the title so that the properties box refers to the whole dialog box. Change the caption to &quot;About Property Sheet Demo.&quot;</P>

<li><P> Click the first static text string and change the caption to &quot;Property Sheet Demo, Version 1.0&quot;. Click the second and add &quot;Que books&quot; to the end of the copyright string.</P>

<li><P> Add a third static string with the text &quot;Special Edition Using Visual C++ 5&quot; so that your About box resembles the one in Figure 12.6. Close the dialog box editor.</P>

</ol>

<A HREF="Nfigs06.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch12/Nfigs06.gif"><b>Fig. 12.6</b></A>

<P><I>The About box should look like this.</I></P>

<ol start=12>

<li><P> Click the + next to String Table in the ResourceView window. Double-click the String Table ID to bring up the string table editor.</P>

<li><P> Double-click the <font color="#008000">IDR_MAINFRAME</font> string and then change the first segment of the string to &quot;Property Sheet Demo&quot;, as shown in Figure 12.7. The meanings of these strings are discussed in <A HREF="index16.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index16.htm" 
target="text">Chapter 16</A>, &quot;Building an Automation Server,&#148; in the &quot;Shortcomings of this Server&quot; section. The one you just changed is the Window Title, used in the title bar of the application.</P>

</ol>

<A HREF="Nfigs07.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch12/Nfigs07.gif"><b>Fig. 12.7</b></A>

<P><I>The first segment of the </I><I>IDR_MAINFRAME</I><I> string appears in your main </I><I>window's title bar.</I></P>

<P>Now that you have the application's basic resources the way you want them, it's time to add the resources that define the application's property sheet. This means creating dialog box resources for each page in the property sheet. Follow these 
steps:</P>

<ol>

<li><P> Click the New Dialog button on the Resource toolbar, or press Ctrl+1 on your keyboard, to create a new dialog box resource. The new dialog box, called <font color="#008000">IDD_DIALOG1</font>, appears in the dialog box editor.</P>

<P>  This dialog box, when it is set up properly, will represent the first page of the property sheet.</P>

<li><P> Delete the OK and Cancel buttons by selecting each with your mouse and then pressing your keyboard's Delete key.</P>

<li><P> If the Properties box is not still up, bring it up by choosing <U>V</U>iew, <U>P</U>roperties. Change the ID of the dialog box to <font color="#008000">IDD_PAGE1DLG</font> and the caption to <font color="#008000">Page 1</font>, as shown in Figure 
12.8.</P>

</ol>

<A HREF="Nfigs08.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch12/Nfigs08.gif"><b>Fig. 12.8</b></A>

<P><I>Change the caption and resource ID of the new dialog box.</I></P>

<ol start=4>

<li><P> Click the Styles tab of the dialog box's property sheet. In the St<U>y</U>le drop-down box, select Child, and in the <U>B</U>order drop-down box, select Thin. Turn off the System Menu check box. Your properties dialog box should resemble Figure 
12.9.</P>

<P>  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>

</ol>

<A HREF="Nfigs09.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch12/Nfigs09.gif"><b>Fig. 12.9</b></A>

<P><I>A property page uses styles different from those used in regular dialog </I><I>boxes.</I></P>

<ol start=5>

<li><P> Add an edit box to the property page, as shown in Figure 12.10. In most applications you would change the resource ID from IDC_EDIT1, but for this demonstration application, leave it unchanged.</P>

</ol>

<A HREF="Nfigs10.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch12/Nfigs10.gif"><b>Fig. 12.10</b></A>

<P><I>A property page can hold whatever controls you like.</I></P>

<ol start=6>

<li><P> Create a second property page by following steps 1 through 5 again. For this property page, use the ID <font color="#008000">IDD_PAGE2DLG</font>, a caption of <font color="#008000">Page 2</font>, and add a check box rather than an edit control, as 
shown in Figure 12.11.</P>

</ol>

<A HREF="Nfigs11.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch12/Nfigs11.gif"><b>Fig. 12.11</b></A>

<P><I>The second property page should look like this.</I></P>

<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>

<ol>

<li><P> 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 <U>V</U>iew, Class<U>W</U>izard from the menu bar. The MFC ClassWizard property sheet appears, displaying the Adding A 
Class dialog box 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>, &quot;Dialog Boxes and Controls.&quot;</P>

<li><P> Select the <U>C</U>reate New Class option and then click OK. The New Class dialog box appears.</P>

<li><P> In the <U>N</U>ame box, type <font color="#008000">CPage1</font>, and in the <U>B</U>ase Class box, select <font color="#008000">CPropertyPage</font>. (Don't accidentally select CPropertySheet.) Then click the OK button to create the class.</P>

<P>  You've now associated the property page with an object of the <font color="#008000">CPropertyPage</font> class, which means that you can use the object to manipulate the property page as needed. The <font color="#008000">CPropertyPage</font> class 
will be especially important when you learn about wizards.</P>

<li><P> Select the Member Variables tab of the MFC ClassWizard property sheet. With <font color="#008000">IDC_EDIT1</font> highlighted, click the <U>A</U>dd Variable button. The Add Member Variable dialog box appears.</P>

<li><P> Name the new member variable <font color="#008000">m_edit</font>, as shown in Figure 12.12, and then click the OK button. ClassWizard adds the member variable, which will hold the value of the property page's control, to the new <font 
color="#008000">CPage1</font> class.</P>

</ol>

<A HREF="Nfigs12.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch12/Nfigs12.gif"><b>Fig. 12.12</b></A>

<P><I>ClassWizard makes it easy to connect controls on a dialog box to member </I><I>variables of the class representing the dialog box.</I></P>

<ol start=6>

<li><P> Click OK on the MFC ClassWizard Properties sheet to finalize the creation of the <font color="#008000">CPage1</font> class.</P>

<li><P> Follow steps 1 through 7 for the second property sheet. Name the class <font color="#008000">CPage2</font> and add a Boolean member variable called <font color="#008000">m_check</font> for the <font color="#008000">IDC_CHECK1</font> control, as 
shown in Figure 12.13.</P>

</ol>

<A HREF="Nfigs13.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch12/Nfigs13.gif"><b>Fig. 12.13</b></A>

<P><I>The second property page needs a Boolean member variable called </I><I>m_checkbox</I><I>.</I></P>

<P>At this point you have done all the resource editing, and there is no need to have so many windows open. Choose <U>W</U>indow, Close Al<U>l </U>from the menu bar and close the properties box. You will now create a property sheet class that displays the 
property pages already created. Follow these steps:</P>

<ol>

<li><P> Bring up ClassWizard and click the Add Class button. A tiny menu appears below the button&#151;choose New. The New Class dialog box appears.</P>

<li><P> In the <U>N</U>ame box, type <font color="#008000">CPropSheet</font>, select <font color="#008000">CPropertySheet</font> in the <U>B</U>ase Class box, and then click OK.</P>

<li><P> ClassWizard creates the <font color="#008000">CPropSheet</font> class. Click the MFC ClassWizard Properties sheet's OK button to finalize the class.</P>

</ol>

<P>At this point, you have three new classes&#151;<font color="#008000">CPage1</font>, <font color="#008000">CPage2</font>, and <font color="#008000">CPropSheet</font>&#151;in your program. The first two classes are derived from MFC's <font 
color="#008000">CPropertyPage</font> class, and the third is derived from <font color="#008000">CPropertySheet</font>. 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>

<ol>

<li><P> Click the ClassView tab to display the ClassView window. Expand the Propsheet classes, as shown Figure 12.14.</P>

</ol>

<A HREF="Nfigs14.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch12/Nfigs14.gif"><b>Fig. 12.14</b></A>

<P><I>The ClassView window lists the classes that make up your project.</I></P>

<ol start=2>

<li><P> 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 will find two classes in this file: CPropSheetApp, 
generated by AppWizard, and CPropSheet, generated by ClassWizard when you created the new class.</P>

</ol>

<blockquote><p><img src="note.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/note.gif">

<P>If the new class for the property sheet was in a file of its own, you would have to add #include statements to any file that used the CPropSheet class. Keep this in mind when you create your own property sheets.</P>

<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>

<ol start=3>

<li><P> Add the following lines near the middle of the file, right before the CPropSheet class declaration:</P>

<pre><font color="#008000">#include &quot;page1.h&quot;</font></pre>

<pre><font color="#008000">#include &quot;page2.h&quot;</font></pre>

<P>  These lines give the <font color="#008000">CPropSheet</font> class access to the <font color="#008000">CPage1</font> and <font color="#008000">CPage2</font> classes, so that the property sheet can declare member variables of these property page 
classes.</P>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -