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

📄 ch09.htm

📁 VC使用大全。里面集合了VC使用的各种使用技巧。非常有用。
💻 HTM
📖 第 1 页 / 共 5 页
字号:

<P>The <font color="#008000">CString</font> to be passed to this function is going to be the string from the document class, which can be accessed as <font color="#008000">pDoc-&gt;GetString()</font>. The <font color="#008000">lpRect</font> is the client 
rectangle of the view, returned by <font color="#008000">GetClientRect()</font>. Finally, <font color="#008000">nFormat</font> is the way the string should display; for example, <font color="#008000">DT_CENTER</font> means that the text should be centered 
from left to right within the view. <font color="#008000">DT_VCENTER</font> means that the text should be centered up and down, but this works only for single lines of text that are identified with <font color="#008000">DT_SINGLELINE</font>. Multiple 
format flags can be combined with <font color="#008000">|</font>, so <font color="#008000">DT_CENTER|DT_VCENTER|DT_SINGLELINE</font> is the <font color="#008000">nFormat</font> that you want. The drawing code to be added to <font 
color="#008000">CShowStringView::OnDraw()</font> looks like this:</P>

<pre><font color="#008000">    CRect rect;</font></pre>

<pre><font color="#008000">    GetClientRect(&amp;rect);</font></pre>

<pre><font color="#008000">    pDC-&gt;DrawText(pDoc-&gt;GetString(), &amp;rect, DT_CENTER|DT_VCENTER|DT_SINGLELINE);</font></pre>

<P>This sets up a <font color="#008000">CRect</font> and passes its address to <font color="#008000">GetClientRect()</font>, which sets the <font color="#008000">CRect</font> to the client area of the view. <font color="#008000">DrawText()</font> draws 
the document's string in the rectangle, centered vertically and horizontally.</P>

<P>At this point, the application should display the string properly. Build and execute it, and you should see something like Figure 9.2. You have quite a lot of functionality: menus, toolbars, status bar, and so on, but nothing than any other Windows 
application doesn't have, yet. Starting with the next section, that changes.</P>

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

<P><I>ShowString starts simply, with the usual greeting.</I></P>

<H3><A ID="I8" NAME="I8"><B>Building the ShowString Menus</B></A></H3>

<P>AppWizard creates two menus for you, shown in the ResourceView window in Figure 9.3. <font color="#008000">IDR_MAINFRAME</font> is the menu shown when no file is open; <font color="#008000">IDR_SHOWSTTYPE</font> is the menu shown when a ShowString 
document is open. Notice that <font color="#008000">IDR_MAINFRAME</font> has no <U>V</U>iew or <U>W</U>indow menus and that the <U>F</U>ile menu is much shorter than the one on the <font color="#008000">IDR_SHOWSTTYPE</font> menu, with only <U>N</U>ew, 
<U>O</U>pen, P<U>r</U>int Setup, recent files, and E<U>x</U>it items.</P>

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

<P><I>AppWizard creates two menus for ShowString.</I></P>

<P>You are going to add a menu item to ShowString, so the first decision is where to add it. The user will be able to edit the string that displays and to set the format of the string. You could add a <U>V</U>alue item to the <U>E</U>dit menu that brings 
up a small dialog box for only the string and then create a <U>F</U>ormat menu with one item, <U>A</U>ppearance, that brings up the dialog box to set the appearance. But the choice you are going to see here is to combine everything into one dialog box and 
then put it on a new <U>T</U>ools menu, under the <U>O</U>ptions item. </P>

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

<P>You may have noticed already that more and more Windows applications are standardizing on <U>T</U>ools, <U>O</U>ptions as the place for miscellaneous settings.</P>

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

<P>Do you need to add the item to both menus? No. When there is no document open, there is nowhere to save the changes made with this dialog box. So only <font color="#008000">IDR_SHOWSTTYPE</font> needs to have a menu added. Bring up the menu by 
double-clicking it in the ResourceView window. At the far right of the menu, after <U>H</U>elp, is an empty menu. Click it and type <B>&amp;Tools</B>. The Properties dialog box appears; pin it to the background by clicking the pushpin. The Caption box 
contains <font color="#008000">&amp;Tools</font>. The menu at the end becomes the <U>T</U>ools menu, with an empty item underneath it; another empty menu then appears to the right of the <U>T</U>ools menu, as shown in Figure 9.4.</P>

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

<P><I>Adding the </I><I><U>T</U></I><I>ools menu is easy in the ResourceView window.</I></P>

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

<P>The &amp; in the Caption edit box precedes the letter that serves as the mnemonic key for selecting that menu with the keyboard (for example, Alt + T in the case of <U>T</U>ools). This letter appears underlined in the menu. There is no further work 
required on your part. You can opt to select a different mnemonic key by moving the &amp; so it precedes a different letter in the menu or menu item name (for example, T&amp;ools changes the key from 'T' to 'o').</P>

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

