荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: Second (石开), 信区: Program
标  题: 2.2 在窗口中输出文字
发信站: 荔园晨风BBS站 (Wed May 30 01:06:48 2001), 转信

2.2 在窗口中输出文字

在这里我假定读者已经利用ApplicationWizard生成了一个SDI界面的程序代码。接下来
的你只需要在CView派生类的OnDraw成员函数中加入绘图代码就可以了。在这里我需要解
释一下OnDraw函数的作用,OnDraw函数会在窗口需要重绘时自动被调用,传入的参数CD
C* pDC对应的就是DC环境。使用OnDraw的优点就在于在你使用打印功能的时候传入OnDr
aw的DC环境将会是打印机绘图环境,使用打印预览时传入的是一个称为CPreviewDC的绘
图环境,所以你只需要一份代码就可以完成窗口/打印预览/打印机绘图三重功能。利用
Windows的设备无关性和M$为打印预览所编写的上千行代码你可以很容易的完成一个具有
所见即所得的软件。

输出文字一般使用CDC::BOOL TextOut( int x, int y, const CString& str )和CDC::
int DrawText( const CString& str, LPRECT lpRect, UINT nFormat )两个函数,对T
extOut来讲只能输出单行的文字,而DrawText可以指定在一个矩形中输出单行或多行文
字,并且可以规定对齐方式和使用何种风格。nFormat可以是多种以下标记的组合(利用
位或操作)以达到选择输出风格的目的。
DT_BOTTOM底部对齐   Specifies bottom-justified text. This value must be comb
ined with DT_SINGLELINE.

DT_CALCRECT计算指定文字时所需要矩形尺寸   Determines the width and height of
 the rectangle. If there are multiple lines of text, DrawText will use the w
idth of the rectangle pointed to by lpRect and extend the base of the rectan
gle to bound the last line of text. If there is only one line of text, DrawT
ext will modify the right side of the rectangle so that it bounds the last c
haracter in the line. In either case, DrawText returns the height of the for
matted text, but does not draw the text.

DT_CENTER中部对齐   Centers text horizontally.

DT_END_ELLIPSIS or DT_PATH_ELLIPSIS   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 DT_MODIFYSTRING flag is specifi
ed.

You can specify DT_END_ELLIPSIS to replace characters at the end of the stri
ng, or DT_PATH_ELLIPSIS to replace characters in the middle of the string. I
f the string contains backslash (\) characters, DT_PATH_ELLIPSIS preserves a
s much as possible of the text after the last backslash.

DT_EXPANDTABS   Expands tab characters. The default number of characters per
 tab is eight.

DT_EXTERNALLEADING   Includes the font抯 external leading in the line height
. Normally, external leading is not included in the height of a line of text
.
DT_LEFT左对齐   Aligns text flush-left.

DT_MODIFYSTRING   Modifies the given string to match the displayed text. Thi
s flag has no effect unless the DT_END_ELLIPSIS or DT_PATH_ELLIPSIS flag is
specified.

Note Some uFormat flag combinations can cause the passed string to be modifi
ed. Using DT_MODIFYSTRING with either DT_END_ELLIPSIS or DT_PATH_ELLIPSIS ma
y cause the string to be modified, causing an assertion in the CString overr
ide.

DT_NOCLIP   Draws without clipping. DrawText is somewhat faster when DT_NOCL
IP is used.

DT_NOPREFIX禁止使用&前缀   Turns off processing of prefix characters. Normal
ly, DrawText interprets the ampersand (&) mnemonic-prefix character as a dir
ective to underscore the character that follows, and the two-ampersand (&&)
mnemonic-prefix characters as a directive to print a single ampersand. By sp
ecifying DT_NOPREFIX, this processing is turned off.
DT_PATH_ELLIPSIS

DT_RIGHT右对齐   Aligns text flush-right.

DT_SINGLELINE单行输出   Specifies single line only. Carriage returns and lin
efeeds do not break the line.
DT_TABSTOP设置TAB字符所占宽度   Sets tab stops. The high-order byte of nForm
at is the number of characters for each tab. The default number of character
s per tab is eight.

DT_TOP定部对齐   Specifies top-justified text (single line only).
DT_VCENTER中部对齐   Specifies vertically centered text (single line only).
DT_WORDBREAK每行只在单词间被折行   Specifies word-breaking. Lines are automa
tically broken between words if a word would extend past the edge of the rec
tangle specified by lpRect. A carriage return杔inefeed sequence will also br
eak the line.

在输出文字时如果希望改变文字的颜色,你可以利用CDC::SetTextColor( COLORREF cr
Color )进行设置,如果你希望改变背景色就利用CDC::SetBkColor( COLORREF crColor
 ),很多时候你可能需要透明的背景色你可以利用CDC::SetBkMode( int nBkMode )设置
,可接受的参数有
OPAQUE   Background is filled with the current background color before the t
ext, hatched brush, or pen is drawn. This is the default background mode.
TRANSPARENT   Background is not changed before drawing.

接下来讲讲如何创建字体,你可以创建的字体有两种:库存字体CDC::CreateStockObje
ct( int nIndex )和自定义字体。

在创建非库存字体时需要填充一个LOGFONT结构并使用CFont::CreateFontIndirect(con
st LOGFONT* lpLogFont )(可以参考文章在同一系统中显示GB字符和BIG5字符),或使
用CFont::CreateFont( int nHeight, int nWidth, int nEscapement, int nOrientat
ion, int nWeight, BYTE bItalic, BYTE bUnderline, BYTE cStrikeOut, BYTE nChar
Set, BYTE nOutPrecision, BYTE nClipPrecision, BYTE nQuality, BYTE nPitchAndF
amily, LPCTSTR lpszFacename )其中的参数和LOGFONT中的分量有一定的对应关系。下
面分别讲解参数的意义:

nHeight 字体高度(逻辑单位)等于零为缺省高度,否则取绝对值并和可用的字体高度
进行匹配。
nWidth 宽度(逻辑单位)如果为零则使用可用的横纵比进行匹配。
nEscapement 出口矢量与X轴间的角度
nOrientation 字体基线与X轴间的角度
nWeight 字体粗细,可取以下值
Constant Value
FW_DONTCARE 0
FW_THIN 100
FW_EXTRALIGHT 200
FW_ULTRALIGHT 200
FW_LIGHT 300
FW_NORMAL 400
FW_REGULAR 400
FW_MEDIUM 500
FW_SEMIBOLD 600
FW_DEMIBOLD 600
FW_BOLD 700
FW_EXTRABOLD 800
FW_ULTRABOLD 800
FW_BLACK 900
FW_HEAVY 900
bItalic 是否为斜体
bUnderline 是否有下划线
cStrikeOut 是否带删除线
nCharSet 指定字符集合,可取以下值
Constant Value
ANSI_CHARSET 0
DEFAULT_CHARSET 1
SYMBOL_CHARSET 2
SHIFTJIS_CHARSET 128
OEM_CHARSET 255
nOutPrecision 输出精度
OUT_CHARACTER_PRECIS OUT_STRING_PRECIS
OUT_DEFAULT_PRECIS OUT_STROKE_PRECIS
OUT_DEVICE_PRECIS OUT_TT_PRECIS
OUT_RASTER_PRECIS
nClipPrecision 剪辑精度,可取以下值
CLIP_CHARACTER_PRECIS CLIP_MASK
CLIP_DEFAULT_PRECIS CLIP_STROKE_PRECIS
CLIP_ENCAPSULATE CLIP_TT_ALWAYS
CLIP_LH_ANGLES
nQuality 输出质量,可取以下值
DEFAULT_QUALITY   Appearance of the font does not matter.
DRAFT_QUALITY   Appearance of the font is less important than when PROOF_QUA
LITY is used. For GDI raster fonts, scaling is enabled. Bold, italic, underl
ine, and strikeout fonts are synthesized if necessary.
PROOF_QUALITY   Character quality of the font is more important than exact m
atching of the logical-font attributes. For GDI raster fonts, scaling is dis
abled and the font closest in size is chosen. Bold, italic, underline, and s
trikeout fonts are synthesized if necessary.
nPitchAndFamily 字体间的间距
lpszFacename 指定字体名称,为了得到系统所拥有的字体可以利用EmunFontFamiliesE
x。(可以参考文章在同一系统中显示GB字符和BIG5字符)

此外可以利用CFontDialog来得到用户选择的字体的LOGFONT数据。

最后我讲一下文本坐标的计算,利用CDC::GetTextExtent( const CString& str )可以
得到字符串的在输出时所占用的宽度和高度,这样就可以在手工输出多行文字时使用正
确的行距。另外如果需要更精确的对字体高度和宽度进行计算就需要使用CDC::GetText
Metrics( LPTEXTMETRIC lpMetrics ) 该函数将会填充TEXTMETRIC结构,该结构中的分
量可以非常精确的描述字体的各种属性。

--
                            既然热爱生命
                            那么,
                            一切都在意料之中。

※ 来源:·荔园晨风BBS站 bbs.szu.edu.cn·[FROM: 192.168.28.190]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店