Microsoft Windows CE 3.0  

Drawing and Formatting Text

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.

Before text can be drawn to the screen, you need to create a font and set up the appropriate device contexts. Windows 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.

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.
    HDC hdcSurface; // Device context handle HFONT
    hfontTimes; // Font handle LOGFONT logfont; // Logical font
  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 features 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; _tcscpy (logfont.lfFaceName,
    TEXT("Times New Roman")); hfontTimes = CreateFontIndirect
    (&logfont); if (!hfontTimes) { // CreateFontIndirect failed.
    Insert code here for error // handling. // ... } }
  3. Remove the font object at the end of the program.
    void DestroyText () { // Destroy font object.
    DeleteObject (hfontTimes); }

    The following code example defines the initiateTextfunction, which replaces an existing font with a new font, and the 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.

    // 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.

    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

    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.

    Related Topics

    Working with DirectDraw Surfaces

     Last updated on Tuesday, May 18, 2004

    © 2004 Microsoft Corporation. All rights reserved.