Win32 Easy Font Handling Tutorial

Exploring LOGFONT, GetObject and CreateFontIndirect in Windows Prog

© Guy Lecky-Thompson

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:

  1. Get a default system font
  2. Determine its characteristics
  3. Populate the LOGFONT structure
  4. Create a new font

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 and Fonts

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:

HFONT hSystemVariableFont = (HFONT ) GetStockObject( ANSI_VAR_FONT );

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.

Using GetObject with LOGFONT

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:

LOGFONT lfSystemVariableFont;
GetObject ( hSystemVariableFont, sizeof(LOGFONT), &lfSystemVariableFont );

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.

Using CreateFontIndirect

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:

HFONT hCustomFont = CreateFontIndirect ( &lfSystemVariableFont );

As long as hCustomFont is not equal to NULL, it can be selected.

Selecting the HFONT

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.


The copyright of the article Win32 Easy Font Handling Tutorial in Windows Programming is owned by Guy Lecky-Thompson. Permission to republish Win32 Easy Font Handling Tutorial must be granted by the author in writing.




Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo