1.6.11 Drawing Formatted Text

The DrawText function formats and draws text within a given rectangle in the client area. This function provides simple text processing that most applications, other than word processors, can use to display text. DrawText output is similar to the output generated by a terminal, except it uses the selected font and can clip the text if it extends outside a given rectangle. DrawText provides many different formatting styles. Table 1.5 lists the styles that are available:

Table 1.5 Text Formatting Styles

Value Description
DT_BOTTOM Bottom-justified (single line only).
DT_CENTER Centered.
DT_EXPANDTABS Expands tab characters into spaces. Otherwise, tabs are treated as single characters. The number of spaces depends on the tab stop size specified by DT_TABSTOP. If DT_TABSTOP is not given, the default is eight spaces.

Table 1.5 Text Formatting Styles (continued)

Value Description
DT_EXTERNALLEADING Includes the font external leading in line height. External leading is not included in the height of a line of text. (Leading is the space between lines of text.) If DT_EXTERNALLEADING is not given, there is no spacing between lines of text. Depending on the selected font, this means that characters in different lines may touch or overlap.
DT_LEFT Left-justified. Default.
DT_NOCLIP Draws text without clipping. All text will be drawn even if it extends outside the specified rectangle. The DrawText function is somewhat faster when DT_NOCLIP is used.
DT_RIGHT Right-justified.
DT_SINGLELINE Single line only. Carriage returns and linefeeds do not break the line. Default is multiple-line formatting.
DT_TABSTOP Sets tab stops. The high-order byte of the wFormat parameter is the number of characters for each tab. If DT_TABSTOP is not given, the default tab size is eight spaces.
DT_TOP Top-justified (single line only). Default.
DT_VCENTER Vertically centered (single line only).
DT_WORDBREAK Sets word breaks. Lines are automatically broken between words if a word would extend past the edge of the rectangle specified by the lpRect parameter. Carriage-return/linefeed sequence also causes a line break. Word-break characters are space, tab, carriage return, linefeed, and carriage-return/linefeed combinations. Applies to multiple-line formatting only.

The DrawText function uses the selected font, so applications can draw formatted text in other than the system font.

DrawText does not hyphenate, and although it can justify text to the left, right, or center, it cannot combine justification styles. In other words, it cannot justify both left and right.

DrawText recognizes a number of control characters and carries out special actions when it encounters them. Table 1.6 lists the control characters and the respective action:

Table 1.6 DrawText Control Characters

Character (ANSI value) Action
Carriage return(13) Interpreted as a line-break character. The text is immediately broken and started on the next line down in the rectangle.
Linefeed(10) Interpreted as a line-break character. The text is immediately broken and started on the next line down in the rectangle.
  A carriage-return/linefeed character combination is interpreted as a single line-break character.
Space(32) Interpreted as a word-break character if the DT_WORDBREAK style is given. If the text is too long to fit on the current line in the formatting rectangle, the line is broken at the closest word-break character to the end of the line.
Tab(9) Expanded into a given number of spaces if the DT_EXPANDTABS style is given. The number of spaces depends on what tab-stop value is given with the DT_TABSTOP style. The default is eight.