📄 teach_road_22.htm
字号:
<html><head><meta http-equiv="Content-Type"content="text/html; charset=gb_2312-80"><title>VC Road</title><style type="text/css" MEDIA="screen"><!--a {text-decoration: underline;}a:hover {color:red; text-decoration:underline}small {color: navy; font-size: 12px ; line-height:16px;}h1 {color: navy; font-size: 12px ; line-height:16px;}
big {color: navy; font-size: 22px ; line-height:26px;}
td {color: navy; font-size: 12px ; line-height:16px;bgcolor="#FFCFA3";}--></style></head>
<body background="../img/ground.gif">
<!-- 耐特付费广告代码开始 不得修改. -->
<center><script language="javascript">
<!--
var date = new Date();
var ra = date.getTime() % 1000;
var ua = document.URL;
document.write("<iframe src='http://www.china-free.com/cgi-bin/ad/random.cgi?id=560' width=468 height=60 scrolling=no marginwidth=0 marginheight=0 frameborder=0 vspace=0 hspace=0 >");
document.write("<a href='http://www.china-free.com/cgi-bin/ad/random.cgi?id=560' target='_blank'>");
document.write("<img src='http://www.china-free.com/cgi-bin/ad/random.cgi?job=go&id=560' width=468 height=60 border=0></a>");
document.write("</iframe>");
//-->
</script>
<noscript>
<a target="_blank" href="http://www.china-free.com/cgi-bin/ad/random.cgi?id=560">
<img src="http://www.china-free.com/cgi-bin/ad/random.cgi?job=go&id=560" width="468" height="60" border="0"></a>
</noscript>
<br><a HREF="http://www.china-free.com/ad" target="_blank"><img SRC="http://www.china-free.com/ad/bd.gif" BORDER="0" ></a></center>
<!-- 耐特付费广告代码结尾. -->
<p align=center><big>2.2 在窗口中输出文字</big></p>
<table border=0 align=center width=80%>
<tr><td>
<p>在这里我假定读者已经利用ApplicationWizard生成了一个SDI界面的程序代码。接下来的你只需要在CView派生类的OnDraw成员函数中加入绘图代码就可以了。在这里我需要解释一下OnDraw函数的作用,OnDraw函数会在窗口需要重绘时自动被调用,传入的参数CDC* pDC对应的就是DC环境。使用OnDraw的优点就在于在你使用打印功能的时候传入OnDraw的DC环境将会是打印机绘图环境,使用打印预览时传入的是一个称为CPreviewDC的绘图环境,所以你只需要一份代码就可以完成窗口/打印预览/打印机绘图三重功能。利用Windows的设备无关性和M$为打印预览所编写的上千行代码你可以很容易的完成一个具有所见即所得的软件。</p>
<p>输出文字一般使用CDC::BOOL TextOut( int x, int y, const CString& str )和CDC::int DrawText( const CString& str, LPRECT lpRect, UINT nFormat )两个函数,对TextOut来讲只能输出单行的文字,而DrawText可以指定在一个矩形中输出单行或多行文字,并且可以规定对齐方式和使用何种风格。nFormat可以是多种以下标记的组合(利用位或操作)以达到选择输出风格的目的。
<ul>
<li>
<b>DT_BOTTOM</b>底部对齐 Specifies bottom-justified text. This value must be combined with <b>DT_SINGLELINE</b>.<br><br></li>
<li>
<b>DT_CALCRECT</b>计算指定文字时所需要矩形尺寸 Determines the width and height of the rectangle. If there are multiple lines of text, <b>DrawText</b> will use the width of the rectangle pointed to by <i>lpRect</i> and extend the base of the rectangle to bound the last line of text. If there is only one line of text, <b>DrawText</b> will modify the right side of the rectangle so that it bounds the last character in the line. In either case, <b>DrawText</b> returns the height of the formatted text, but does not draw the text.<br><br></li>
<li>
<b>DT_CENTER</b>中部对齐 Centers text horizontally.<br><br></li>
<li>
<b>DT_END_ELLIPSIS</b> or <b>DT_PATH_ELLIPSIS </b>Replaces part of the given string with ellipses, if necessary, so that the result fits in the specified rectangle. The given string is not modified unless the <b>DT_MODIFYSTRING</b> flag is specified.<p class=tl>
You can specify <b>DT_END_ELLIPSIS</b> to replace characters at the end of the string, or <b>DT_PATH_ELLIPSIS</b> to replace characters in the middle of the string. If the string contains backslash (\) characters, <b>DT_PATH_ELLIPSIS</b> preserves as much as possible of the text after the last backslash.</P></li>
<li>
<b>DT_EXPANDTABS</b> Expands tab characters. The default number of characters per tab is eight.<br><br></li>
<li>
<b>DT_EXTERNALLEADING</b> Includes the font抯 external leading in the line height. Normally, external leading is not included in the height of a line of text.<br><br></li>
<li>
<b>DT_LEFT</b>左对齐 Aligns text flush-left.<br><br></li>
<li>
<b>DT_MODIFYSTRING </b>Modifies the given string to match the displayed text. This flag has no effect unless the <b>DT_END_ELLIPSIS</b> or <b>DT_PATH_ELLIPSIS</b> flag is specified.<p class=atl>
<B><b>Note</b></B> Some <i>uFormat</i> flag combinations can cause the passed string to be modified. Using <b>DT_MODIFYSTRING</b> with either <b>DT_END_ELLIPSIS</b> or <b>DT_PATH_ELLIPSIS</b> may cause the string to be modified, causing an assertion in the <b>CString</b> override.</p><p class=atl>
</p></li>
<li>
<b>DT_NOCLIP</b> Draws without clipping. <b>DrawText</b> is somewhat faster when <b>DT_NOCLIP</b> is used.<br><br></li>
<li>
<b>DT_NOPREFIX</b>禁止使用&前缀 Turns off processing of prefix characters. Normally, <b>DrawText</b> interprets the ampersand (<b>&</b>) mnemonic-prefix character as a directive to underscore the character that follows, and the two-ampersand (<b>&&</b>) mnemonic-prefix characters as a directive to print a single ampersand. By specifying <b>DT_NOPREFIX</b>, this processing is turned off.<br><br></li>
<li>
<b>DT_PATH_ELLIPSIS </b><br><br></li>
<li>
<b>DT_RIGHT</b>右对齐 Aligns text flush-right.<br><br></li>
<li>
<b>DT_SINGLELINE</b>单行输出 Specifies single line only. Carriage returns and linefeeds do not break the line.<br><br></li>
<li>
<b>DT_TABSTOP</b>设置TAB字符所占宽度 Sets tab stops. The high-order byte of <i>nFormat</i> is the number of characters for each tab. The default number of characters per tab is eight.<br><br></li>
<li>
<b>DT_TOP</b>定部对齐 Specifies top-justified text (single line only).<br><br></li>
<li>
<b>DT_VCENTER</b>中部对齐 Specifies vertically centered text (single line only).<br><br></li>
<li>
<b>DT_WORDBREAK</b>每行只在单词间被折行 Specifies word-breaking. Lines are automatically broken between words if a word would extend past the edge of the rectangle specified by <i>lpRect</i>. A carriage return杔inefeed sequence will also break the line.</li>
</ul></p>
<p>在输出文字时如果希望改变文字的颜色,你可以利用CDC::SetTextColor( COLORREF crColor )进行设置,如果你希望改变背景色就利用CDC::SetBkColor( COLORREF crColor ),很多时候你可能需要透明的背景色你可以利用CDC::SetBkMode( int nBkMode )设置,可接受的参数有
<ul type=disc>
<li>
<b>OPAQUE</b> Background is filled with the current background color before the text, hatched brush, or pen is drawn. This is the default background mode.<br><br></li>
<li>
<b>TRANSPARENT</b> Background is not changed before drawing.</li>
</ul></p>
<p>接下来讲讲如何创建字体,你可以创建的字体有两种:库存字体CDC::CreateStockObject( int nIndex )和自定义字体。<br>在创建非库存字体时需要填充一个LOGFONT结构并使用CFont::CreateFontIndirect(const LOGFONT* lpLogFont )(可以参考文章<a href=http://vchelp.163.net/refer/gb_big5_dis.htm>在同一系统中显示GB字符和BIG5字符</a>),或使用CFont::CreateFont( int nHeight, int nWidth, int nEscapement, int nOrientation, int nWeight, BYTE bItalic, BYTE bUnderline, BYTE cStrikeOut, BYTE nCharSet, BYTE nOutPrecision, BYTE nClipPrecision, BYTE nQuality, BYTE nPitchAndFamily, LPCTSTR lpszFacename )其中的参数和LOGFONT中的分量有一定的对应关系。下面分别讲解参数的意义:</p>
<p>
<i>nHeight</i> 字体高度(逻辑单位)等于零为缺省高度,否则取绝对值并和可用的字体高度进行匹配。<br>
<i>nWidth</i> 宽度(逻辑单位)如果为零则使用可用的横纵比进行匹配。<br>
<i>nEscapement</i> 出口矢量与X轴间的角度<BR>
<i>nOrientation</i> 字体基线与X轴间的角度<BR>
<i>nWeight</i> 字体粗细,可取以下值<BR>
<table border=1 cellpadding=5 cols=2 frame=below rules=rows>
<tr valign=top>
<td class=label width=42%><b>Constant</b></td>
<td class=label width=58%><b>Value</b></td>
</tr>
<tr valign=top>
<td width=42%><b>FW_DONTCARE</b></td>
<td width=58%>0</td>
</tr>
<tr valign=top>
<td width=42%><b>FW_THIN</b></td>
<td width=58%>100</td>
</tr>
<tr valign=top>
<td width=42%><b>FW_EXTRALIGHT</b></td>
<td width=58%>200</td>
</tr>
<tr valign=top>
<td width=42%><b>FW_ULTRALIGHT</b></td>
<td width=58%>200</td>
</tr>
<tr valign=top>
<td width=42%><b>FW_LIGHT</b></td>
<td width=58%>300</td>
</tr>
<tr valign=top>
<td width=42%><b>FW_NORMAL</b></td>
<td width=58%>400</td>
</tr>
<tr valign=top>
<td width=42%><b>FW_REGULAR</b></td>
<td width=58%>400</td>
</tr>
<tr valign=top>
<td width=42%><b>FW_MEDIUM</b></td>
<td width=58%>500</td>
</tr>
<tr valign=top>
<td width=42%><b>FW_SEMIBOLD</b></td>
<td width=58%>600</td>
</tr>
<tr valign=top>
<td width=42%><b>FW_DEMIBOLD</b></td>
<td width=58%>600</td>
</tr>
<tr valign=top>
<td width=42%><b>FW_BOLD</b></td>
<td width=58%>700</td>
</tr>
<tr valign=top>
<td width=42%><b>FW_EXTRABOLD</b></td>
<td width=58%>800</td>
</tr>
<tr valign=top>
<td width=42%><b>FW_ULTRABOLD</b></td>
<td width=58%>800</td>
</tr>
<tr valign=top>
<td width=42%><b>FW_BLACK</b></td>
<td width=58%>900</td>
</tr>
<tr valign=top>
<td width=42%><b>FW_HEAVY</b></td>
<td width=58%>900</td>
</tr>
</table><BR>
<i>bItalic</i> 是否为斜体<BR>
<i>bUnderline</i> 是否有下划线<BR>
<i>cStrikeOut</i> 是否带删除线<BR>
<i>nCharSet</i> 指定字符集合,可取以下值<BR>
<table border=1 cellpadding=5 cols=2 frame=below rules=rows>
<tr valign=top>
<td class=label width=42%><b>Constant</b></td>
<td class=label width=58%><b>Value</b></td>
</tr>
<tr valign=top>
<td width=42%><b>ANSI_CHARSET</b></td>
<td width=58%>0</td>
</tr>
<tr valign=top>
<td width=42%><b>DEFAULT_CHARSET</b></td>
<td width=58%>1</td>
</tr>
<tr valign=top>
<td width=42%><b>SYMBOL_CHARSET</b></td>
<td width=58%>2</td>
</tr>
<tr valign=top>
<td width=42%><b>SHIFTJIS_CHARSET</b></td>
<td width=58%>128</td>
</tr>
<tr valign=top>
<td width=42%><b>OEM_CHARSET</b></td>
<td width=58%>255</td>
</tr>
</table><br>
<i>nOutPrecision</i> 输出精度<BR>
<table border=1 cellpadding=5 cols=2 frame=below rules=rows>
<tr valign=top>
<td width=42%><b>OUT_CHARACTER_PRECIS</b></td>
<td width=58%><b>OUT_STRING_PRECIS</b></td>
</tr>
<tr valign=top>
<td width=42%><b>OUT_DEFAULT_PRECIS</b></td>
<td width=58%><b>OUT_STROKE_PRECIS</b></td>
</tr>
<tr valign=top>
<td width=42%><b>OUT_DEVICE_PRECIS</b></td>
<td width=58%><b>OUT_TT_PRECIS</b></td>
</tr>
<tr valign=top>
<td width=42%><b>OUT_RASTER_PRECIS</b></td>
<td width=58%></td>
</tr>
</table><br>
<i>nClipPrecision</i> 剪辑精度,可取以下值<BR>
<table border=1 cellpadding=5 cols=2 frame=below rules=rows>
<tr valign=top>
<td width=42%><b>CLIP_CHARACTER_PRECIS</b></td>
<td width=58%><b>CLIP_MASK</b></td>
</tr>
<tr valign=top>
<td width=42%><b>CLIP_DEFAULT_PRECIS</b></td>
<td width=58%><b>CLIP_STROKE_PRECIS</b></td>
</tr>
<tr valign=top>
<td width=42%><b>CLIP_ENCAPSULATE</b></td>
<td width=58%><b>CLIP_TT_ALWAYS</b></td>
</tr>
<tr valign=top>
<td width=42%><b>CLIP_LH_ANGLES</b></td>
<td width=58%></td>
</tr>
</table><br>
<i>nQuality</i> 输出质量,可取以下值<BR>
<ul type=disc>
<li>
<b>DEFAULT_QUALITY</b> Appearance of the font does not matter.<br><br></li>
<li>
<b>DRAFT_QUALITY</b> Appearance of the font is less important than when <b>PROOF_QUALITY</b> is used. For GDI raster fonts, scaling is enabled. Bold, italic, underline, and strikeout fonts are synthesized if necessary.<br><br></li>
<li>
<b>PROOF_QUALITY</b> Character quality of the font is more important than exact matching of the logical-font attributes. For GDI raster fonts, scaling is disabled and the font closest in size is chosen. Bold, italic, underline, and strikeout fonts are synthesized if necessary.</li>
</ul>
<i>nPitchAndFamily</i> 字体间的间距<BR>
<i>lpszFacename</i> 指定字体名称,为了得到系统所拥有的字体可以利用EmunFontFamiliesEx。(可以参考文章<a href=http://vchelp.163.net/refer/gb_big5_dis.htm>在同一系统中显示GB字符和BIG5字符</a>)<BR>
</p>
<p>此外可以利用CFontDialog来得到用户选择的字体的LOGFONT数据。</p>
<p>最后我讲一下文本坐标的计算,利用CDC::GetTextExtent( const CString& str )可以得到字符串的在输出时所占用的宽度和高度,这样就可以在手工输出多行文字时使用正确的行距。另外如果需要更精确的对字体高度和宽度进行计算就需要使用CDC::GetTextMetrics( LPTEXTMETRIC lpMetrics ) 该函数将会填充TEXTMETRIC结构,该结构中的分量可以非常精确的描述字体的各种属性。</p>
<p align=center><a href=teach_first.htm#charpter2>返回</a></p>
</td></tr>
</table>
<p align=center><small>版权所有 闻怡洋 <a href=http://vchelp.163.net/>http://vchelp.163.net/</a></small></p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -