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
nHeight | Height of characters. |
---|---|
nWidth | Average width of characters. |
nEscapement | Angle of escapement. |
nOrientation | Base-line orientation. |
fnWeight | Weight of the font (e.g., FW_NORMAL). |
fdwItalic | Italic attribute (TRUE or FALSE). |
fdwUnderline | Underline attribute (TRUE or FALSE). |
fdwStrikeOut | Strikeout attribute (TRUE or FALSE). |
fdwCharSet | Character set identifier. |
fdwOutputPrecision | Output precision. |
fdwClipPrecision | Clipping precision. |
fdwQuality | Output quality. |
fdwPitchAndFamily | Pitch and family. |
lpszFace | Typeface 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; }