right17-2.htm

来自「Visual C++面向对象程序设计教程(配套习题资源)」· HTM 代码 · 共 319 行 · 第 1/3 页

HTM
319
字号
      </span></p>
    </td>
    <td width="61" valign="top" style="mso-border-top-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; border-left-style: none; border-left-width: medium; border-right: .5pt solid windowtext; border-top-style: none; border-top-width: medium; border-bottom: .5pt solid windowtext; padding-left: 5.4pt; padding-right: 5.4pt; padding-top: 0cm; padding-bottom: 0cm">
      <p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt">Public<o:p>
      </o:p>
      </span></p>
    </td>
    <td width="393" valign="top" style="mso-border-top-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; border-left-style: none; border-left-width: medium; border-right-style: none; border-right-width: medium; border-top-style: none; border-top-width: medium; border-bottom: .5pt solid windowtext; padding-left: 5.4pt; padding-right: 5.4pt; padding-top: 0cm; padding-bottom: 0cm">
      <p class="MsoNormal"><span style="font-size:10.0pt;font-family:宋体;mso-ascii-font-family:
  &quot;Times New Roman&quot;;mso-hansi-font-family:&quot;Times New Roman&quot;">用来存储用户输入的字符数</span><span lang="EN-US" style="font-size:10.0pt"><o:p>
      </o:p>
      </span></p>
    </td>
  </tr>
</table>
<p ALIGN="justify" style="line-height: 150%; margin-top: 0; margin-bottom: 0"><font lang="ZH-CN" size="2">(2)在视图类中添加代码处理数据显示&nbsp;</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><span style="mso-bidi-font-size: 10.0pt; font-family: 宋体; mso-bidi-font-family: Times New Roman; mso-font-kerning: 10.5pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA"><font size="2">①</font></span> 
<font size="2"><font LANG="ZH-CN">在类</font>CTextView<font LANG="ZH-CN">的构造函数中添加下面的代码,对变量</font>numberchar<font LANG="ZH-CN">进行初始化。</font></font></p> 
<blockquote>
  <blockquote>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">CTextView::CTextView(  
    )</font></p>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">{</font></p>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
    numberchar=0;</font></p>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">}</font></p>
  </blockquote>
</blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><span style="mso-bidi-font-size: 10.0pt; font-family: 宋体; mso-bidi-font-family: Times New Roman; mso-font-kerning: 10.5pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA"><font size="2">②</font></span><font size="2"><span style="mso-bidi-font-size: 10.0pt; font-family: 宋体; mso-bidi-font-family: Times New Roman; mso-font-kerning: 10.5pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA"> 
</span></font><font size="2">构造消息映射:</font></p>
<blockquote>
  <blockquote>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">BEGIN_MESSAGE_MAP(CTextView,  
    CView)</font></p>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
    ...</font></p>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
    ON_WM_CHAR( )</font></p> 
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font color="#008000" size="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
    ...</font></p>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">END_MESSAGE_MAP( 
    )</font></p>
  </blockquote>
</blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">MFC<font LANG="ZH-CN">的消息映射宏</font>BEGIN_MESSAGE_MAP<font LANG="ZH-CN">把</font>Windows<font LANG="ZH-CN">系统发来的</font>WM_CHAR<font LANG="ZH-CN">消息连接到</font>CTextView<font LANG="ZH-CN">类的成员函数</font>OnChar()<font LANG="ZH-CN">上,即把这个成员函数作为处理</font>WM_CHAR<font LANG="ZH-CN">消息的过程。</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><span style="mso-bidi-font-size: 10.0pt; font-family: 宋体; mso-bidi-font-family: Times New Roman; mso-font-kerning: 10.5pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA"><font size="2">③</font></span><font size="2"><span style="mso-bidi-font-size: 10.0pt; font-family: 宋体; mso-bidi-font-family: Times New Roman; mso-font-kerning: 10.5pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA"> 
</span><font LANG="ZH-CN">在</font>OnChar()<font LANG="ZH-CN">函数中添加如下的代码:</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font color="#008000"><font size="2">CTextDoc  
*pdoc;&nbsp;&nbsp;&nbsp;&nbsp; //</font></font><font size="2"><font color="#008000"><font LANG="ZH-CN">定义一个指向文档类对象的指针</font>pdoc</font><font LANG="ZH-CN"></font></font></p> 
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">pdoc=GetDocument(  
);//<font LANG="ZH-CN">获取指向当前视窗类所对应的文档类对象的指针,并把这个指针赋给定义的文档类型指针</font>pdoc</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">numberchar++;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
//<font LANG="ZH-CN">将记录输入字符数的变量</font>numberchar<font LANG="ZH-CN">增</font>1</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">pdoc-&gt;text.Insert(numberchar,nChar);  
//<font LANG="ZH-CN">根据从消息</font>WM_CHAR<font LANG="ZH-CN">传来的参数</font>nChar<font LANG="ZH-CN">,也就是键盘输入字符的</font>ASCII<font LANG="ZH-CN">码,</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font color="#008000"><font lang="ZH-CN" size="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
//</font><font size="2"><font LANG="ZH-CN">把输入的字符添加到文档中的字符串对象</font>text<font LANG="ZH-CN">中</font></font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">CClientDC  
clientdc(this);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
//<font LANG="ZH-CN">定义并生成了一个当前视窗的客户区的设备描述表对象</font>clientdc<font LANG="ZH-CN">,</font>this</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font color="#008000"><font size="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
//</font><font LANG="ZH-CN" size="2">指代成员函数所在类的对象的指针</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">clientdc.TextOut(0,0,pdoc-&gt;text);&nbsp;&nbsp;&nbsp;  
//<font LANG="ZH-CN">调用设备描述表</font>clientdc<font LANG="ZH-CN">的方法</font>textout<font LANG="ZH-CN">,在视窗的客户区中输出文档中</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font color="#008000"><font lang="ZH-CN" size="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
//</font><font size="2"><font LANG="ZH-CN">的字符串</font>text</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font color="#008000"><font size="2">pdoc_&gt;UpdateAllViews(this,0L,0);&nbsp;&nbsp;&nbsp;  
//</font><font LANG="ZH-CN" size="2">通知本视窗所在的文档的所有其他视窗,文档中的数据已经更新,这些视窗</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font lang="ZH-CN" size="2" color="#008000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
//应该重新从文档中取回数据用来显示,这样就维持了同一文档的所有视窗的</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font lang="ZH-CN" size="2" color="#008000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
//数据一致性</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font lang="ZH-CN" size="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><span style="mso-bidi-font-size: 10.0pt; font-family: 宋体; mso-bidi-font-family: Times New Roman; mso-font-kerning: 10.5pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA"><font size="2">④  
</font></span><font size="2"><font LANG="ZH-CN">在</font>OnDraw()函数中添加如下代码:</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font color="#008000" size="2">void  
CTextView::OnDraw(CDC* pDC)</font></p> 
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">{</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">&nbsp;&nbsp;&nbsp;&nbsp;  
CTextDoc* pDoc = GetDocument( );</font></p> 
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">&nbsp;&nbsp;&nbsp;&nbsp;  
ASSERT_VALID(pDoc);</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">&nbsp;&nbsp;&nbsp;&nbsp;  
pDC-&gt;TextOut(0,0,pDoc-&gt;text);</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">}</font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font LANG="ZH-CN">当窗口的大小改变的时候,应用程序会调用</font>OnDraw(  
)<font LANG="ZH-CN">这个函数。</font></font><font size="2"><font LANG="ZH-CN">添加的这行代码的作用就是把字符串对象</font>text<font LANG="ZH-CN">重新显示在窗口的客户区当中,这样在窗口大小改变的时候,数据依然显示在窗口客户区中。</font></font></p>
<p style="line-height: 100%; text-indent: 0; margin: 0" class="右标题" align="left"> </p>

<hr size="1" color="#008000">
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>            

<p style="line-height: 100%; margin-top: 0; margin-bottom: 0">&nbsp;&nbsp; <span style="position: absolute; left: 32; top: 2086"><a href="right17-1.htm" target="_self"><img border="0" src="rightd1.gif" width="113" height="70"></a></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                                 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="position: absolute; left: 501; top: 2091"><a href="right17-3.htm" target="_self"><img border="0" src="rightd2.gif" width="124" height="63"></a></span></p>                    

</body>

</html>

⌨️ 快捷键说明

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