Font Selector Combobox

CFontCombo Overview

The CFontCombo class is a CComboBox-derived class that while being a simple class is very useful in that it enables you to display all the fonts on the system in a combo box for the user's selection. Obviously, for the curious, you not only get the control itself, but the source code that shows how to iterate through the system's fonts and then how to apply a selected font to a bit of text (you can see this bit in the article's demo project shown in the screen shot above).

How it works

The way it works is that when the CFontCombo is subclassed (as with the MFC DDX mechanism), it calls ProcessFonts.
void CFontCombo::PreSubclassWindow() 
 // call function to get system's fonts when control is subclassed
 // call base class' implementation
Once, in the CFontCombo::ProcessFonts member function. As you can see below, ProcessFonts calls the Win32 SDK function, ::EnumFontFamiliesEx. A quick peek at the MSDN Library reveals that "The EnumFontFamiliesEx function enumerates all fonts in the system that match the font characteristics specified by the LOGFONT structure." The main arguments here are the second, third and forth ones. In those, I specify (in order) the LOGFONT structure (which defines which fonts I want - I selected all ANSI fonts), a callback function that Windows will call once for each font found in the system and finally a user supplied pointer to a block of memory.

The first two are pretty much self-explanatory. The last one (the user supplied memory) works like this. I'm passing the address of a CStringArray (called fontlist) which will be passed to my callback function (CFontCombo::EnumFontFamExProc) for each found font. In that callback function (which you'll see shortly), I get the name of a font which I then store in the fontlist. However, the call to EnumFontFamiliesEx doesn't return until all fonts have been enumerated. Therefore, once this call does return, I know that all the fonts are in my fontlist string array. At this point, I simply iterate through that list (see the for loop below) and add each string to the combo box.

void CFontCombo::ProcessFonts(void)

 lf.lfCharSet = ANSI_CHARSET;

                      (FONTENUMPROC) CFontCombo::EnumFontFamExProc, 
                      (LPARAM) &fontlist, 0);

 for(pos = fontlist.GetHeadPosition(); pos != NULL;)
The last thing to look at is the callback function (CFontCombo::EnumFontFamExProc) itself. Here I simply take the font name (from the first argument) and add it to my string array (passed to me as an lparam. The last line (where I return 1) simply tells Windows that I'm not finished and that I want to continue receiving more fonts.
int CALLBACK CFontCombo::EnumFontFamExProc(ENUMLOGFONTEX *lpelfe,
                                           NEWTEXTMETRICEX *lpntme,
                                           int FontType,
                                           LPARAM lParam)
 CStringList* m_temp = (CStringList*) lParam;

 return 1; // I want to get all fonts


Download demo project - 19 Kb
Download source - 1 Kb


Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: February 12, 2015 The evolution of systems engineering with the SysML modeling language has resulted in improved requirements specification, better architectural definition, and better hand-off to downstream engineering. Agile methods have proven successful in the software domain, but how can these methods be applied to systems engineering? Check out this webcast and join Bruce Powel Douglass, author of Real-Time Agility, as he discusses how agile methods have had a tremendous …

  • Remember getting your first box of LEGOS as a kid? How fun it was putting the pieces together, collaborating with your friends to create something new? Now, as an IT professional, assembling and maintaining a Lego-like collaboration infrastructure isn't what you signed up for. Piecing together disparate systems of record for email, web meetings and other applications is about as painful as stepping on a pile of Legos. Download the e-book to learn how implementing a collaboration system connects systems of …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date