Drawing Text
A version of this page is also available for
4/8/2010
Before text can be drawn to the screen, you need to create a font and set up the appropriate device contexts. Windows Embedded CE provides a complete set of functions to format and draw text.
After you have selected a font, set your text-formatting options, and computed the necessary character width and height values for a string of text, you can draw characters and symbols using the ExtTextOut function. When you call the ExtTextOut function, the operating system passes the call to the graphics engine, which in turn passes the call to the appropriate device driver.
In most cases, ExtTextOut is faster than DrawText. However, there are some instances when DrawText is more efficient, as in the case where you need to draw multiple lines of text within the borders of a rectangular region. DrawText does not work with rotated text.
The following code examples show what is necessary to initialize, use, and destroy font objects.
To globally declare a font type, a logfont type, and a handle to a device context
Declare global font variables.
HDC hdcSurface; // Device context handle HFONT hfontTimes; // Font handle LOGFONT logfont; // Logical font structure
During initialization of the application, create the font, which in this example is Times Roman Bold. You can set up any number of fonts this way, specifying functionalities such as italic and underlining.
void CreateText () { // First, clear all fields. memset (&logfont, 0, sizeof (logfont)); // Create a GDI Times New Roman font. logfont.lfHeight = 18; logfont.lfWidth = 0; logfont.lfEscapement = 0; logfont.lfOrientation = 0; logfont.lfWeight = FW_BOLD; logfont.lfItalic = FALSE; logfont.lfUnderline = FALSE; logfont.lfStrikeOut = FALSE; logfont.lfCharSet = DEFAULT_CHARSET; logfont.lfOutPrecision = OUT_DEFAULT_PRECIS; logfont.lfClipPrecision = CLIP_DEFAULT_PRECIS; logfont.lfQuality = DEFAULT_QUALITY; logfont.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE; _tcsncpy (logfont.lfFaceName, TEXT("Times New Roman"), LF_FACESIZE); logfont.lfFaceName[LF_FACESIZE-1] = TEXT('\0'); // Ensure null termination hfontTimes = CreateFontIndirect (&logfont); if (!hfontTimes) { // CreateFontIndirect failed. Insert code here for error // handling. // ... } }
Remove the font object at the end of the program.
void DestroyText () { // Destroy font object. DeleteObject (hfontTimes); }
The following code example defines an InitiateText function, which replaces an existing font with a new font, and a ReleaseText function, which restores the old font. The new font must be selected to a device context (DC) before the DC can be used for text output. The InitiateText function can be expanded to take a font as a parameter if several fonts have been initialized. SelectObject returns a handle to the old settings, which is saved in a global variable so ReleaseText can restore the old settings.
// Global variable
HFONT hfontSave;
void InitiateText ()
{
// Get a GDI DC onto the backbuffer, where you will render the text.
g_nLastError = g_lpDDSBack->GetDC(&hdcSurface);
// Select the font into the DC.
hfontSave = (HFONT) SelectObject(hdcSurface, hfontTimes);
// Set the background mode to transparent, so there is no
// rectangle behind the text.
SetBkMode(hdcSurface, TRANSPARENT);
}
void ReleaseText ()
{
// Release the GDI DC.
SelectObject(hdcSurface, hfontSave);
g_nLastError = g_lpDDSBack->ReleaseDC(hdcSurface);
}
The following code example shows how to use ExtTextOut to print text to the screen. This function takes a DC handle, the screen coordinates, a text string, and an RGB color value as parameters.
void printText (HDC hdcSurface, int screen_x, int screen_y,
LPTSTR lpszText, COLORREF color)
{
int bReturn;
// Set text color.
SetTextColor (hdcSurface, color);
bReturn = ExtTextOut (hdcSurface,
screen_x,
screen_y,
0, // No flags set
NULL, // No clipping rectangle
lpszText, // String to display
lstrlen (lpszText), // Number of characters
NULL); // Use default spacing.
}
The following table shows the functions used to draw and format text.
Function | Description |
---|---|
GetTextExtentPoint32 |
Computes the width and height of a string of text. |
GetTextMetrics |
Retrieves the logical dimensions of a font. |
SetTextColor |
Sets the color of drawn text. |
GetTextColor |
Retrieves the color of drawn text. |
SetBkColor |
Sets the background color. |
GetBkColor |
Retrieves the background color. |
SetBkMode |
Sets the background mode. |
GetBkMode |
Retrieves the background mode. |
For an explanation of these functions, see Formatting Text.
The default text color for a display device context is black; the default background color is white; and the default background mode is OPAQUE. The background mode specifies how the background color is to be mixed with the current colors on the video display.
Call the SetTextColor and GetTextColor functions to respectively set and retrieve the color of the text drawn. Call the SetBkColor and GetBkColor functions to respectively set or retrieve the background color. Call the SetBkMode and the GetBkMode functions to respectively set or retrieve the background mode.
Call the GetTextExtentPoint32 function to compute the advance width and height of a string of text. Call the GetTextMetrics function to retrieve the logical dimensions of a font. Call the GetDeviceCaps function to determine the dimensions of an output device.