CreateFont function

Namespace: WinGDI

Header: wingdi.h

Synopsis

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

ParameterDescription
nHeightHeight of characters, in logical units.
nWidthAverage character width, in logical units.
nEscapementAngle of escapement (in tenths of degrees).
nOrientationBase-line orientation angle (in tenths of degrees).
fnWeightFont weight (e.g., FW_NORMAL, FW_BOLD).
fdwItalicItalic flag (TRUE/FALSE).
fdwUnderlineUnderline flag (TRUE/FALSE).
fdwStrikeOutStrikeout flag (TRUE/FALSE).
fdwCharSetCharacter set identifier (e.g., DEFAULT_CHARSET).
fdwOutputPrecisionOutput precision (e.g., OUT_DEFAULT_PRECIS).
fdwClipPrecisionClipping precision (e.g., CLIP_DEFAULT_PRECIS).
fdwQualityOutput quality (e.g., DEFAULT_QUALITY).
fdwPitchAndFamilyPitch and family (e.g., DEFAULT_PITCH | FF_DONTCARE).
lpszFaceTypeface name (e.g., "Arial").

Return Value

If the function succeeds, the return value is a handle to the created font (HFONT). If it fails, the return value is NULL. Use GetLastError for extended error information.

Remarks

Example

#include <windows.h>
#include <stdio.h>

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmd, int nShow) {
    HWND hwnd = GetConsoleWindow();
    HDC hdc = GetDC(hwnd);

    HFONT hFont = CreateFont(
        -12,               // nHeight
        0,                 // nWidth
        0,                 // nEscapement
        0,                 // nOrientation
        FW_BOLD,           // fnWeight
        FALSE,             // fdwItalic
        FALSE,             // fdwUnderline
        FALSE,             // fdwStrikeOut
        DEFAULT_CHARSET,  // fdwCharSet
        OUT_DEFAULT_PRECIS,
        CLIP_DEFAULT_PRECIS,
        DEFAULT_QUALITY,
        DEFAULT_PITCH | FF_SWISS,
        TEXT("Arial")
    );

    if (!hFont) {
        MessageBox(hwnd, TEXT("Failed to create font."), TEXT("Error"), MB_ICONERROR);
        return 0;
    }

    HFONT hOld = (HFONT)SelectObject(hdc, hFont);
    TextOut(hdc, 10, 10, TEXT("Hello, GDI!"), 12);
    SelectObject(hdc, hOld);
    DeleteObject(hFont);
    ReleaseDC(hwnd, hdc);
    return 0;
}

See Also