Microsoft Docs

CreateFont function

Namespace: GDI32

Header: wingdi.h

Library: gdi32.lib

Syntax

HFONT CreateFont(
    int nHeight,
    int nWidth,
    int nEscapement,
    int nOrientation,
    int fnWeight,
    DWORD fdwItalic,
    DWORD fdwUnderline,
    DWORD fdwStrikeOut,
    DWORD fdwCharSet,
    DWORD fdwOutputPrecision,
    DWORD fdwClipPrecision,
    DWORD fdwQuality,
    DWORD fdwPitchAndFamily,
    LPCTSTR lpszFace
);

Parameters

nHeightHeight of characters.
nWidthAverage width of characters.
nEscapementAngle of escapement.
nOrientationBase-line orientation.
fnWeightWeight of the font (e.g., FW_NORMAL).
fdwItalicItalic attribute (TRUE or FALSE).
fdwUnderlineUnderline attribute (TRUE or FALSE).
fdwStrikeOutStrikeout attribute (TRUE or FALSE).
fdwCharSetCharacter set identifier.
fdwOutputPrecisionOutput precision.
fdwClipPrecisionClipping precision.
fdwQualityOutput quality.
fdwPitchAndFamilyPitch and family.
lpszFaceTypeface name.

Return value

If the function succeeds, the return value is a handle to the newly created font (HFONT). If the function fails, the return value is NULL.

Remarks

CreateFont creates a logical font with the specified characteristics. The created font can be selected into a device context (DC) with SelectObject. For more control, use CreateFontIndirect.

Example

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmd, int nShow)
{
    HWND hWnd = GetDesktopWindow();
    HDC hDC = GetDC(hWnd);

    HFONT hFont = CreateFont(
        -24,               // height
        0,                 // width
        0,                 // escapement
        0,                 // orientation
        FW_BOLD,           // weight
        FALSE,             // italic
        FALSE,             // underline
        FALSE,             // strikeout
        DEFAULT_CHARSET,  // charset
        OUT_DEFAULT_PRECIS,
        CLIP_DEFAULT_PRECIS,
        DEFAULT_QUALITY,
        DEFAULT_PITCH | FF_SWISS,
        TEXT("Arial")      // typeface name
    );

    SelectObject(hDC, hFont);
    TextOut(hDC, 100, 100, TEXT("Hello, GDI!"), 12);

    ReleaseDC(hWnd, hDC);
    DeleteObject(hFont);
    return 0;
}