DrawText API.

2.2 在窗口中輸出文字

在這裏我假定讀者已經利用ApplicationWizard生成了一個SDI界面的程序代碼。接下來的你只需要在CView派生類的OnDraw成員函數中加入繪圖代碼就可以了。在這裏我需要解釋一下OnDraw函數的作用,OnDraw函數會在窗口需要重繪時自動被調用,傳入的參數CDC* pDC對應的就是DC環境。使用OnDraw的優點就在於在你使用打印功能的時候傳入OnDraw的DC環境將會是打印機繪圖環境,使用打印預覽時傳入的是一個稱爲CPreviewDC的繪圖環境,所以你只需要一份代碼就可以完成窗口/打印預覽/打印機繪圖三重功能。利用Windows的設備無關性和M$爲打印預覽所編寫的上千行代碼你可以很容易的完成一個具有所見即所得的軟件。

輸出文字一般使用CDC::BOOL TextOut( int x, int y, const CString& str )和CDC::int DrawText( const CString& str, LPRECT lpRect, UINT nFormat )兩個函數,對TextOut來講只能輸出單行的文字,而DrawText可以指定在一個矩形中輸出單行或多行文字,並且可以規定對齊方式和使用何種風格。nFormat可以是多種以下標記的組合(利用位或操作)以達到選擇輸出風格的目的。

  • DT_BOTTOM底部對齊   Specifies bottom-justified text. This value must be combined with DT_SINGLELINE.

  • DT_CALCRECT計算指定文字時所需要矩形尺寸   Determines the width and height of the rectangle. If there are multiple lines of text, DrawText will use the width of the rectangle pointed to by lpRect and extend the base of the rectangle to bound the last line of text. If there is only one line of text, DrawText will modify the right side of the rectangle so that it bounds the last character in the line. In either case, DrawText returns the height of the formatted 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 specified.

    You can specify DT_END_ELLIPSIS to replace characters at the end of the string, or DT_PATH_ELLIPSIS to replace characters in the middle of the string. If the string contains backslash (/) characters, DT_PATH_ELLIPSIS preserves as 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. This 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 modified. Using DT_MODIFYSTRING with either DT_END_ELLIPSIS or DT_PATH_ELLIPSIS may cause the string to be modified, causing an assertion in the CString override.

  • DT_NOCLIP   Draws without clipping. DrawText is somewhat faster when DT_NOCLIP is used.

  • DT_NOPREFIX禁止使用&前綴   Turns off processing of prefix characters. Normally, DrawText interprets the ampersand (&) mnemonic-prefix character as a directive to underscore the character that follows, and the two-ampersand (&&) mnemonic-prefix characters as a directive to print a single ampersand. By specifying 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 linefeeds do not break the line.

  • DT_TABSTOP設置TAB字符所佔寬度   Sets tab stops. The high-order byte of nFormat is the number of characters for each tab. The default number of characters 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 automatically broken between words if a word would extend past the edge of the rectangle specified by lpRect. A carriage return杔inefeed sequence will also break the line.

在輸出文字時如果希望改變文字的顏色,你可以利用CDC::SetTextColor( COLORREF crColor )進行設置,如果你希望改變背景色就利用CDC::SetBkColor( COLORREF crColor ),很多時候你可能需要透明的背景色你可以利用CDC::SetBkMode( int nBkMode )設置,可接受的參數有

  • OPAQUE   Background is filled with the current background color before the text, hatched brush, or pen is drawn. This is the default background mode.

  • TRANSPARENT   Background is not changed before drawing.

接下來講講如何創建字體,你可以創建的字體有兩種:庫存字體CDC::CreateStockObject( int nIndex )和自定義字體。
在創建非庫存字體時需要填充一個LOGFONT結構並使用CFont::CreateFontIndirect(const LOGFONT* lpLogFont )(可以參考文章在同一系統中顯示GB字符和BIG5字符),或使用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中的分量有一定的對應關係。下面分別講解參數的意義:

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_QUALITY is used. For GDI raster fonts, scaling is enabled. Bold, italic, underline, and strikeout fonts are synthesized if necessary.

  • PROOF_QUALITY   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.
nPitchAndFamily 字體間的間距
lpszFacename 指定字體名稱,爲了得到系統所擁有的字體可以利用EmunFontFamiliesEx。(可以參考文章在同一系統中顯示GB字符和BIG5字符

此外可以利用CFontDialog來得到用戶選擇的字體的LOGFONT數據。

最後我講一下文本座標的計算,利用CDC::GetTextExtent( const CString& str )可以得到字符串的在輸出時所佔用的寬度和高度,這樣就可以在手工輸出多行文字時使用正確的行距。另外如果需要更精確的對字體高度和寬度進行計算就需要使用CDC::GetTextMetrics( LPTEXTMETRIC lpMetrics ) 該函數將會填充TEXTMETRIC結構,該結構中的分量可以非常精確的描述字體的各種屬性。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章