I\'m creating window using pure Win32 API (RegisterClass and CreateWindow functions). How can I specify a font for the window instead of system defined one?
When you create your own window class, you are responsible for managing the font yourself. This task will have four parts:
In case you superclass a standard common control that already has its own font handle, use this approach: Just create a font using CreateFont
or CreateFontIndirect
and set it using WM_SETFONT
message (in MFC and ATL there would be a corresponding SetFont
function). When the font is no longer needed, destroy the font using DeleteObject
. Be sure to not destroy the window's previously set font.
In case you're writing a custom control that draws itself, just create a new font object using CreateFont
or CreateFontIndirect
and store it in your class somewhere. If you want to support third-party users, handle WM_SETFONT
and WM_GETFONT
to let the user set another font. When painting, use the current font object stored in your class.
As vividos said just use CreateFont()/CreateFontIndirect:
HFONT hFont = CreateFont (13, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, ANSI_CHARSET,
OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
DEFAULT_PITCH | FF_DONTCARE, TEXT("Tahoma"));
And then set this font for your window/control with the WM_SETFONT message:
SendMessage(window, WM_SETFONT, hFont, TRUE);