drawtext

The function is used to output the 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

The 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 later.

uFormat

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

c

The 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 make the background portion of the text transparent or 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 Detects the width and height of the rectangle. If there are more than one line of text, drawtext uses the width specified by pRect and extends the bottom of the rectangle to hold each line of text. If there is only one line of text, drawtext modifies the right side of the pRect to accommodate 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: Ignores prefix characters in text () and the characters that follow the prefix characters are 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: Prevents line breaks from inserting into the DBCS (double-wide string, or wide string), and the line break rule is equivalent to an SBCS string. Valid only when used with DT-WORDBREAK. For example, a Chinese character is a wide character, and when you set the flag, the continuous Chinese character is not interrupted by line breaks like an English word.
DT_NOPREFIX Close the processing of prefix characters. Typically, DrawText interprets the prefix escape character to underline the character after it to display a single . Specify the DT-NOPREFIX, which is turned off. For example:
Enter string:	"A&bc&&d"
Usually output:	"Abc&d"
DT_NOPREFIX:	"A&bc&&d"
DT_PATH_ELLIPSIS For the displayed text, replace the characters in the middle of the string with an ellipsis to fit inside the rectangle. If the string contains a backslash, dT-PATH?ELLIPSIS keeps the text behind the last backslash as much as possible.
The string will not be modified unless the DT-MODIFYSTRING flag is specified.
DT_PREFIXONLY Windows 2000/XP: Draws an underscore only under the () prefix character's position. 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 right-to-left reading order when the text is Hebrew or Arabic. The default reading order is 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 stop. The 15-8 bit of uFormat specifies the character width of the TAB. The default TAB represents an 8-character width. 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 the 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();
}