HFONT CreateFont(
[in] int cHeight,
[in] int cWidth,
[in] int cEscapement,
[in] int cOrientation,
[in] int cWeight,
[in] DWORD fdwWeight,
[in] DWORD fdwItalic,
[in] DWORD fdwUnderline,
[in] DWORD fdwStrikeOut,
[in] DWORD fdwCharSet,
[in] DWORD fdwOutputPrecision,
[in] DWORD fdwQuality,
[in] DWORD fdwPitchAndFamily,
[in] LPCTSTR lpszFace
);
The CreateFont
function creates a logical font with the specified characteristics. The font object can then be selected into a device context by calling the SelectObject
function.
The parameters specify the desired characteristics of the font. The system uses these values to find the closest match in the available physical fonts.
cHeight
cWidth
cEscapement
cOrientation
parameter.
cOrientation
cWeight
FW_DONTCARE
(0)FW_THIN
(100)FW_EXTRALIGHT
(200)FW_LIGHT
(300)FW_NORMAL
(400)FW_MEDIUM
(500)FW_SEMIBOLD
(600)FW_BOLD
(700)FW_EXTRABOLD
(800)FW_HEAVY
(900)fdwItalic
TRUE
for italic text.FALSE
for non-italic text.fdwUnderline
TRUE
for underlined text.FALSE
for non-underlined text.fdwStrikeOut
TRUE
for strikeout text.FALSE
for non-strikeout text.fdwCharSet
ANSI_CHARSET
DEFAULT_CHARSET
EASTASIAN_CHARSET
GB2312_CHARSET
IDENTITY_CHARSET
SHIFTJIS_CHARSET
SYMBOL_CHARSET
UNICODE_CHARSET
fdwOutputPrecision
OUT_DEFAULT_PRECIS
OUT_STRING_PRECIS
OUT_ONLY_PRECIS
OUT_OUTLINE_PRECIS
OUT_RASTER_PRECIS
OUT_STROKE_PRECIS
OUT_TT_ONLY_PRECIS
OUT_TT_COMPSUBSTRING_PRECIS
fdwQuality
DEFAULT_QUALITY
DRAFT_QUALITY
PROOF_QUALITY
NONANTIALIASED_QUALITY
CLEARTYPE_QUALITY
ANTIALIASED_QUALITY
fdwPitchAndFamily
DEFAULT_PITCH
FIXED_PITCH
VARIABLE_PITCH
FF_DONTCARE
FF_ROMAN
FF_SWISS
FF_MODERN
FF_SCRIPT
FF_DECORATIVE
lpszFace
If the function succeeds, the return value is a handle to the newly created logical font object. A font handle is a value that uniquely identifies a font.
If the function fails, the return value is NULL
.
The GDI maps the font characteristics specified by the CreateFont
parameters to the closest available font in the system. If an exact match is not found, the GDI substitutes font characteristics as necessary.
The interpretation of the cHeight
parameter depends on the coordinate system of the device context.
MM_TEXT
(where 1 logical unit equals 1 pixel), a positive height specifies the height of the font in pixels.MM_HIMETRIC
(where 1000 logical units equal 1 meter), a positive height specifies the height of the font in HIMETRIC units.The cWidth
parameter specifies the average width of characters in the font. For most fonts, this value is the width of the character 'X'. If cWidth
is 0, the GDI selects the closest matching character width for the font.
The cEscapement
and cOrientation
parameters control the orientation of characters within a font.
cEscapement
: The angle, in tenths of a degree, between the escapement vector and the x-axis. The escapement vector determines the direction in which characters are placed on a line.cOrientation
: The angle, in tenths of a degree, between the string's baseline and the x-axis. This parameter affects the rotation of the characters themselves.cEscapement
and cOrientation
values are often the same, resulting in characters being drawn with the specified rotation.
The fdwPitchAndFamily
parameter specifies the pitch (e.g., fixed or variable) and the font family (e.g., Roman, Swiss, Modern). The GDI uses this information to select a font from the specified family that best matches the other font characteristics. If FF_DONTCARE
is specified, the GDI can choose any font family.
The fdwQuality
parameter influences how the GDI selects a font.
DEFAULT_QUALITY
: Font attributes are exactly the same as those specified.DRAFT_QUALITY
: Font attributes are similar but not necessarily the same as those specified.PROOF_QUALITY
: Font attributes are very similar to those specified.NONANTIALIASED_QUALITY
: Font is rendered without antialiasing.CLEARTYPE_QUALITY
: Font is rendered with ClearType technology for improved readability on LCD screens.ANTIALIASED_QUALITY
: Font is rendered with antialiasing.
// Create a bold, italic, underlined font
LOGFONT lf = {0};
lf.lfHeight = -15; // Font height in logical units
lf.lfWeight = FW_BOLD; // Font weight
lf.lfItalic = TRUE; // Italic
lf.lfUnderline = TRUE; // Underlined
lf.lfCharSet = DEFAULT_CHARSET; // Character set
lf.lfOutPrecision = OUT_DEFAULT_PRECIS; // Output precision
lf.lfQuality = DEFAULT_QUALITY; // Font quality
lf.lfPitchAndFamily = DEFAULT_PITCH | FF_SWISS; // Pitch and family
// Copy the face name
wcscpy_s(lf.lfFaceName, LF_FACESIZE, L"Arial");
HFONT hFont = CreateFontIndirect(&lf);
if (hFont != NULL) {
// Select the font into the device context
HDC hdc = GetDC(NULL); // Get screen DC for example
HGDIOBJ hOldFont = SelectObject(hdc, hFont);
// Use the font...
// Clean up
SelectObject(hdc, hOldFont);
DeleteObject(hFont);
ReleaseDC(NULL, hdc);
}