Important:
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
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 ExtTextOutfunction. When you call the ExtTextOutfunction, the operating system passes the call to the graphics engine, which in turn passes the call to the appropriate device driver.

In most cases, ExtTextOutis faster than DrawText. However, there are some instances when DrawTextis more efficient, as in the case where you need to draw multiple lines of text within the borders of a rectangular region. DrawTextdoes 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
  1. Declare global font variables.

    Copy Code
    HDC hdcSurface; 	 // Device context handle
    HFONT hfontTimes; 	 // Font handle
    LOGFONT logfont; 	// Logical font structure
    
  2. 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.

    Copy Code
    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.
    	// ...
      }
    }
    
  3. Remove the font object at the end of the program.

    Copy Code
    void DestroyText ()
    {
      // Destroy font object.
      DeleteObject (hfontTimes);
    }
    

The following code example defines an InitiateTextfunction, which replaces an existing font with a new font, and a ReleaseTextfunction, 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 InitiateTextfunction can be expanded to take a font as a parameter if several fonts have been initialized. SelectObjectreturns a handle to the old settings, which is saved in a global variable so ReleaseTextcan restore the old settings.

Copy Code
// 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 ExtTextOutto print text to the screen. This function takes a DC handle, the screen coordinates, a text string, and an RGB color value as parameters.

Copy Code
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 SetTextColorand GetTextColorfunctions to respectively set and retrieve the color of the text drawn. Call the SetBkColorand GetBkColorfunctions to respectively set or retrieve the background color. Call the SetBkModeand the GetBkModefunctions to respectively set or retrieve the background mode.

Call the GetTextExtentPoint32function to compute the advance width and height of a string of text. Call the GetTextMetricsfunction to retrieve the logical dimensions of a font. Call the GetDeviceCapsfunction to determine the dimensions of an output device.

See Also