11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 9. Font<br />

9.2 Enumerating Fonts in the System<br />

Font Types<br />

There are three type of fonts in Windows system: raster font, vector font and True Type font. The<br />

difference among them is how the character glyph is stored for each type of fonts. For raster fonts, the<br />

glyph is simply a bitmap; for vector fonts, the glyph is a collection of end points that define the line<br />

segments; for true type fonts, the glyph is a collection of line and curve commands. The raster fonts can not<br />

be drawn in a scaled size, they are device dependent (the size of the output depends on the resolution of the<br />

device). The vector fonts and true Type Fonts are device independent because they are scalable, however,<br />

drawing True Type fonts is faster than drawing vector fonts. This is because the glyph of True Type fonts<br />

contains commands and hints.<br />

Enumerating Font Family<br />

We can find out all the font families installed in a system by calling API function<br />

::EnumFontFamilies(…), which has the following format:<br />

int EnumFontFamilies<br />

(<br />

HDC hdc, LPCTSTR lpszFamily, FONTENUMPROC lpEnumFontFamProc, LPARAM lParam<br />

);<br />

The first parameter hdc is the handle to a device context, which can be obtained from any window.<br />

The second parameter lpszFamily specifies the font family name that will be enumerated, it could be<br />

any of “Decorative”, “Dontcare”, “Modern”, “Roman”, “Script” and “Swiss”. To enumerate all fonts in the<br />

system, we just need to pass NULL to it.<br />

The third parameter is a pointer to callback function, which will be used to implement the actual<br />

enumeration. This function must be provided by the programmer.<br />

The final parameter lParam is a user-defined parameter that allows us to send information to the<br />

callback function.<br />

The callback function must have the following format:<br />

int CALLBACK EnumFontFamProc<br />

(<br />

ENUMLOGFONT FAR *lpelf, NEWTEXTMETRIC FAR *lpntm, int FontType, LPARAM lParam<br />

);<br />

Each time a new font family is enumerated, this function is called by the system. So the function will<br />

be called for all the available font types (e.g. if there are three types of fonts in the system, this funciton<br />

will be called three times by the system). The font’s information is stored in an ENUMLOGFONT type object<br />

that is pointed by lpelf, and the font type is specified by FontType parameter. We can check<br />

RASTER_FONTTYPE or TRUETYPE_FONTTYPE bit of FontType to judge if the font is a raster font or a true type<br />

font. The final parameter lParam will be used to store the information that is passed through the user<br />

defined parameter (lParam in the function ::EnumFontFamilies(…)).<br />

Sample 9.2\GDI demonstrates how to enumerate all the valid fonts in the system. It is a standard SDI<br />

application generated by Application Wizard. The application will display all the available fonts in the<br />

client window after it is executed. Because there are many types of fonts, the view class of this application<br />

is derived from CScrollView (This can be set in the final step of Application Wizard).<br />

First, an int type array is declared in class CGDIView:<br />

……<br />

class CGDIView : public CScrollView<br />

{<br />

protected:<br />

int m_nFontCount[3];<br />

}<br />

240

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!