right17-2.htm

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

HTM
319
字号

<p style="line-height: 100%; text-indent: 0; margin-left: 0; margin-right: 0; margin-top: 5; margin-bottom: 0" align="left"><font size="2"><font LANG="ZH-CN">从类关系图中可以看出,</font>CTextApp<font LANG="ZH-CN">类的主要作用是用来处理消息的,它统一管理程序收到的所有消息,然后把消息分配到相应的对象。</font>CMainFrame<font LANG="ZH-CN">是</font>CTextView<font LANG="ZH-CN">的父类,也就是说视窗</font>View<font LANG="ZH-CN">显示在主框窗口</font>MainFrame<font LANG="ZH-CN">的客户区中。类</font>CTextView<font LANG="ZH-CN">的作用是显示数据,而数据的来源是类</font>CTextDoc。</font></p>

<p ALIGN="justify" style="line-height: 150%; margin-top: 5; margin-bottom: 0"><font lang="ZH-CN" size="2">(1)  
CTextApp类</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font LANG="ZH-CN">在</font>MFC<font LANG="ZH-CN">方式的</font>Windows<font LANG="ZH-CN">应用程序中,用来处理消息的是系统自动生成的</font>MFC<font LANG="ZH-CN">中的类</font>CWinApp<font LANG="ZH-CN">的派生类</font>CTextApp。</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font LANG="ZH-CN" size="2">注意这一行代码:</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;  
</font><font size="2">CTextApp theApp;</font></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>CTextApp<font LANG="ZH-CN">类型的对象,生成的时候系统会主动调用</font>Initinstance()<font LANG="ZH-CN">函数完成一些必要的初始化工作。</font></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>Initinstance()<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">CSingleDocTemplate*  
    pDocTemplate;</font></p>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">pDocTemplate 
    = new CSingleDocTemplate(</font></p>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">IDR_MAINFRAME,</font></p>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">RUNTIME_CLASS(CTextDoc),</font></p>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">RUNTIME_CLASS(CMainFrame),  
    // <font LANG="ZH-CN">主</font>SDI <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">RUNTIME_CLASS(CTextView));</font></p>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">AddDocTemplate(pDocTemplate);</font></p>
  </blockquote>
</blockquote>
<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>pDocTemplate<font LANG="ZH-CN">,通过</font>new<font LANG="ZH-CN">操作符,系统动态生成了这个文档模板对象,然后使用</font>AddDocTemplate()<font LANG="ZH-CN">函数把这个文档模板对象加入到应用程序所维护的文档模板链表当中,这个文档模板</font>pDocTemplate<font LANG="ZH-CN">的作用就是把程序用到的框架窗口</font>CMainFrame<font LANG="ZH-CN">、文档</font>CTextDoc<font LANG="ZH-CN">、视窗</font>CTextView<font LANG="ZH-CN">与应用对象</font>CTextApp<font LANG="ZH-CN">联系起来。</font></font></p>
<p ALIGN="justify" style="line-height: 150%; margin-top: 0; margin-bottom: 0"><font LANG="ZH-CN" size="2">(2)  
CMainFrame类</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">CMainFrame<font LANG="ZH-CN">类是由</font>MFC<font LANG="ZH-CN">中的</font>CFrameWnd<font LANG="ZH-CN">派生来的,在类</font>CMainFrame<font LANG="ZH-CN">中,系统已经从类</font>CFrameWnd<font LANG="ZH-CN">那里继承了处理窗口的一般事件的</font>Windows<font LANG="ZH-CN">消息,比如改变窗口的大小、窗口最小化等等的成员函数。</font></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>MFC<font LANG="ZH-CN">程序中,我们并不需要经常对类</font>CMainFrame<font LANG="ZH-CN">进行操作,更多的是对视窗类进行操作,达到对程序中的数据进行编辑和修改的目的。</font></font></p>
<p ALIGN="justify" style="line-height: 150%; margin-top: 0; margin-bottom: 0"><font LANG="ZH-CN" size="2">(3)  
CTextView与CTextDoc类</font></p>
<font SIZE="3">
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 5"></font><font size="2">CTextView<font LANG="ZH-CN">类和</font>CTextDoc<font LANG="ZH-CN">类是密切相关的,文档与视窗的关系如图所示。</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
<img border="0" src="right19.gif" width="285" height="105"></p>
<font SIZE="3">
<p ALIGN="justify" style="line-height: 100%; margin-top: 5; margin-bottom: 0"></font><font LANG="ZH-CN" size="2">在该图中,文档是由文档模板对象生成的,并由应用程序对象管理,用户通过与文档相联系的视窗对象来存储、管理应用程序的数据。</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 size="2">3.  
<font LANG="ZH-CN">添加代码</font></font></p>
<p ALIGN="justify" style="line-height: 150%; margin-top: 0; margin-bottom: 0"><font size="2"><font LANG="ZH-CN">(1)在文档类</font>CTextDoc<font LANG="ZH-CN">中添加成员变量,用来存储要处理数据。</font></font></p>
<table border="1" cellspacing="0" cellpadding="0" style="border-collapse: collapse; mso-border-top-alt: solid windowtext .5pt; mso-border-bottom-alt: solid windowtext .5pt; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; border-style: none; border-width: medium" width="683" bgcolor="#CCFFCC">
  <tr>
    <td width="90" valign="top" style="border-left-style: none; border-left-width: medium; border-right: .5pt solid windowtext; border-top: .5pt solid windowtext; border-bottom: .5pt solid windowtext; padding-left: 5.4pt; padding-right: 5.4pt; padding-top: 0cm; padding-bottom: 0cm" align="center">
      <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>
    <td width="80" valign="top" style="mso-border-left-alt: solid windowtext .5pt; border-left-style: none; border-left-width: medium; border-right: .5pt solid windowtext; border-top: .5pt solid windowtext; border-bottom: .5pt solid windowtext; padding-left: 5.4pt; padding-right: 5.4pt; padding-top: 0cm; padding-bottom: 0cm" align="center">
      <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>
    <td width="61" valign="top" style="mso-border-left-alt: solid windowtext .5pt; border-left-style: none; border-left-width: medium; border-right: .5pt solid windowtext; border-top: .5pt solid windowtext; border-bottom: .5pt solid windowtext; padding-left: 5.4pt; padding-right: 5.4pt; padding-top: 0cm; padding-bottom: 0cm" align="center">
      <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>
    <td width="393" valign="top" style="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: .5pt solid windowtext; border-bottom: .5pt solid windowtext; padding-left: 5.4pt; padding-right: 5.4pt; padding-top: 0cm; padding-bottom: 0cm" align="center">
      <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>
  <tr>
    <td width="90" valign="top" style="mso-border-top-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">text<o:p>
      </o:p> 
      </span></p>
    </td>
    <td width="80" 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">CString<o:p>
      </o:p>
      </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">text</span><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>
  <tr>
    <td width="90" valign="top" style="mso-border-top-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">numberchar<o:p>
      </o:p> 
      </span></p>
    </td>
    <td width="80" 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">int<o:p>
      </o:p>

⌨️ 快捷键说明

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