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.
cHeightcWidthcEscapementcOrientation parameter.
cOrientationcWeightFW_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)fdwItalicTRUE for italic text.FALSE for non-italic text.fdwUnderlineTRUE for underlined text.FALSE for non-underlined text.fdwStrikeOutTRUE for strikeout text.FALSE for non-strikeout text.fdwCharSetANSI_CHARSETDEFAULT_CHARSETEASTASIAN_CHARSETGB2312_CHARSETIDENTITY_CHARSETSHIFTJIS_CHARSETSYMBOL_CHARSETUNICODE_CHARSETfdwOutputPrecisionOUT_DEFAULT_PRECISOUT_STRING_PRECISOUT_ONLY_PRECISOUT_OUTLINE_PRECISOUT_RASTER_PRECISOUT_STROKE_PRECISOUT_TT_ONLY_PRECISOUT_TT_COMPSUBSTRING_PRECISfdwQualityDEFAULT_QUALITYDRAFT_QUALITYPROOF_QUALITYNONANTIALIASED_QUALITYCLEARTYPE_QUALITYANTIALIASED_QUALITYfdwPitchAndFamilyDEFAULT_PITCHFIXED_PITCHVARIABLE_PITCHFF_DONTCAREFF_ROMANFF_SWISSFF_MODERNFF_SCRIPTFF_DECORATIVElpszFaceIf 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);
}