drawtext

This function is used to output a string in the specified format within the specified area.

int drawtext(
	LPCTSTR str,
	RECT* pRect,
	UINT uFormat
);
int drawtext(
	TCHAR c,
	RECT* pRect,
	UINT uFormat
);

Parameters

str

A string to be output.

pRect

The pointer to the specified rectangular area. Some uFormat flags use this rectangular area to make a return value. See the details below.

uFormat

Specify how to format the output text. See the details below.

c

A character to be output.

Return Value

When the function executes successfully, the height of the text is returned.

If the DT-VCENTER or DT-BOTTOM flag is specified, the return value represents the offset from the pRect-top to the bottom of the output text.

If the function fails to execute, 0 is returned.

Remarks

By default, the background of the output string is filled with the current background color. Use the function setbkmode to set the background portion of the text to remain transparent or to fill with a background color.

The following are the settings that the uFormat parameter can use to format the text output:

Sign Description
DT_BOTTOM Adjust the text position to the bottom of the rectangle, only if used with DT-SINGLELINE.
DT_CALCRECT Detect the width and height of the rectangle. If there are multiple lines of text, drawtext uses the width specified by pRect and extends the bottom of the rectangle to accommodate each line of text. If there is only one line of text, drawtext modifies the right side of the pRect to hold the last text. In either case, drawtext returns the formatted text height and does not output the text.
DT_CENTER The text is centered horizontal.
DT_EDITCONTROL Copy the visible text as a single-line edit. Specifically, it is based on the average width of the character, which is applied to the editing control in this way, and does not display the last line of the visible part in this way.
DT_END_ELLIPSIS For text display, if the last character of the string is not in the rectangle, it is truncated and identified by an ellipsis. If it is a word instead of a character, the end of it is outside the range of the rectangle and it is not truncated.
The string will not be modified unless the DT-MODIFYSTRING flag is specified.
DT_EXPANDTABS Expand the TAB symbol. By default, each TAB occupies 8 characters. Note that DT-WORD-ELLIPSIS, DT-PATH-ELLIPSIS, and DT-END-ELLIPSIS cannot be used with DT-EXPANDTABIS.
DT_EXTERNALLEADING The line spacing that contains the font in the row height. Typically, row spacing is not contained in the row height of the body.
DT_HIDEPREFIX Windows 2000/XP: The prefix character (&) in the text is ignored, and the character after the prefix character is not underlined. Other prefix characters are still processed.
For example:
Enter string:	"A&bc&&d"
Usually output:	"Abc&d"
DTDT_HIDEPREFIX:"Abc&d"
DT_INTERNAL Use the system font to calculate the wide and high properties of the text.
DT_LEFT The text is aligned left.
DT_MODIFYSTRING Modify the body that specifies the string as displayed. Valid only if used in conjunction with the DT-END-ELLIPSIS or DT-PATH-ELLIPSIS flags.
DT_NOCLIP Makes the output text unrestricted from pRect cropping. Using DT-NOCLIP will make drawtext perform a little faster.
DT_NOFULLWIDTHCHARBREAK Windows 2000/XP: Prevent line breaks from being inserted into DBCS (double-wide character string, called a wide string), and line break rules are equivalent to SBCS strings. Valid only if DT_WORDBREAK with a device. For example, a Chinese character is a wide character, and when the flag is set, consecutive Chinese characters are not interrupted by line breaks like English words.
DT_NOPREFIX Turn off the processing of prefix characters. Typically, DrawText interprets the prefix escape character &  as underlined after it, explaining that && shows a single & . Specify DT_NOPREFIX, and this processing is turned off.
For example:
Enter string:	"A&bc&&d"
Usually output:	"Abc&d"
DT_NOPREFIX:	"A&bc&&d"
DT_PATH_ELLIPSIS For the text displayed, replace the characters in the middle of the string with an odd sign to fit inside the rectangle. If the string contains a backslash (\), DT_PATH_ELLIPSIS to preserve the text behind the last backslash as much as possible.
Strings are not modified unless a DT_MODIFYSTRING specified.
DT_PREFIXONLY Windows 2000/XP: Draw an underscore only at the position of the (&)prefix character. No other characters in the string are drawn.
For example:
Enter string:	"A&bc&&d"
Usually output:	"Abc&d"
DT_PREFIXONLY:	" _   "
DT_RIGHT The text is aligned to the right.
DT_RTLREADING Set the order of reading from right to left (when the text is Hebrew or Arabic). The default reading order is from left to right.
DT_SINGLELINE Make the text appear on one line. Both the carriage return and the line break are invalid.
DT_TABSTOP Set the TAB tab tab position. The 15-8 bits of uFormat specify the character width of the TAB. The default TAB represents an 8-character width. Note that . Note that DT-CALCRECT, DT-EXTERNALLEAD, DT-NOCLIP, and DT-NOPREFIX cannot be used with DT-TABSTOP.
DT_TOP The top of the text is aligned.
DT_VCENTER The text is centered vertical. Valid only when used with DT-SINGLELINE.
DT_WORDBREAK Wrap the line. Line breaks are automatically wrapped when the text exceeds the right boundary (without breaking the word). Carriage returns can also be wrapped.
DT_WORD_ELLIPSIS Cut off unfit text and add an ellipsis at the end.

Examples

The following example outputs a string "Hello World" in the center of the screen:

#include <graphics.h>
#include <conio.h>

int main()
{
	// Drawing window initialization
	initgraph(640, 480);

	// Output string sin in the center of the screen
	RECT r = {0, 0, 639, 479};
	drawtext(_T("Hello World"), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);

	// Press any key to exit
	_getch();
	closegraph();
}
(贡献者:Krissi  编辑