DrawText

  int DrawText(hdc, lpString, cchString, lprc, wFormat)    
  HDC hdc; /* handle of device context */
  LPCTSTR lpString; /* address of string to draw */
  int cchString; /* string length in characters */
  LPRECT lprc; /* address of structure with formatting dimensions */
  UINT wFormat; /* text-drawing flags */

This function draws formatted text in the rectangle specified by the lprc parameter. It formats text by expanding tabs into appropriate spaces, justifying text to the left, right, or center of the given rectangle, and breaking text into lines that fit within the given rectangle. The type of formatting is specified by the wFormat parameter.

The DrawText function uses the device context's selected font, text color, and background color to draw the text. Unless the DT_NOCLIP format is used, DrawText clips the text so that the text does not appear outside the given rectangle. All formatting is assumed to have multiple lines unless the DT_SINGLELINE format is given.

Parameters

hdc

Identifies the device context.

lpString

Points to the string to be drawn. If the cchString parameter is -1, the string must be null-terminated.

cchString

Specifies the number of characters in the string. If cchString is -1, then lpString is assumed to be a pointer to a null-terminated string and DrawText computes the character count automatically.

lprc

Points to a RECT structure that contains the rectangle (in logical coordinates) in which the text is to be formatted.

wFormat

Specifies the method of formatting the text. It can be any combination of the following values:

Value Meaning

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 the lprc parameter 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_EXPANDTABS Expands tab characters. The default number of characters per tab is eight.
DT_EXTERNALLEADING Includes the font external leading in line height. Normally, external leading is not included in the height of a line of text.
DT_LEFT Aligns text flush-left.
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 mnemonic-prefix character & as a directive to underscore the character that follows, and the mnemonic-prefix characters && as a directive to print a single &. By specifying DT_NOPREFIX, this processing is turned off.
DT_RIGHT Aligns text flush-right.
DT_SINGLELINE Specifies single line only. Carriage returns and linefeeds do not break the line.
DT_TABSTOP Sets tab stops. Bits 15–8 (high-order byte of the low-order word) of the wFormat parameter 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 the lprc parameter. A carriage return/line sequence will also break the line.

Note that the DT_CALCRECT, DT_EXTERNALLEADING, DT_INTERNAL, DT_NOCLIP, and DT_NOPREFIX values cannot be used with the DT_TABSTOP value:

Return Value

The return value specifies the height of the text.

Comments

The DrawText function may be used as either a wide-character function (where text arguments must use Unicode) or an ANSI function (where text arguments must use characters from the Windows 3.x character set installed).

If the selected font is too large for the specified rectangle, the DrawText function does not attempt to substitute a smaller font.

See Also

GrayString, TextOut, TabbedTextOut