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

CFont *ptrFt;<br />

}<br />

ptrFt=dc.GetCurrentFont();<br />

ptrFt->GetLogFont(&lf);<br />

ASSERT(m_fontDraw.CreateFontIndirect(&lf));<br />

m_colorFont=RGB(0, 0, 0);<br />

m_bTransparentBgd=TRUE;<br />

When a DC is created, it selects the default font, pen, brush and other GDI objects. So here we create a<br />

DC that does not belong to any window, and call function CDC::GetCurrentFont() to obtain its currently<br />

selected font (which is the default font). Then function CFont::GetLogFont(…) is called to retrieve the font<br />

information, which is stored in a LOGFONT type object. With this object, we can create a system default font<br />

by calling function CFont::CreateFontIndirect(…). By default, the font color is set to black and the text<br />

background mode is set to transparent.<br />

We need to provide a way of letting user modify the font styles. This can be easily implemented by<br />

using a font common dialog box. In the sample, two commands are added to the application: Font | Select<br />

and Font | Escapement and Orientation, whose IDs are ID_FONT_SELECT and ID_FONT_STYLE<br />

respectively. Also, message handlers are added through using Class Wizard, the corresponding functions<br />

are CGDIDoc::OnFontStyle() and CGDIDoc::OnFontSelect().<br />

Function CGDIDoc::OnFontSelect() lets the user select a font, set its styles, and specify the text color.<br />

It is impelemented as follows:<br />

void CGDIDoc::OnFontSelect()<br />

{<br />

CFontDialog dlg;<br />

LOGFONT lf;<br />

}<br />

if(dlg.DoModal() == IDOK)<br />

{<br />

dlg.GetCurrentFont(&lf);<br />

if(m_fontDraw.GetSafeHandle() != NULL)m_fontDraw.DeleteObject();<br />

ASSERT(m_fontDraw.CreateFontIndirect(&lf));<br />

m_colorFont=dlg.GetColor();<br />

UpdateAllViews(NULL);<br />

}<br />

A font common dialog is implemented to let the user pick up a font. If a font is selected, function<br />

CFontDialog::GetCurrentFont(…) is called to retrieve the information of the font, which is stored in a<br />

LOGFONT type object. Because member m_fontDraw is already initialized, we need to delete the old font<br />

before creating a new one. The font is created by calling function CFont::CreateFontIndirect(…). After<br />

this the color of the font is retrieved by calling function CFontDialog::GetColor(), and stored in the<br />

variable CGDIDoc::m_colorFont. Finally function CDocument::UpdateAllViews(…) is called to update the<br />

client window of the application.<br />

Since font common dialog box does not contain escapement and orientation choices, we have to<br />

implement an extra dialog box to let the user set them. In the sample, dialog template IDD_DIALOG_STYLE is<br />

added for this purpose. Within this template, besides the default “OK” and “Cancel” buttons, there are two<br />

other controls included in the dialog box: edit box IDC_EDIT_ESP, which allows the user to set escapement<br />

angle; check box IDC_CHECK, which allows the user to select text background style (transparent or opaque).<br />

A new class CStyleDlg is added for this dialog template, within which two variables m_lEsp (long type)<br />

and m_bBgdStyle (Boolean type) are declared. Both of them are added through using Class Wizard, and are<br />

associated with controls IDC_EDIT_ESP and IDC_CHECK respectively.<br />

Command Font | Escapement and Orientation is implemented as follows:<br />

void CGDIDoc::OnFontStyle()<br />

{<br />

LOGFONT lf;<br />

CStyleDlg dlg;<br />

m_fontDraw.GetLogFont(&lf);<br />

dlg.m_lEsp=lf.lfEscapement;<br />

238

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

Saved successfully!

Ooh no, something went wrong!