Visual C++ and MFC Fundamentals Chapter 8 GDI Orientation and Transformations
7.4 Fonts...............................................................................................................
7.4.1 Introduction...........................................................................................
A font is a list of symbols that can be drawn on a device context to produce a message. A
font is designed by an artist but usually follows a specific pattern. For example a font
designed to produce symbols readable in the English language must be designed by a set
of predetermined and agreed upon symbols. These English symbols are grouped in an
entity called the English alphabet. When designing such a font, the symbols created must
conform to that language. This also implies that one font can be significantly different
from another and a font is not necessarily a series of readable symbols.
Just like everything else in the computer, a font must have a name. To accommodate the
visual needs, a font is designed to assume different sizes.
7.4.2 Font Selection.......................................................................................
Before using a font to draw text in a device, the font must have been installed. Microsoft
Windows installs many fonts during setup. To handle its various assignments, the
operating system uses a particular font known as the System Font. This is the font used to
display the menu items and other labels for resources in applications. If you want to use a
different font to draw text in your application, you must select it.
Selecting a font, as well as selecting any other GDI object we will use from now on, is
equivalent to specifying the characteristics of a GDI object you want to use. To do this,
you must first create the object, unless it exists already. To select an object, pass it as a
pointer to the CDC::SelectObject() method. For example, to select a font, the syntax you
would use is:
virtual CFont* SelectObject(CFont* pFont);
This method takes as argument the font you want to use, pFont. It returns a pointer to the
font that was previously selected. If there was a problem when selecting the font, the
method returns NULL. As you can see, you must first have a font you want to select.
7.4.3 Font Creation........................................................................................
A font is created as a variable of the CFont class (of course, you can also use the Win32
API's HFONT class). The CFont class is based on CGdiObject. To declare a CFont
variable, you can use the default constructor of this class. This can be easily done as
follows:
CFont NewFont;
After declaring a CFont variable, you must initialize it. This can be done by calling one
of the Create member functions. The easiest technique of creating a font is done with the
CreatePointFont() method. Its syntax is:
BOOL CreatePointFont(int nPointSize, LPCTSTR lpszFaceName, CDC* pDC =
NULL);
The nPointSize is the height of the font. It is supplied as a multiple of 1/10.