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

📄 ch09.htm

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

<P>This arranges for <font color="#008000">OnToolsOptions()</font> to be called when the command <font color="#008000">ID_TOOLS_OPTIONS</font> is sent. ClassWizard also added a skeleton for <font color="#008000">OnToolsOptions()</font>:</P>

<pre><font color="#008000">void CShowStringDoc::OnToolsOptions() </font></pre>

<pre><font color="#008000">{</font></pre>

<pre><font color="#008000">    // TODO: Add your command handler code here</font></pre>

<pre><font color="#008000">    </font></pre>

<pre><font color="#008000">}</font></pre>

<H3><A ID="I15" NAME="I15"><B>Making the Dialog Box Work</B></A></H3>

<p><font color="#008000">OnToolsOptions()</font> should initialize and display the dialog box and then do something with the value that the user provided. (This process was 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;Dialogs and Controls.&quot;) You have already connected the edit box to a member variable, <font color="#008000">m_string</font>, of the dialog box class. You initialize this member variable before displaying the dialog box and use it afterwards.</P>


<p><font color="#008000">OnToolsOptions()</font>, shown in Listing 9.9, displays the dialog box. Add this code to the empty function ClassWizard generated for you when you arranged to catch the message.</P>

<P><I>Listing </I><I>9</I><I>.9&#151;</I>SHOWSTRINGDOC.CPP<I>&#151;</I>OnToolsOptions()</P>

<pre><font color="#008000">void CShowStringDoc::OnToolsOptions() </font></pre>

<pre><font color="#008000">{</font></pre>

<pre><font color="#008000">    COptionsDialog dlg;</font></pre>

<pre><font color="#008000">    dlg.m_string = string;</font></pre>

<pre><font color="#008000">    if (dlg.DoModal() == IDOK)</font></pre>

<pre><font color="#008000">    {</font></pre>

<pre><font color="#008000">        string = dlg.m_string;</font></pre>

<pre><font color="#008000">        SetModifiedFlag();</font></pre>

<pre><font color="#008000">        UpdateAllViews(NULL);</font></pre>

<pre><font color="#008000">    }</font></pre>

<pre><font color="#008000">}</font></pre>

<P>This code fills the member variable of the dialog box with the member variable of the document (ClassWizard added <font color="#008000">m_string</font> as a public member variable of <font color="#008000">COptionsDialog</font>, so the document can 
change it) and then brings up the dialog box by calling <font color="#008000">DoModal()</font>. If the user clicks OK, the member variable of the document changes, the modified flag is set (so that the user is prompted to save the document on exit), and 
the view is asked to redraw itself with a call to <font color="#008000">UpdateAllViews()</font>. In order for this to compile, of course, the compiler must know what a <font color="#008000">COptionsDialog</font> is, so add this line at the beginning of 
<font color="#008000">ShowStringDoc.cpp</font>:</P>

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

<P>At this point, you can build the application and run it. Choose <U>T</U>ools, <U>O</U>ptions, and change the string. Click OK and you see the new string in the view. Exit the application; you are asked whether to save the file. Save it, restart the 
application, and open the file again. The default &quot;Hello, world!&quot; document remains open, and the changed document is open with a different string. The application works, as you can see in Figure 9.14 (the windows are resized to let them both fit 
in the figure).</P>

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

<P><I>ShowString can change the string, save it to a file, and reload it.</I></P>

<H3><A ID="I16" NAME="I16"><B>Adding Appearance Options to the Options Dialog Box</B></A></H3>

<P>ShowString doesn't have much to do, just demonstrate menus and dialog boxes. But the only dialog box control that ShowString uses is an edit box. In this section, you add a set of radio buttons and check boxes to change the way the string is drawn in 
the view.</P>

<P><A ID="I17" NAME="I17"><B>Changing the Options Dialog Box</B></A></P>

<P>It is quite simple to incorporate a full-fledged Font dialog box into an application, but the example in this section is going to do something much simpler. A group of radio buttons will let the user choose among several colors. One check box will 
allow the user to specify that the text should be centered horizontally, and another that the text be centered vertically. Because these are check boxes, the text can be either, neither, or both.</P>

<P>Open the <font color="#008000">IDD_OPTIONS</font> dialog box by double-clicking it in the ResourceView window, and then add the radio buttons by following these steps:</P>

<ol> 

<li><P> Stretch the dialog box taller to make room for the new controls.</P>

<li><P> Click the radio button in the Controls floating toolbar, and then click the Options dialog box to drop the control.</P>

<li><P> Choose <U>V</U>iew, <U>P</U>roperties, and then pin the Properties dialog box in place.</P>

<li><P> Change the resource ID of the first radio button to <B><font color="#008000">IDC_OPTIONS_BLACK</font></B>, and change the caption to <B>&amp;Black</B>.</P>

<li><P> Select the <U>G</U>roup box to indicate that this is the first of a group of radio buttons.</P>

<li><P> Add another radio button with resource <B><font color="#008000">ID IDC_OPTIONS_RED</font></B> and <B>&amp;Red</B> as the caption. Do not select the <U>G</U>roup box since the <U>R</U>ed radio button does not start a new group but is part of the 
group that started with the <U>B</U>lack radio button.</P>

<li><P> Add a third radio button with resource ID <B><font color="#008000">IDC_OPTIONS_GREEN</font></B> and <B>&amp;Green</B> as the caption. Again, do not select <U>G</U>roup.</P>

<li><P> Drag the three radio buttons into a horizontal arrangement, and select all three.</P>

<li><P> Choose <U>L</U>ayout, <U>A</U>lign Controls, <U>B</U>ottom (to even them up).</P>

<li><P> Choose <U>L</U>ayout, <U>S</U>pace Evenly, <U>A</U>cross to space the controls across the dialog box.</P>

</ol>

<P>Next, add the check boxes by following these steps:</P>

<ol>

<li><P> Click the check box in the Controls floating toolbar and then click the Options dialog box, dropping a check box onto it.</P>

<li><P> Change the resource ID of this check box to <B><font color="#008000">IDC_OPTIONS_HORIZCENTER</font></B> and the caption to <B>Center &amp;Horizontally</B>.</P>

<li><P> Select the <U>G</U>roup box to indicate the start of a new group after the radio buttons.</P>

<li><P> Drop another check box onto the dialog box as in step 1 and give it the resource ID <B><font color="#008000">IDC_OPTIONS_VERTCENTER</font></B> and the caption <B>Center &amp;Vertically</B>.</P>

<li><P> Arrange the check boxes under the radio buttons.</P>

<li><P> Click the Group box on the Controls floating toolbar, and then click and drag a group box around the radio buttons. Change the caption to <B>Text Color</B>.</P>

<li><P> Move the OK and Cancel buttons down to the bottom of the dialog box.</P>

<li><P> Select each horizontal group of controls and use <U>L</U>ayout, <U>C</U>enter in Dialog, <U>H</U>orizontal to make things neater.</P>

<li><P> Choose <U>E</U>dit, Select A<U>l</U>l, and then drag all the controls up toward the top of the dialog box. Shrink the dialog box to fit around the new controls. It should now resemble Figure 9.15.</P>

</ol>

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

<P><I>The options dialog box for ShowString has been expanded.</I></P>

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

<P>If you don't recognize the icons on the Controls toolbar, use the ToolTips<I>.</I> If you hold the cursor over any of the toolbar buttons, a tip pops up after a few seconds, telling you what control the button represents.</P>

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

<P>Finally, set the tab order by choosing <U>L</U>ayout, <U>T</U>ab Order, and then clicking the controls, in this order:</P>

<ol> 

<li><P> <font color="#008000">IDC_OPTIONS_STRING</font></pre>

<li><P> <font color="#008000">IDC_OPTIONS_BLACK</font></pre>

<li><P> <font color="#008000">IDC_OPTIONS_RED</font></pre>

