Font, Shell and Masked Edit Controls for MFC
(Page 1 of 5 )
Great communication is a de facto standard for anything aiming for success nowadays. We can talk about a business meeting or an application; this is equally important. Every application first must establish reliable and effortless communication with the user, and afterward with the operating system itself. With the introduction of common controls, doing this got a lot more straightforward.
Now you can use these already-optimized controls to resolve the communication between the user and the operating system directly. This assures a consistent working environment for the user across multiple programs and fast application creation for the programmer. All that remains unrevealed is how to pull this off.
While the MSDN contains a lot of information, it is far from perfect. Most of the time you will have the feeling that you are flooded with a massive amount of information while you just want to know what a tool can do and how you can use it. In this article, I will try to fill this gap.
I will use the MSDN example application, which is quite good. If you want to see it for yourself, you can download it from here. The controls I am going to explain are the ones introduced, or at least improved upon, with the launch of the MFC Feature Pack. Therefore, you will need to install this. If you have service pack one for Visual Studio 2008, you already have it. So let's get started.
The font tool allows the user to pick a font from a list. To illustrate the fonts and present as much as possible in a space as small as possible, it uses a combo box. In one of my earlier articles entitled New Controls for MFC, I presented how to create property sheets inside a dialog and use these to add controls. We will use the same approach now. If you are not familiar with this topic, feel free to read that article first.

As the image above already shows, we have four main options we can set in the case of this control. First, at the creation process, because a font combo box in its essence remains a combo box (with custom initial items), you can add a combo box inside the resource editor, and afterward add it as a class of the dialog. Afterward, change the CComboBox to CMFCFontComboBox.
Now the fonts can be of three types: true type, raster or device fonts. Raster fonts are in fact bitmap fonts. Bitmap fonts are simply collections of raster images of glyphs. Therefore, for every character it displays (small, large, italic, bold, and etcetera), there must be another image of it.
True type is an outline font standard developed in the 1980 by Apple. Device, or hardware, fonts are fonts that are provided directly by your system's hardware or by software other than IDL (interface description language). Most of these have a different image icon in front of them: true type fonts get a TT letter, raster fonts get something that resembles a page with writing on it, and the device fonts get no icon.

Item | Effect |
GetSelFont | Returns a CMFCFontInfo pointer about the currently-selected font. |
SelectFont | Select the specified font inside the combo box. You can specify the name of the font with the character set or with a CMFCFontInfo pointer. |
Setup | Initializes the combo box with the corresponding items. The first parameter is an int that contains bitwise or which of the three font types to show; the second is a byte of the character set; the third is a byte that tells the font's pitch and family. Every one of these are optional parameters and have defaults. |
m_bDrawUsingFont | Draws inside the combo box using the specific font. |
In practice, it all looks like this:
CMFCFontComboBox m_wndFont;
…
m_wndFont.SelectFont (_T ("Arial"));
…
int nFontType = 0;
if (m_bTrueType)
{
nFontType |= TRUETYPE_FONTTYPE;
}
if (m_bRaster)
{
nFontType |= RASTER_FONTTYPE;
}
if (m_bDeviceFont)
{
nFontType |= DEVICE_FONTTYPE;
}
CWaitCursor wait;
m_wndFont.Setup (nFontType);
…
CMFCFontComboBox::m_bDrawUsingFont = m_bDrawUsingFont;
With this effect:

Next: Shell controls >>
More .NET Articles
More By Gabor Bernat