CreateFontIndirect

2.x

  HFONT CreateFontIndirect(lplf)    
  const LOGFONT FAR* lplf; /* address of struct. with font attributes */

The CreateFontIndirect function creates a logical font that has the characteristics given in the specified structure. The font can subsequently be selected as the current font for any device.

Parameters

lplf

Points to a LOGFONT structure that defines the characteristics of the logical font. The LOGFONT structure has the following form:

typedef struct tagLOGFONT {     /* lf */
    int   lfHeight;
    int   lfWidth;
    int   lfEscapement;
    int   lfOrientation;
    int   lfWeight;
    BYTE  lfItalic;
    BYTE  lfUnderline;
    BYTE  lfStrikeOut;
    BYTE  lfCharSet;
    BYTE  lfOutPrecision;
    BYTE  lfClipPrecision;
    BYTE  lfQuality;
    BYTE  lfPitchAndFamily;
    BYTE  lfFaceName[LF_FACESIZE];
} LOGFONT;

For a full description of this structure, see the Microsoft Windows Programmer's Reference, Volume 3.

Return Value

The return value is the handle of the logical font if the function is successful. Otherwise, it is NULL.

Comments

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

Fonts created by using the CreateFontIndirect function must be selected out of any device context in which they were used and then removed by using the DeleteObject function.

Example

The following example uses the CreateFontIndirect function to retrieve the handle of a logical font. The nPtSize and pszFace parameters are passed to the function containing this code. The MulDiv and GetDeviceCaps functions are used to convert the specified point size into the correct point size for the MM_TEXT mapping mode on the current device.

HFONT hfont, hfontOld;

PLOGFONT plf = (PLOGFONT) LocalAlloc(LPTR, sizeof(LOGFONT));

plf->lfHeight = -MulDiv(nPtSize, GetDeviceCaps(hdc, LOGPIXELSY), 72);
strcpy(plf->lfFaceName, pszFace);

hfont = CreateFontIndirect(plf);

hfontOld = SelectObject(hdc, hfont);

TextOut(hdc, 10, 50, pszFace, strlen(pszFace));

LocalFree((HLOCAL) plf);
SelectObject(hdc, hfontOld);
DeleteObject(hfont);

See Also

CreateFont, DeleteObject