Partager via


CreateFontIndirect

A version of this page is also available for

Windows Embedded CE 6.0 R3

4/8/2010

This function creates a logical font that has the characteristics defined in the specified structure. An application can subsequently select the font as the current font for any device context.

Syntax

HFONT CreateFontIndirect(
  const LOGFONT* lplf
); 

Parameters

  • lplf
    [in] Long pointer to a LOGFONT structure that defines the characteristics of the logical font.

Return Value

A handle to a logical font indicates success. NULL indicates failure. To get extended error information, call GetLastError.

Remarks

The CreateFontIndirect function creates a logical font with the characteristics specified in the LOGFONT structure. When you select this font by using the SelectObject function, the font mapper for the graphics device interface (GDI) attempts to match the logical font with an existing physical font. If the font mapper fails to find an exact match, the font mapper provides an alternative that has characteristics that match as many of the requested characteristics as possible.

When you no longer need the font, call the DeleteObject function to delete the font.

Windows CE 2.0 and later support either TrueType or raster fonts, but not both. The OEM chooses the font type, raster or TrueType, at system design time, and an application cannot change the font type.

Windows CE 1.0 and 1.01 support only raster fonts.

Warning

The internal leading height of the Meiryo font is greater than for other fonts. This can cause problems unless proper care is taken in programming and testing. A good approach is to use a negative value of lfheight in the LOGFONT structure. See that topic for more information. Use of the Meiryo font should be thoroughly tested.

Example code

The following code sample shows how to use the CreateFontIndirect function with the characteristics specified in the LOGFONT structure.

Note

To make the following code example easier to read, error checking is not included. This code example should not be used in a release configuration unless it has been modified to include secure error handling.

/**********************************************************************
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.
Copyright (c) 2003 Microsoft Corporation. All Rights Reserved.
MODULE: 
  cleartype_sample.cpp
ABSTRACT: 
  This application code draws ClearType fonts with different settings.
**********************************************************************/
#include <windows.h>
#include <tchar.h>
int WINAPI WinMain
(
    HINSTANCE hInstance,
    HINSTANCE hInstPrev,
#ifdef  UNDER_CE
    LPWSTR    pszCmdLine,
#else
    LPSTR     pszCmdLine,
#endif
    int       nCmdShow
)
{
// This retrieves the device context (DC) for the primary display driver. This is not associated with a window.
// This is only used for simplicity of the example.
HDC hdc = GetDC(NULL);
int nSmooth, nOldSmooth;
// A rectangle to hold the text.
RECT rc = { 10, 10, 100, 100};
LOGFONT lf;
HFONT hFontNew, hFontOld;
int nTextHeight;
// Clear out the lf structure to use when creating the font.
memset(&lf, 0, sizeof(LOGFONT));
// Retrieve the old Cleartype gamma.
SystemParametersInfo(SPI_GETFONTSMOOTHINGCONTRAST, 0, &nOldSmooth, FALSE);
// Draw text with the default system font. First calculate the size of the rectangle to use.
DrawText(hdc, TEXT("This is the default system font."), -1, &rc, DT_CALCRECT | DT_LEFT | DT_TOP);
DrawText(hdc, TEXT("This is the default system font."), -1, &rc, DT_LEFT | DT_TOP);
// Draw a Cleartype font with a font smoothing of 1000.
nSmooth = 1000;
SystemParametersInfo(SPI_SETFONTSMOOTHINGCONTRAST, 0, &nSmooth, FALSE);
nTextHeight = rc.bottom - rc.top;
rc.top += nTextHeight;
rc.bottom += nTextHeight;
lf.lfQuality = CLEARTYPE_QUALITY;
hFontNew = CreateFontIndirect(&lf);
hFontOld = (HFONT) SelectObject(hdc, hFontNew);
DrawText(hdc, TEXT("This is a ClearType font w/ 1000 font smoothing."), -1, &rc, DT_CALCRECT | DT_LEFT | DT_TOP);
DrawText(hdc, TEXT("This is a ClearType font w/ 1000 font smoothing."), -1, &rc, DT_LEFT | DT_TOP);
SelectObject(hdc, hFontOld);
DeleteObject(hFontNew);
// Sleep 20 seconds to let the user see the text.
Sleep(20000);
SystemParametersInfo(SPI_SETFONTSMOOTHINGCONTRAST, 0, &nOldSmooth, FALSE);
DeleteDC(hdc);
}

Requirements

Header windows.h
Library coredll.lib
Windows Embedded CE Windows CE 1.0 and later
Windows Mobile Pocket PC for Windows Mobile Version 5.0 and later, Smartphone for Windows Mobile Version 5.0 and later

See Also

Reference

LOGFONT

Other Resources

DeleteObject
SelectObject