<P>Click the new <U>T</U>ools menu and drag it between the <U>V</U>iew and <U>W</U>indow menus, corresponding to the position of <U>T</U>ools in products like Developer Studio and Microsoft Word. Next, click the empty sub-item. The Properties dialog box 
changes to show the blank properties of this item; change the caption to <B>&amp;Options</B> and enter a sensible prompt, as shown in Figure 9.5.</P>

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

<P><I>The menu command </I><I><U>T</U></I><I>ools, </I><I><U>O</U></I><I>ptions will control everything that ShowString does.</I></P>

<P>All menu items have a resource ID, and this resource ID is the way the menu items are connected to your code. Developer Studio will choose a good one for you, but it doesn't appear in the Properties dialog box right away. Click some other menu item, 
and then click <U>O</U>ptions again; you see that the resource ID is <font color="#008000">ID_TOOLS_OPTIONS</font>. Alternatively, press Enter when you are finished and the highlight moves down to the empty menu item below <U>O</U>ptions. Press the 
up-arrow cursor key to return the highlight to the <U>O</U>ptions item.</P>

<P>If you'd like to provide an accelerator, such as Ctrl+C for <U>E</U>dit, <U>C</U>opy, this is a good time to do it. Click the + next to Accelerator in the ResourceView window and then double-click <font color="#008000">IDR_MAINFRAME</font>, the only 
Accelerator table in this application. At a glance, you can see what key combinations are already in use. Ctrl+O is already taken, but Ctrl+T is available. To connect Ctrl+T to <U>T</U>ools, <U>O</U>ptions, follow these steps:</P>

<ol>

<li><P> Click the empty line at the bottom of the Accelerator table. If you have closed the Properties dialog box, bring it back by choosing <U>V</U>iew, <U>P</U>roperties, and then pin it in place. (Alternatively, double-click the empty line to bring up 
the Properties dialog box.) </P>

