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

📄 ch09.htm

📁 VC使用大全。里面集合了VC使用的各种使用技巧。非常有用。
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<pre><font color="#008000">        ar &lt;&lt; string;</font></pre>

<pre><font color="#008000">        ar &lt;&lt; color;</font></pre>

<pre><font color="#008000">        ar &lt;&lt; horizcenter;</font></pre>

<pre><font color="#008000">        ar &lt;&lt; vertcenter;</font></pre>

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

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

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

<pre><font color="#008000">        ar &gt;&gt; string;</font></pre>

<pre><font color="#008000">        ar &gt;&gt; color;</font></pre>

<pre><font color="#008000">        ar &gt;&gt; horizcenter;</font></pre>

<pre><font color="#008000">        ar &gt;&gt; vertcenter;</font></pre>

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

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

<P>Finally, you need to initialize these variables in <font color="#008000">OnNewDocument()</font>. What are good defaults for these new member variables? Black text, centered in both directions, was the old behavior, and it makes sense to use it as the 
default. The new <font color="#008000">OnNewDocument()</font> is shown in Listing 9.12.</P>

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

<pre><font color="#008000">BOOL CShowStringDoc::OnNewDocument()</font></pre>

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

<pre><font color="#008000">    if (!CDocument::OnNewDocument())</font></pre>

<pre><font color="#008000">        return FALSE;</font></pre>

<pre><font color="#008000">    string = &quot;Hello, world!&quot;;</font></pre>

<pre><font color="#008000">    color = 0;     //black</font></pre>

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

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

<pre><font color="#008000">    return TRUE;</font></pre>

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

<P>Of course, at the moment, users cannot change these member variables  from the defaults. To allow the user to change the variables, you have to change the function that handles the dialog box.</P>

<P><A ID="I20" NAME="I20"><B><I>Changing </I></B><B>OnToolsOptions()</B></A></P>

<P>The <font color="#008000">OnToolsOptions()</font> function sets the values of the dialog box member variables from the document member variables and then displays the dialog box. If the user clicks OK, the document member variables are set from the 
dialog box member variables and the view is redrawn. Having just added three member variables to the dialog box and the document, you have three lines to add before the dialog box displays and then three more to add in the block that's called after OK is 
clicked. The new <font color="#008000">OnToolsOptions()</font> is shown in Listing 9.13.</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>.13&#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">    dlg.m_color = color;</font></pre>

<pre><font color="#008000">    dlg.m_horizcenter = horizcenter;</font></pre>

<pre><font color="#008000">    dlg.m_vertcenter = vertcenter;</font></pre>

<pre><font color="#008000">    </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">        color = dlg.m_color;</font></pre>

<pre><font color="#008000">        horizcenter = dlg.m_horizcenter;</font></pre>

<pre><font color="#008000">        vertcenter = dlg.m_vertcenter;</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>So what happens when the user brings up the dialog box and changes the value of a control, say, by deselecting Center Horizontally? The framework&#151;through Dialog Data Exchange (DDX), as set up by ClassWizard&#151;changes the value of <font 
color="#008000">COptionsDialog::m_horizcenter</font> to <font color="#008000">FALSE</font>. This code in <font color="#008000">OnToolsOptions()</font> changes the value of <font color="#008000">CShowStringDoc::horizcenter</font> to <font 
color="#008000">FALSE</font>. When the user saves the document, <font color="#008000">Serialize()</font> saves <font color="#008000">horizcenter</font>. This is all good, but none of this code actually changes the way the view is drawn. That involves <font 
color="#008000">OnDraw()</font>.</P>

<P><A ID="I21" NAME="I21"><B><I>Changing </I></B><B>OnDraw()</B></A></P>

<P>The single call to <font color="#008000">DrawText()</font> in <font color="#008000">OnDraw()</font> gets a little more complex now. The document member variables are used to set the appearance of the view. Edit <font color="#008000">OnDraw()</font> by 
expanding <font color="#008000">CShowStringView</font> in the ClassView, and double-clicking <font color="#008000">OnDraw()</font>.</P>

<P>The color is set with <font color="#008000">CDC::SetTextColor()</font> before the call to <font color="#008000">DrawText()</font>. You should always save the old text color and restore it when you are finished. The parameter to <font 
color="#008000">SetTextColor()</font> is a <font color="#008000">COLORREF</font>, and you can directly specify combinations of red, green, and blue as hex numbers in the form <font color="#008000">0x00bbggrr</font>, so that, for example, <font 
color="#008000">0x000000FF</font> is bright red. Most people prefer to use the <font color="#008000">RGB</font> macro, which takes hex numbers from <font color="#008000">0x0</font> to <font color="#008000">0xFF</font>, specifying the amount of each color; 
bright red is <font color="#008000">RGB(FF,0,0)</font>, for instance. Add the lines shown in Listing 9.14 lines before the call to <font color="#008000">DrawText()</font> to set up everything.</P>

<P><I>Listing </I><I>9</I><I>.14&#151;</I>SHOWSTRINGDOC.CPP<I>&#151;</I>OnDraw()<I> Additions Before </I>DrawText()<I> Call</I></P>

<pre><font color="#008000">    COLORREF oldcolor;</font></pre>

<pre><font color="#008000">    switch (pDoc-&gt;GetColor())</font></pre>

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

<pre><font color="#008000">    case 0:</font></pre>

<pre><font color="#008000">        oldcolor = pDC-&gt;SetTextColor(RGB(0,0,0)); //black</font></pre>

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

<pre><font color="#008000">    case 1:</font></pre>

<pre><font color="#008000">        oldcolor = pDC-&gt;SetTextColor(RGB(0xFF,0,0)); //red</font></pre>

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

<pre><font color="#008000">    case 2:</font></pre>

<pre><font color="#008000">        oldcolor = pDC-&gt;SetTextColor(RGB(0,0xFF,0)); //green</font></pre>

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

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

<P>Add this line after the call to <font color="#008000">DrawText()</font>:</P>

<pre><font color="#008000">    pDC-&gt;SetTextColor(oldcolor);</font></pre>

<P>There are two approaches to setting the centering flags. The brute-force way is to list the four possibilities (neither, horizontal, vertical, and both) and have a different <font color="#008000">DrawText()</font> statement for each. If you were to add 
other settings, this would quickly become unworkable. It's better to set up an integer to hold the <font color="#008000">DrawText()</font> flags and &quot;or in&quot; each flag, if appropriate. Add the lines shown in Listing 9.15 before the call to <font 
color="#008000">DrawText()</font>.</P>

<P><I>Listing </I><I>9</I><I>.15&#151;</I>SHOWSTRINGDOC.CPP<I>&#151;</I>OnDraw()<I> Additions After </I>DrawText()<I> </I><I>Call</I></P>

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

<pre><font color="#008000">    if (pDoc-&gt;GetHorizcenter())</font></pre>

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

<pre><font color="#008000">        DTflags |= DT_CENTER;</font></pre>

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

<pre><font color="#008000">    if (pDoc-&gt;GetVertcenter())</font></pre>

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

<pre><font color="#008000">        DTflags |= (DT_VCENTER|DT_SINGLELINE);</font></pre>

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

<P>The call to <font color="#008000">DrawText()</font> now uses the <font color="#008000">DTflags</font> variable:</P>

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

<P>Now the settings from the dialog box have made their way to the dialog box class, to the document, and finally to the view, to actually affect the appearance of the text string. Build and execute ShowString and then try it out. Any surprises? Be sure 
to change the text, experiment with various combinations of the centering options, and try all three colors.</P>

<H3><A ID="I22" NAME="I22"><B>From Here</B><B>...</B></A></H3>

<P>This is not the last you will see of ShowString; it reappears in <A HREF="index12.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index12.htm" target="text">Chapter 12</A>, &quot;Help,&quot; and throughout Part V, &quot;ActiveX Applications and ActiveX Controls&quot; (Chapters <A HREF="index13.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index13.htm" 
target="text">13</A>-<A HREF="index17.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index17.htm" target="text">17</A>.) But there's a lot of other material to cover between here and there. The rest of this part of the book presents sample applications and how-to instructions for everyday tasks all developers 
face:</P>

<ul>

<li> Printing is covered in <A HREF="index08.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index08.htm" target="text">Chapter 8</A>, &quot;Printing and Print Preview.&quot;</P>

<li> Implementing property sheets is covered in <A HREF="index13.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index13.htm" target="text">Chapter 13</A>, &quot;Property Pages and Sheets and Wizards.&quot;</P>

<li> Adding buttons to toolbars and implementing status bars are covered in <A HREF="index11.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index11.htm" target="text">Chapter 11</A>, &quot;Status Bars and Toolbars.&quot;</P>

</ul>

<p><hr></p>

<center>

<p><font size=-2>

&copy; 1997, QUE Corporation, an imprint of Macmillan Publishing USA, a

Simon and Schuster Company.</font></p>

</center>

</BODY></HTML>

⌨️ 快捷键说明

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