<li><P> <font color="#008000">IDC_OPTIONS_GREEN</font></pre>

<li><P> <font color="#008000">IDC_OPTIONS_HORIZCENTER</font></pre>

<li><P> <font color="#008000">IDC_OPTIONS_VERTCENTER</font></pre>

<li><P> <font color="#008000">IDOK</font></pre>

<li><P> <font color="#008000">IDCANCEL</font></pre>

</ol>

<P>Then click away from the dialog box to leave the two static text controls as positions 9 and 10.</P>

<P><A ID="I18" NAME="I18"><B>Adding Member Variables to the Dialog Box Class</B></A></P>

<P>Having added controls to the dialog box, you need to add corresponding member variables to the <font color="#008000">COptionsDialog </font>class. Bring up ClassWizard, select the Member Variable tab, and add member variables for each control. Figure 
9.16 shows the summary of the member variables created. The check boxes are connected to <font color="#008000">BOOL</font> variables; these member variables are <font color="#008000">TRUE</font> if the box is selected and <font color="#008000">FALSE</font> 
if it is not. The radio buttons are handled differently. Only the first&#151;the one with the Group box selected in its Properties dialog box&#151;is connected to a member variable. That integer is a zero-based index that indicates which button is 
selected. In other words, when the <U>B</U>lack button is selected, <font color="#008000">m_color</font> is 0; when <U>R</U>ed is selected, <font color="#008000">m color</font> is 1; and when <U>G</U>reen is selected, <font color="#008000">m</font>_<font 
color="#008000">color</font> is 2.</P>

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

<P><I>Member variables in the dialog box class are connected to individual controls or the group of radio buttons.</I></P>

<P><A ID="I19" NAME="I19"><B>Adding Member Variables to the Document</B></A></P>

<P>The variables to be added to the document are the same ones that were added to the dialog box. You add them to the <font color="#008000">CShowStringDoc</font> class definition in the header file, to <font color="#008000">OnNewDocument()</font>, and to 
<font color="#008000">Serialize()</font>. Add the lines in Listing 9.10 at the top of the <font color="#008000">CShowStringDoc</font> definition in <font color="#008000">ShowStringDoc.h</font>, replacing the previous definition of <font 
color="#008000">string</font> and <font color="#008000">GetString()</font>.</P>

<P><I>Listing </I><I>9</I><I>.10&#151;</I>SHOWSTRINGDOC.H<I>&#151;</I>CShowStringDoc<I> Member Variables</I></P>

<pre><font color="#008000">private:</font></pre>

<pre><font color="#008000">    CString string;</font></pre>

<pre><font color="#008000">    int    color;</font></pre>

<pre><font color="#008000">    BOOL horizcenter;</font></pre>

<pre><font color="#008000">    BOOL vertcenter;</font></pre>

<pre><font color="#008000">public:</font></pre>

<pre><font color="#008000">    CString GetString() {return string;}</font></pre>

<pre><font color="#008000">    int    GetColor() {return color;}</font></pre>

<pre><font color="#008000">    BOOL GetHorizcenter() {return horizcenter;}</font></pre>

<pre><font color="#008000">    BOOL GetVertcenter() {return vertcenter;}</font></pre>

<P>As with <font color="#008000">string</font>, these are private variables with public <font color="#008000">get</font> functions but no set functions. All these options should be serialized; the new <font color="#008000">Serialize()</font> is shown in 
Listing 9.11. Change your copy by double-clicking the function name in ClassView and adding the new code.</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><I>Listing </I><I>9</I><I>.11&#151;</I>SHOWSTRINGDOC.CPP<I>&#151;</I>Serialize()</P>

<pre><font color="#008000">void CShowStringDoc::Serialize(CArchive&amp; ar)</font></pre>

<pre><font color="#008000">{</font></pre>

<pre><font color="#008000">    if (ar.IsStoring())</font></pre>

<pre><font color="#008000">    {</font></pre>

⌨️ 快捷键说明

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