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
 this->ProcessFonts();
	
 // call base class' implementation
 CComboBox::PreSubclassWindow();
}
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)
{
 LOGFONT lf;
 POSITION pos;

 lf.lfCharSet = ANSI_CHARSET;
 lf.lfFaceName[0]='\0';

 ::EnumFontFamiliesEx(this->GetParent()->GetDC()->m_hDC, 
                      &lf, 
                      (FONTENUMPROC) CFontCombo::EnumFontFamExProc, 
                      (LPARAM) &fontlist, 0);

 for(pos = fontlist.GetHeadPosition(); pos != NULL;)
  this->AddString(fontlist.GetNext(pos));
}
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;
 m_temp->AddTail((char*)lpelfe->elfFullName);

 return 1; // I want to get all fonts
}

Downloads

Download demo project - 19 Kb
Download source - 1 Kb


Comments

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

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Managing your company's financials is the backbone of your business and is vital to the long-term health and viability of your company. To continue applying the necessary financial rigor to support rapid growth, the accounting department needs the right tools to most efficiently do their job. Read this white paper to understand the 10 essentials of a complete financial management system and how the right solution can help you keep up with the rapidly changing business world.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds