Tutorial article to help Win32 programmers use LOGFONT, HFONT, GetObject, and CreateFontIndirect to create fonts for use in their own programs.
This article is designed to help Win32 programmers who are having difficulties creating fonts from scratch using the CreateFontIndirect function and a LOGFONT structure.
The sequence of operations is easy:
In fact, steps 2 and 3 will be combined into one function call and some adjustment of the LOGFONT structure. First, however, a little background.
Text output in Win32 programming involves selecting a font, and then using GDI functions such as DrawText or TextOut to write the text to the Device Context (DC). That DC should have been obtained via a call to GetDC or during the processing of the WM_PAINT message, subsequently through the BeginPaint function.
(If the reader is not familiar with the drawing process, then the Drawing with WM_PAINT Processing article will be of help).
The font is just another selectable GDI (Graphics Device Interface) object, and part of the underlying Windows painting mechanism. As such, there are stock objects available which reflect GUI fonts used by Windows.
So, the first step is to choose a default font, and retrieve it by calling:
Other possible values are listed in the API reference, but the most commonly used ones will usually be:
At this point, do not select it; just retrieve the information about the font, using GetObject.
The LOGFONT structure needs to be populated before it can be used with CreateFontIndirect. Now that there is a handle to a standard font in hSystemVariableFont, it can be done with a call to GetObject:
At this point, the LOGFONT structure contains all the information about the variable font. This is probably not necessary, so adjust it to match your needs.
For example, to make the font bigger, change the lfSystemVariableFont.lfHeight or lfSystemVariableFont.lfWidth members. Or, to create bold text, set the lfSystemVariableFont.lfWeight to a value such as FW_BOLD. Italic text can also be created by setting lfSystemVariableFont.lfItalic to TRUE.
The only things that should not be set are the lfFaceName and lfPicthAndFamily members. This is simply because that would be identical to selecting an arbritary font, and at that point, the GetObject step can be skipped, and you can set each member of LOGFONT yourself.
Once there is a correctly populated LOGFONT structure, call CreateFontIndirect to create the (temporary) font.
The CreateFontIndirect function creates a font in memory that can be used as a GDI object. It is disposable and temporary, but necessary to check the return value against NULL to be sure that it has worked. The call is simple:
As long as hCustomFont is not equal to NULL, it can be selected.
Since HFONT is a GDI object, it can be selected and deleted (when youhave finished with it!) in the same way that you do with other GDI objects:
It is, however, only ever used in calls to TextOut or DrawText, and should never be deleted whilst still selected.