<li><P> Click the drop-down list box labeled <U>I</U>D and choose <font color="#008000">ID_TOOLS_OPTIONS</font> from the list, which is in alphabetical order. (There are a lot of entries before <font color="#008000">ID_TOOLS_OPTIONS</font>; drag the 
elevator down almost to the bottom of the list or start typing the resource ID&#151;by the time you enter <font color="#008000">ID_TO</font> the highlight will be in the right place.)</P>

<li><P> Enter <B>T</B> in the <U>K</U>ey box; then make sure that the <U>C</U>trl check box is selected and that the A<U>l</U>t and <U>S</U>hift boxes are deselected. Alternatively, click the <U>N</U>ext Key Typed button and then type <B>Ctrl+T</B>, and 
the dialog box will be filled in properly.</P>

<li><P> Click another line in the Accelerator table to commit the changes.</P>

</ol>

<P>Figure 9.6 shows the Properties dialog box for this accelerator after clicking the newly entered line again.</P>

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

<P><I>Keyboard accelerators are connected to resource IDs.</I></P>

<P>What happens when the user chooses this new menu item, <U>T</U>ools, <U>O</U>ptions? A dialog box displays. So, tempting as it may be to start connecting this menu to code, it makes more sense to build the dialog box first.</P>

<H3><A ID="I9" NAME="I9"><B>Building the ShowString Dialog Boxes</B></A></H3>

<P><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; introduced dialog boxes. This section builds on that background. ShowString is actually going to have two custom dialog boxes: one brought up by <U>T</U>ools, 
<U>O</U>ptions, and also an About dialog box. An About dialog box has been provided by AppWizard, but it needs to be changed a little; you build the Options dialog box from scratch.</P>

<P><A ID="I10" NAME="I10"><B>ShowString's About Dialog Box</B></A></P>

<P>Figure 9.7 shows the About dialog box that AppWizard makes for you; it contains the name of the application and the current year. To view the About dialog box for ShowString, click the ResourceView tab in the project workspace window, expand the 
Dialogs list by clicking the + icon next to the word Dialogs, and then double-click <font color="#008000">IDD_ABOUTBOX</font> to bring up the About dialog box resource.</P>

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

<P><I>AppWizard makes an About dialog box for you.</I></P>

<P>You might want to add a company name to your About dialog box. Here's how to add &quot;Que Books,&quot; as an example. Click the line of text that reads <font color="#008000">Copyright &copy; 1997</font>, and it will be surrounded by a selection box. 
Bring up the Properties dialog box, if it is not up. Edit the caption to add <B><font color="#008000">Que </font></B><B><font color="#008000">Books</font></B> at the end; the changes are reflected immediately in the dialog box.</P>

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

<P>If the rulers you see in Figure 9.7 don't appear when you open <font color="#008000">IDD_ABOUTBOX</font> in Developer Studio, you can turn them on by choosing <U>L</U>ayout, <U>G</U>uide Settings and then selecting the <U>R</U>ulers and Guides radio 
button in the top half of the Guide Settings dialog box.</P>

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

<P>I decided to add a text string reminding users what book this application is from. Here's how to do that: </P>

<ol> 

<li><P> Size the dialog box a little taller by clicking the whole dialog box to select it, then clicking the sizing square in the middle of the bottom border, and dragging the bottom border down a little. (This visual editing is what gave Visual C++ its 
name when it first came out.)</P>

<li><P> In the floating toolbar called Controls, click the button labeled Aa to get a <I>static control,</I> which means a piece of text that the user cannot change, perfect for labels like this. Click within the dialog box under the other text to insert 
the static text there.</P>

<li><P> In the Properties dialog box, change the caption from <font color="#008000">Static</font> to <B><font color="#008000">Using Visual C++ 5</font></B>. The box automatically resizes to fit the text.</P>

<li><P> Hold down the Ctrl key and click the other two static text lines in the dialog box. Choose <U>L</U>ayout, <U>A</U>lign Controls, <U>L</U>eft, which aligns the edges of the three selected controls. The one you select last stays still, and the 
others move to align with it.</P>

<li><P> Choose <U>L</U>ayout, <U>S</U>pace Evenly, <U>D</U>own. These menu options can save you a great deal of dragging, squinting at the screen, and then dragging again.</P>

</ol>

<P>The About dialog box should look like Figure 9.8.</P>

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

<P><I>In a matter of minutes, you can customize your About dialog box.</I></P>

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

<P>All the <U>L</U>ayout menu items are on the Dialog toolbar.</P>

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

<P><A ID="I11" NAME="I11"><B>ShowString's Options Dialog Box</B></A></P>

<P>The Options dialog box is pretty simple to build. First, make a new dialog box by choosing <U>I</U>nsert, <U>R</U>esource, and then double-clicking Dialog. An empty dialog box called Dialog1 appears, with an OK and a Cancel button, as shown in Figure 
9.9.</P>

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

<P><I>A new dialog box always has OK and Cancel buttons.</I></P>

<P>Next, follow these steps to convert the empty dialog box into the Options dialog box:</P>

<ol>

<li><P> Change the ID to <B><font color="#008000">IDD_OPTIONS</font></B> and the caption to <B>Options</B>.</P>

<li><P> In the floating toolbar called Controls, click the button labeled <font color="#008000">ab|</font> to get an edit box in which the user can enter the new value for the string. Click inside the dialog box to place the control and then change the ID 
to <B><font color="#008000">IDC_OPTIONS_STRING</font></B>. (Control IDs should all start with &quot;IDC&quot; and then mention the name of their dialog box and an identifier that is unique to that dialog box.)</P>

<li><P> Drag the sizing squares to resize the edit box as wide as possible.</P>

<li><P> Add a static label above the edit box and change that caption to <B>String:</B>.</P>

</ol>

<P>You will revisit this dialog box later, when adding the appearance capabilities, but for now it's ready to be connected. It should look like Figure 9.10.</P>

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

<P><I>The Options dialog box is the place to change the string.</I></P>

<H3><A ID="I12" NAME="I12"><B>Making the Menu Work</B></A></H3>

<P>When the user chooses <U>T</U>ools, <U>O</U>ptions, the Options dialog box should display. You use ClassWizard to arrange for one of your functions to be called when the item is chosen, and then you write the function, which creates an object of your 
dialog box class and then displays it.</P>

<P><A ID="I13" NAME="I13"><B>The Dialog Box Class</B></A></P>

<P>ClassWizard makes the dialog box class for you. While the window displaying the <font color="#008000">IDD_OPTIONS</font> dialog box has focus, choose <U>V</U>iew, Class<U>W</U>izard. ClassWizard realizes there is not yet a class that corresponds to 
this dialog box and offers to create one, as shown in Figure 9.11.</P>

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

<P><I>Create a C++ class to go with the new dialog box.</I></P>

<P>Leave <U>C</U>reate a New Class selected and then click OK. The Create New Class dialog box, shown in Figure 9.12, appears.</P>

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

<P><I>The dialog box class inherits from </I><I>CDialog.</I></P>

<P>Fill in the dialog box as follows:</P>

<ol>

<li><P> Choose a sensible name for the class, one that starts with <I>C</I> and contains the word <I>Dialog</I>; this example uses <B><font color="#008000">COptionsDialog</font></B>.</P>

<li><P> The base class defaults to <font color="#008000">CDialog</font>, which is perfect for this case.</P>

<li><P> Click OK to create the class.</P>

⌨️ 快捷键说明

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