📄 vcc59.htm
字号:
<html>
<head>
<title>c++系列</title>
<meta content="text/html; charset=gb2312" http-equiv=Content-Type>
</head>
<p align="center"><script src="../../1.js"></script></a>
<body bgcolor="#ffffff" leftmargin="5" topmargin="1" marginheight="5" marginwidth="5">
<div align=center>
<table border=0 cellpadding=0 cellspacing=0 width=680 align="center">
<tbody>
<tr>
<td width=200 height="59">
</tr>
</tbody>
</table>
<table border=1 bordercolordark=#ffffff bordercolorlight=#ffffff cellpadding=0
cellspacing=0 width=685 align="center" height="70">
<tbody>
<tr>
<td bgcolor=#F9D23C height=14>
<div align=center class=H1><font color="#FFFFFF">在窗口中输出文字</font></div>
</td>
</tr>
<tr valign=top>
<td class=H1 height=51>
<p align="left"><span class="unnamed1">在这里我假定读者已经利用ApplicationWizard生成了一个SDI界面的程序代码。接下来的你只需要在CView派生类的OnDraw成员函数中加入绘图代码就可以了。在这里我需要解释一下OnDraw函数的作用,OnDraw函数会在窗口需要重绘时自动被调用,传入的参数CDC*
pDC对应的就是DC环境。使用OnDraw的优点就在于在你使用打印功能的时候传入OnDraw的DC环境将会是打印机绘图环境,使用打印预览时传入的是一个称为CPreviewDC的绘图环境,所以你只需要一份代码就可以完成窗口/打印预览/打印机绘图三重功能。利用Windows的设备无关性和M$为打印预览所编写的上千行代码你可以很容易的完成一个具有所见即所得的软件。</span></p>
<p class="unnamed1"><span class="unnamed1">输出文字一般使用CDC::BOOL TextOut(
int x, int y, const CString& str )和CDC::int DrawText( const CString&
str, LPRECT lpRect, UINT nFormat )两个函数,对TextOut来讲只能输出单行的文字,而DrawText可以指定在一个矩形中输出单行或多行文字,并且可以规定对齐方式和使用何种风格。nFormat可以是多种以下标记的组合(利用位或操作)以达到选择输出风格的目的。
</span>
<ul>
<li class="unnamed1"> <b>DT_BOTTOM</b>底部对齐 Specifies
bottom-justified text. This value must be combined with <b>DT_SINGLELINE</b>.<br>
<br>
</span>
<li class="unnamed1"> <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>
</span>
<li class="unnamed1"> <b>DT_CENTER</b>中部对齐 Centers
text horizontally.<br>
<br>
</span>
<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. </span>
<p class=tl><span class="unnamed1">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.</span></p>
<li class="unnamed1"> <b>DT_EXPANDTABS</b> Expands
tab characters. The default number of characters per tab is eight.<br>
<br>
</span>
<li class="unnamed1"> <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>
</span>
<li class="unnamed1"> <b>DT_LEFT</b>左对齐 Aligns
text flush-left.<br>
<br>
</span>
<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. </span>
<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.</span></p>
<p class=atl></p>
<li class="unnamed1"> <b>DT_NOCLIP</b> Draws
without clipping. <b>DrawText</b> is somewhat faster when <b>DT_NOCLIP</b>
is used.<br>
<br>
</span>
<li class="unnamed1"> <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>
</span>
<li class="unnamed1"> <b>DT_PATH_ELLIPSIS </b><br>
<br>
</span>
<li class="unnamed1"> <b>DT_RIGHT</b>右对齐 Aligns
text flush-right.<br>
<br>
</span>
<li class="unnamed1"> <b>DT_SINGLELINE</b>单行输出 Specifies
single line only. Carriage returns and linefeeds do not break the
line.<br>
<br>
</span>
<li class="unnamed1"> <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>
</span>
<li class="unnamed1"> <b>DT_TOP</b>定部对齐 Specifies
top-justified text (single line only).<br>
<br>
</span>
<li class="unnamed1"> <b>DT_VCENTER</b>中部对齐 Specifies
vertically centered text (single line only).<br>
<br>
</span>
<li class="unnamed1"> <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. </span></li>
</ul>
<p></p>
<p class="unnamed1"><span class="unnamed1">在输出文字时如果希望改变文字的颜色,你可以利用CDC::SetTextColor(
COLORREF crColor )进行设置,如果你希望改变背景色就利用CDC::SetBkColor( COLORREF crColor
),很多时候你可能需要透明的背景色你可以利用CDC::SetBkMode( int nBkMode )设置,可接受的参数有 </span>
<ul type=disc>
<li class="unnamed1"> <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>
</span>
<li class="unnamed1"> <b>TRANSPARENT</b> Background
is not changed before drawing. </span></li>
</ul>
<p></p>
<p class="unnamed1"> 接下来讲讲如何创建字体,你可以创建的字体有两种:库存字体CDC::CreateStockObject(
int nIndex )和自定义字体。<br>
在创建非库存字体时需要填充一个LOGFONT结构并使用CFont::CreateFontIndirect(const LOGFONT*
lpLogFont )(可以参考文章<a
href="../../vchelp.163.net/refer/gb_big5_dis.htm"
target=_blank>在同一系统中显示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中的分量有一定的对应关系。下面分别讲解参数的意义:</span></p>
<p class="unnamed1"> <i>nHeight</i> 字体高度(逻辑单位)等于零为缺省高度,否则取绝对值并和可用的字体高度进行匹配。<br>
<i>nWidth</i> 宽度(逻辑单位)如果为零则使用可用的横纵比进行匹配。<br>
<i>nEscapement</i> 出口矢量与X轴间的角度<br>
<i>nOrientation</i> 字体基线与X轴间的角度<br>
<i>nWeight</i> 字体粗细,可取以下值<br>
</span>
<table border=1 cellpadding=5 cols=2 frame=below rules=rows>
<tbody>
<tr valign=top>
<td class=label width="42%"> <b>Constant</b></span></td>
<td class=label width="58%"> <b>Value</b></span></td>
</tr>
<tr valign=top>
<td width="42%" class="unnamed1"> <b>FW_DONTCARE</b></span></td>
<td width="58%" class="unnamed1"> 0</span></td>
</tr>
<tr valign=top>
<td width="42%" class="unnamed1"> <b>FW_THIN</b></span></td>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -