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

{<br />

}<br />

POSITION pos;<br />

CGDIView *ptrView;<br />

pos=GetFirstViewPosition();<br />

do<br />

{<br />

ptrView=(CGDIView *)GetNextView(pos);<br />

}while(!ptrView->IsKindOf(RUNTIME_CLASS(CGDIView)));<br />

return ptrView;<br />

We will implement function CGDIView::RedrawCaret() later.<br />

In the sample, member variable m_nCaretIndex is initialized in the constructor along with m_szText:<br />

CGDIDoc::CGDIDoc()<br />

{<br />

m_szText="This is just a test string";<br />

m_nCaretIndex=0;<br />

}<br />

Also, when the document is first created or when a new font is selected, we need to update the value of<br />

m_nCaretVerSize, so functions CGDIDoc::OnNewDocument() and CGDIDoc::OnDialogFont() are updated<br />

as follows:<br />

Old version of CGDIDoc::OnNewDocument():<br />

BOOL CGDIDoc::OnNewDocument()<br />

{<br />

CClientDC dc(NULL);<br />

LOGFONT lf;<br />

CFont *ptrFont;<br />

if(!CDocument::OnNewDocument())return FALSE;<br />

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

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

VERIFY(m_ftDraw.CreateFontIndirect(&lf));<br />

}<br />

return TRUE;<br />

New version of CGDIDoc::OnNewDocument():<br />

BOOL CGDIDoc::OnNewDocument()<br />

{<br />

CClientDC dc(NULL);<br />

LOGFONT lf;<br />

CFont *ptrFont;<br />

TEXTMETRIC tm;<br />

if(!CDocument::OnNewDocument())return FALSE;<br />

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

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

VERIFY(m_ftDraw.CreateFontIndirect(&lf));<br />

dc.GetOutputTextMetrics(&tm);<br />

m_nCaretVerSize=tm.tmHeight;<br />

}<br />

return TRUE;<br />

Old version of CGDIDoc::OnDialogFont():<br />

void CGDIDoc::OnDialogFont()<br />

{<br />

CFontDialog dlg;<br />

LOGFONT lf;<br />

254

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

Saved successfully!

Ooh no, something went wrong!