11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 9. Font<br />

Here, member lfFaceName specifies the font name; lfHeight and lfWidth specify font size; lfWeight,<br />

lfItalic, lfUnderline and lfStrikeOut specify font styles. Besides these styles, there are two other<br />

styles that can be specified: lfEscapement and lfOrientation.<br />

Under Windows 95, lfEscapement and lfOrientation must be assigned the same value when a font<br />

is being created. If they are non-zero, the text will have an angle with respect to the horizontal border of the<br />

window when it is displayed (Figure 9-1). To display text this way, we must assign the angle to both<br />

lfEscapement and lfOrientation when creating the font, the unit of the angle is one tenth of a degree.<br />

Please note that only True Type fonts can have such orientation.<br />

Escapement angle<br />

Figure 9-1. Font escapement<br />

After a font is created, it can be selected into DC for outputting text. After the text output is over, it<br />

must be selected out of the DC. This procedure is exactly the same with other GDI objects.<br />

Sample 9.1\GDI demonstrates how to create and use a font with specified styles. It is a standard SDI<br />

application generated by Application Wizard. In the sample, the user can choose any available font in the<br />

system and set its styles (bold, italic, underline, etc). The face name of the font will be displayed in the<br />

client window using the selected font, and the user can also set the escapement of the font.<br />

Two variables are added to class CGDIDoc: CGDIDoc::m_fontDraw and CGDIDoc::m_colorFont. The<br />

first variable is declared as a CFont type variable, it will be used to create the font. The second variable is<br />

declared as a COLORREF type variable, it will be used to store the color of the text.<br />

Besides font color, we also need to consider the background color of the text. A text can be displayed<br />

with either a transparent or opaque background. In the latter case, we can set the background to different<br />

colors. In order to display text in different styles, another Boolean type variable CGDIDoc::<br />

m_bTransparentBgd is declared, it will be used to indicate if the background is transparent or opaque.<br />

The following is the modified class CGDIDoc:<br />

……<br />

……<br />

class CGDIDoc : public CDocument<br />

{<br />

protected:<br />

CGDIDoc();<br />

DECLARE_DYNCREATE(CGDIDoc)<br />

CFont m_fontDraw;<br />

COLORREF m_colorFont;<br />

BOOL m_bTransparentBgd;<br />

public:<br />

CFont *GetCurrentFont(){return &m_fontDraw;}<br />

COLORREF GetFontColor(){return m_colorFont;}<br />

BOOL GetBgdStyle(){return m_bTransparentBgd;}<br />

virtual ~CGDIDoc();<br />

};<br />

Besides the three new member variables, there are also three new member functions added to the class.<br />

These functions allow the information stored in CGDIDoc to be accessible outside the class, they are<br />

CGDIDoc ::GetCurrentFont(), CGDIDoc::GetFontColor() and CGDIDoc::GetBgdStyle().<br />

The above variables are initialized in the constructor of class CGDIDoc:<br />

CGDIDoc::CGDIDoc()<br />

{<br />

LOGFONT lf;<br />

CClientDC dc(NULL);<br />

237

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

Saved successfully!

Ooh no, something went wrong!