📄 chap7_3.htm
字号:
<p><font face="Times New Roman"><b>SetScrollSizes(MM_TEXT,GetDocument()->GetDocSize());</b></font></p>
<p><font face="Times New Roman">CScrollView::OnInitialUpdate();</font></p>
<p><font face="Times New Roman">}</font></p>
<p><font face="Times New Roman"> SetScrollSizes()</font><font >第一个参数为映射模式。</font><font face="Times New Roman">SetScrollSizes()</font><font >可以使用除</font><font
face="Times New Roman">MM_ISOTROPIC</font><font >和</font><font face="Times New Roman">MM_ANISOTROPIC</font><font >之外的其他任何映射模式。</font><font face="Times New Roman">SetScrollSizes()</font><font >第二个参数为文档大小,用一个</font><font face="Times New Roman">CSize</font><font >类型的数值表示。</font></p>
<p><font > 另外,我们还要检查两个包含绘图输出功能的函数:</font><font face="Times New Roman">CEditorView::OnChar()</font><font >和</font><font face="Times New Roman">CEditorView::OnDraw()</font><font >函数。</font></p>
<p><font face="Times New Roman">void
CEditorView::OnChar(UINT nChar, UINT nRepCnt, UINT
nFlags) </font></p>
<p><font face="Times New Roman">{</font></p>
<p><font face="Times New Roman"><b>CEditorDoc*
pDoc=GetDocument();</b></font></p>
<p><font face="Times New Roman"><b>CClientDC
dc(this);</b></font></p>
<p><font face="Times New Roman"><b>CString
line("");//</b></font><font ><b>存放编辑器当前行字符串</b></font></p>
<p><font face="Times New Roman"><b>POSITION
pos=NULL;//</b></font><font ><b>字符串链表位置指示</b></font></p>
<p><font face="Times New Roman"><b>if(nChar=='\r')</b></font></p>
<p><font face="Times New Roman"><b>{</b></font></p>
<p><font face="Times New Roman"><b>pDoc->nLineNum++;</b></font></p>
<p><font face="Times New Roman"><b>}</b></font></p>
<p><font face="Times New Roman"><b>else</b></font></p>
<p><font face="Times New Roman"><b>{</b></font></p>
<p><font face="Times New Roman"><b>//</b></font><font ><b>按行号返回字符串链表中位置值</b></font></p>
<p><font face="Times New Roman"><b>pos=pDoc->lines.FindIndex(pDoc->nLineNum);</b></font></p>
<p><font face="Times New Roman"><b>if(!pos)</b></font></p>
<p><font face="Times New Roman"><b>{</b></font></p>
<p><font face="Times New Roman"><b>//</b></font><font ><b>没有找到该行号对应的行,因此它是一个空行,</b></font></p>
<p><font face="Times New Roman"><b>//</b></font><font ><b>我们把它加到字符串链表中。</b></font></p>
<p><font face="Times New Roman"><b>line+=(char)nChar;</b></font></p>
<p><font face="Times New Roman"><b>pDoc->lines.AddTail(CString(line));</b></font></p>
<p><font face="Times New Roman"><b>}</b></font></p>
<p><font face="Times New Roman"><b>else{</b></font></p>
<p><font face="Times New Roman"><b>//there is a
line,so add the incoming char to the end of</b></font></p>
<p><font face="Times New Roman"><b>//the line</b></font></p>
<p><font face="Times New Roman"><b>line=pDoc->lines.GetAt(pos);</b></font></p>
<p><font face="Times New Roman"><b>line+=(char)nChar;</b></font></p>
<p><font face="Times New Roman"><b>pDoc->lines.SetAt(pos,line);</b></font></p>
<p><font face="Times New Roman"><b>}</b></font></p>
<p><font face="Times New Roman"><b>TEXTMETRIC
tm;</b></font></p>
<p><font face="Times New Roman"><b>dc.GetTextMetrics(&tm);</b></font></p>
<p><font face="Times New Roman"><b>dc.TextOut(0,</b></font></p>
<p><font face="Times New Roman"><b>(int)pDoc->nLineNum*tm.tmHeight,</b></font></p>
<p><font face="Times New Roman"><b>line,</b></font></p>
<p><font face="Times New Roman"><b>line.GetLength());</b></font></p>
<p><font face="Times New Roman"><b>}</b></font></p>
<p><font face="Times New Roman"><b>pDoc->SetModifiedFlag();</b></font></p>
<p><font face="Times New Roman"><b>SetScrollSizes(MM_TEXT,GetDocument()->GetDocSize());</b></font></p>
<p><font face="Times New Roman">CScrollView::OnChar(nChar,nRepCnt,nFlags);</font></p>
<p><font face="Times New Roman">}</font></p>
<p><font > 在程序运行开始的时侯,视图坐标原点和文档坐标原点是重合的。但是,当用户拖动滚动条时,视图原点就与文档原点不一致了,如图</font><font face="Times New Roman">7-14</font><font >。由于</font><font face="Times New Roman">GDI</font><font >是按照文档坐标(逻辑坐标)来输出图形的,这样自然就无法正确显示文档内容。</font></p>
<p align="center"><font ><img src="T7_14.gif"
alt="T7_14.tif (490924 bytes)" width="462" height="542"></font></p>
<p align="center"><font >图</font><font
face="Times New Roman">7-14 </font><font >文档滚动前后文档坐标原点和视图坐标原点的变化</font></p>
<p><font > 这时,要想获得正确输出,就必须调整视图坐标,让视图坐标原点和文档坐标原点重合,如图</font><font face="Times New Roman">7-15</font><font >所示。</font></p>
<p align="center"><font ><img src="T7_15.gif"
alt="T7_15.tif (109159 bytes)" width="420" height="188"></font></p>
<p align="center"><font >图</font><font
face="Times New Roman">7-15 </font><font >调整视图设备上下文原点后</font></p>
<p><font face="Times New Roman">CScrollView</font><font >视图类提供了一个</font><font
face="Times New Roman">CScrollView::OnPrepareDC()</font><font >成员函数,完成视图设备上下文坐标原点的调整工作。</font></p>
<p><font >现在修改</font><font
face="Times New Roman">OnChar()</font><font >,加入</font><font face="Times New Roman">OnPrepareDC()</font><font >函数,见清单</font><font
face="Times New Roman">7.15</font><font >。</font></p>
<p><font ><b>清单</b></font><font
face="Times New Roman"><b>7.15 </b></font><font ><b>修改后的</b></font><font face="Times New Roman"><b>OnChar</b></font><font ><b>成员函数</b></font></p>
<p><font face="Times New Roman">void
CEditorView::OnChar(UINT nChar, UINT nRepCnt, UINT
nFlags) </font></p>
<p><font face="Times New Roman">{</font></p>
<p><font face="Times New Roman"><b>CEditorDoc*
pDoc=GetDocument();</b></font></p>
<p><font face="Times New Roman"><b>CClientDC
dc(this);</b></font></p>
<p><font face="Times New Roman"><b>OnPrepareDC(&dc);</b></font></p>
<p><font face="Times New Roman"><b>CFont*
pOldFont=dc.SelectObject(pFont); </b></font></p>
<p><font face="Times New Roman"><b>CString
line("");//</b></font><font ><b>存放编辑器当前行字符串</b></font></p>
<p><font face="Times New Roman"><b>POSITION
pos=NULL;//</b></font><font ><b>字符串链表位置指示</b></font></p>
<p><font face="Times New Roman"><b>if(nChar=='\r')</b></font></p>
<p><font face="Times New Roman"><b>{</b></font></p>
<p><font face="Times New Roman"><b>pDoc->nLineNum++;</b></font></p>
<p><font face="Times New Roman"><b>}</b></font></p>
<p><font face="Times New Roman"><b>else</b></font></p>
<p><font face="Times New Roman"><b>{</b></font></p>
<p><font face="Times New Roman"><b>//</b></font><font ><b>按行号返回字符串链表中位置值</b></font></p>
<p><font face="Times New Roman"><b>pos=pDoc->lines.FindIndex(pDoc->nLineNum);</b></font></p>
<p><font face="Times New Roman"><b>if(!pos)</b></font></p>
<p><font face="Times New Roman"><b>{</b></font></p>
<p><font face="Times New Roman"><b>//</b></font><font ><b>没有找到该行号对应的行,因此它是一个空行,</b></font></p>
<p><font face="Times New Roman"><b>//</b></font><font ><b>我们把它加到字符串链表中。</b></font></p>
<p><font face="Times New Roman"><b>line+=(char)nChar;</b></font></p>
<p><font face="Times New Roman"><b>pDoc->lines.AddTail(CString(line));</b></font></p>
<p><font face="Times New Roman"><b>}</b></font></p>
<p><font face="Times New Roman"><b>else{</b></font></p>
<p><font face="Times New Roman"><b>//there is a
line,so add the incoming char to the end of</b></font></p>
<p><font face="Times New Roman"><b>//the line</b></font></p>
<p><font face="Times New Roman"><b>line=pDoc->lines.GetAt(pos);</b></font></p>
<p><font face="Times New Roman"><b>line+=(char)nChar;</b></font></p>
<p><font face="Times New Roman"><b>pDoc->lines.SetAt(pos,line);</b></font></p>
<p><font face="Times New Roman"><b>}</b></font></p>
<p><font face="Times New Roman"><b>TEXTMETRIC
tm;</b></font></p>
<p><font face="Times New Roman"><b>dc.GetTextMetrics(&tm);</b></font></p>
<p><font face="Times New Roman"><b>dc.TextOut(0,</b></font></p>
<p><font face="Times New Roman"><b>(int)pDoc->nLineNum*tm.tmHeight,</b></font></p>
<p><font face="Times New Roman"><b>line,</b></font></p>
<p><font face="Times New Roman"><b>line.GetLength());</b></font></p>
<p><font face="Times New Roman"><b>}</b></font></p>
<p><font face="Times New Roman"><b>pDoc->SetModifiedFlag();</b></font></p>
<p><font face="Times New Roman"><b>dc.SelectObject(pOldFont);</b></font></p>
<p><font face="Times New Roman"><b>SetScrollSizes(MM_TEXT,GetDocument()->GetDocSize());</b></font></p>
<p><font face="Times New Roman">CScrollView::OnChar(nChar,nRepCnt,nFlags);</font></p>
<p><font face="Times New Roman">}</font></p>
<p><font > 但是,对于视图</font><font face="Times New Roman">OnDraw()</font><font >函数,则不需要作这样的调整。这是因为,框架在调用</font><font face="Times New Roman">OnDraw()</font><font >之前,已经自动调用了</font><font face="Times New Roman">OnPrepareDC()</font><font >成员函数完成设备上下文坐标调整工作了。</font></p>
<div align="center"><center><table border="2"
cellpadding="2" cellspacing="0" width="100%"
bgcolor="#79D3FF">
<tr>
<td width="100%"><font >提示:对于框架传过来的设备上下文,不需要调用</font><font face="Times New Roman">OnPrepareDC(),</font><font >因为框架知道它是用于绘图的,因此事先调用了</font><font face="Times New Roman">OnPrepareDC()</font><font >作好了坐标调整工作。如果是自己构造或用</font><font face="Times New Roman">GetDC()</font><font >取得得设备上下文,则需要调用</font><font face="Times New Roman">OnPrepareDC()</font><font >完成设备上下文坐标调整工作。</font></td>
</tr>
</table>
</center></div>
<p><font ><br>
现在编辑器已经能够支持文档滚动了,如图</font><font face="Times New Roman">7-16</font><font >。</font></p>
<p align="center"><font > <img src="T7_16.gif"
alt="T7_16.tif (270924 bytes)" width="487" height="349"></font></p>
<p align="center"><font >图</font><font
face="Times New Roman">7-16</font><font >支持滚动的文本编辑器</font></p>
<div align="center"><center><table border="0"
cellpadding="0" cellspacing="0" width="615">
<tr>
<td><a href="chap7_2.htm">上一页</a></td>
<td><p align="right"><a href="chap7_4.htm">下一页</a></p>
</td>
</tr>
</table>
</center></div><hr size="1" noshade color="#3973DE">
<p align="center"><font color="#000000" size="2">本教程由</font><a
href="http://vcdynasty.yeah.net"><font color="#000000"
size="2">Visual C++王朝(Where programmers come
together)</font></a><font color="#000000" size="2">协助制作<br>
未经许可,请勿以任何形式复制</font></p>
</td>
</tr>
</table>
</center>
</div>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -