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 />

Chapter 9<br />

Font<br />

Font is another very important GDI object, every application deals with font. Usually a system<br />

contains some default fonts that can be used by all the applications. Besides these default fonts, we<br />

can also install fonts provided by the thirty party. For word processing applications, using font is a<br />

complex issue. There are many things we need to take care. For example, when creating this type of<br />

applications, we need to think about the following issues: how to display a font with different styles; how to<br />

change the text alignment; how to add special effects to characters.<br />

9.1 Outputting Text Using Different Fonts<br />

When we implement a font dialog box, all the available fonts contained in the system will be listed in<br />

it. We can select font size, font name, special styles, and text color.<br />

Like other GDI objects such as pen and brush, we need to create font with specific styles and select it<br />

into a DC in order to use it for outputting text. In <strong>MFC</strong>, the class that can be used to implement font is<br />

CFont. To create a font, we can call either CFont::CreateFont(…) or CFont::CreateFontIndirect(…),<br />

whose formats are listed as follows:<br />

BOOL CFont::CreateFont<br />

(<br />

int nHeight, int nWidth,<br />

int nEscapement, int nOrientation, int nWeight,<br />

BYTE bItalic, BYTE bUnderline, BYTE cStrikeOut,<br />

BYTE nCharSet, BYTE nOutPrecision, BYTE nClipPrecision, BYTE nQuality,<br />

BYTE nPitchAndFamily, LPCTSTR lpszFacename<br />

);<br />

BOOL CFont::CreateFontIndirect(const LOGFONT *lpLogFont);<br />

The first function has many parameters and the second one needs only a LOGFONT type pointer. The<br />

results of the two member functions are exactly the same, every style we need to specify for a font in the<br />

first function has a corresponding member in structure LOGFONT:<br />

typedef struct tagLOGFONT{<br />

LONG lfHeight;<br />

LONG lfWidth;<br />

LONG lfEscapement;<br />

LONG lfOrientation;<br />

LONG lfWeight;<br />

BYTE lfItalic;<br />

BYTE lfUnderline;<br />

BYTE lfStrikeOut;<br />

BYTE lfCharSet;<br />

BYTE lfOutPrecision;<br />

BYTE lfClipPrecision;<br />

BYTE lfQuality;<br />

BYTE lfPitchAndFamily;<br />

TCHAR lfFaceName[LF_FACESIZE];<br />

} LOGFONT;<br />

236

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

Saved successfully!

Ooh no, something went wrong!