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

}<br />

dlg.m_bBgdStyle=m_bTransparentBgd;<br />

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

{<br />

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

m_bTransparentBgd=dlg.m_bBgdStyle;<br />

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

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

UpdateAllViews(NULL);<br />

}<br />

First function CFont::GetLogFont(…) is called to retrieve the information of the current font, which is<br />

then stored in a LOGFONT type object. Before the dialog box is invoked, its members CStyleDlg::m_lEsp<br />

and CStyleDlg::m_bBgdStyle are initialized so that the current font’s escapement angle and background<br />

style will be displayed in the dialog box. After function CDialog::DoModal() is called, the new value of<br />

CStyleDlg::m_lEsp is stored back to members lfEscapement and lfOrientation of structure LOGFONT,<br />

and the new value of CStyleDlg::m_bBgdStyle is stored to variable CGDIDoc::m_bTransparentBgd. Then<br />

the old font is deleted and the new font is created. Finally, function CGDIDoc::UpdateAllViews(…) is called<br />

to update the client window.<br />

When we call function CDocument::UpdateAllViews(…), the associated view’s member function<br />

OnDraw(…) will be called automatically. So we need to modify this function to display the font specified by<br />

variable CGDIDoc::m_fontDraw. The following is the implementation of function CGDIView::OnDraw():<br />

void CGDIView::OnDraw(CDC* pDC)<br />

{<br />

CFont *ptrFt;<br />

CFont *ptrFtOld;<br />

CRect rect;<br />

LOGFONT logFont;<br />

}<br />

CGDIDoc* pDoc=GetDocument();<br />

ASSERT_VALID(pDoc);<br />

ptrFt=pDoc->GetCurrentFont();<br />

ASSERT(ptrFt != NULL);<br />

if(ptrFt->GetSafeHandle() != NULL)<br />

{<br />

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

ptrFtOld=pDC->SelectObject(ptrFt);<br />

pDC->SetTextColor(pDoc->GetFontColor());<br />

pDC->SetBkMode(pDoc->GetBgdStyle() ? TRANSPARENT:OPAQUE);<br />

pDC->SetBkColor((~pDoc->GetFontColor())&0x00FFFFFF);<br />

}<br />

GetClientRect(rect);<br />

pDC->TextOut(rect.Width()/4, rect.Height()/4, logFont.lfFaceName);<br />

if(ptrFt->GetSafeHandle() != NULL)pDC->SelectObject(ptrFtOld);<br />

First, function CGDIDoc::GetCurrentFont() is called to retrieve the currently selected font from the<br />

document, then function CFont::GetLogFont(…) is called to retrieve the information of this font (the face<br />

name of the font will be used as the output string). Next, the font is selected into the target DC. Also,<br />

CGDIDoc::GetFontColor() is called to retrieve the current font color, and CDC::SetTextColor(…) is called<br />

to set the text foreground color. Then, CGDIDoc::GetBgdStyle(…) is called to see if the text should be<br />

drawn with an opaque or transparent background, and CDC::SetBkMode(…) is called to set the background<br />

style. Next, the text background color is set to the inverse of the foreground color by calling function<br />

CDC::SetBkColor(…) (If text background style is transparent, this operation has no effect). Finally, function<br />

CDC::TextOut(…) is called to display font’s face name in the client window, and the font is selected out of<br />

the DC.<br />

The default font displayed in the client window should be “System”, which is not a True Type font. To<br />

see how a text can be displayed with different escapement angles, we need to choose a True Type font such<br />

as “Arial”. Please note that the unit of the escapement angle is on tenth of a degree, so if we want to display<br />

the text vertically, escapement angle should be set to 900.<br />

239

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

Saved successfully!

Ooh no, something went wrong!