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

  • On-demand Event Event Date: July 22, 2015 Today's increasingly mobile workforce creates new challenges for organizations that rely heavily on collaboration. Businesses need to be both secure and collaborative, and they can't sacrifice one over the other. The plethora of devices and clouds used by mobile employees only complicates these challenges. Watch this webcast and learn how your business can enable mobile access to files from anywhere while it helps to maintain information governance, compliance, and …

  • An organization's ability to identify, assess and resolve technical issues is critical to business success. Developer, operations and IT teams must be able to collect and analyze data in real-time if they which to resolve issues quickly without creating additional problems. This article introduces the idea of real-time analytics and demonstrates how log data from different layers of the system and application stack can enable real-time analytics and response.

Most Popular Programming Stories

More for Developers

RSS Feeds

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