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

Still, we need to display the result in function CView::OnDraw(…). In the sample, this function is<br />

implemented as follows:<br />

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

{<br />

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

ASSERT_VALID(pDoc);<br />

CString szStr;<br />

int nYPos;<br />

nYPos=10;<br />

szStr.Format("Number of raster fonts: %d", m_nFontCount[0]);<br />

pDC->TextOut(10, nYPos, szStr);<br />

nYPos+=20;<br />

szStr.Format("Number of vector fonts: %d", m_nFontCount[1]);<br />

pDC->TextOut(10, nYPos, szStr);<br />

nYPos+=20;<br />

}<br />

szStr.Format("Number of TrueType fonts: %d", m_nFontCount[2]);<br />

pDC->TextOut(10, nYPos, szStr);<br />

nYPos+=20;<br />

We just display three lines of text indicating how many fonts are contained in the system for each<br />

different font family.<br />

Enumerating Font<br />

Apart from the above information (how many fonts there are for each font family), we may further<br />

want to know the exact properties of every font type (i.e., face name). To implement this, we need to<br />

allocate enough memory to store the information of all fonts. Here, the size of this buffer depends on the<br />

number of fonts whose properties are to be retrieved. Since each font need a LOGFONT structure to store all<br />

its information, we can use the following formulae to calculate the required buffer size:<br />

(sizeof structure LOGFONT) × (number of fonts)<br />

For this purpose, in the sample, another two variables are declared in class CGDIView as follows:<br />

……<br />

……<br />

class CGDIView : public CScrollView<br />

{<br />

protected:<br />

}<br />

LPLOGFONT m_lpLf[3];<br />

CFont *m_ptrFont;<br />

Array m_lpLf will be used to store LOGFONT information, and m_ptrFont will be used to store CFont<br />

type variables. The variables are initialized in the constructor as follows:<br />

……<br />

CGDIView::CGDIView()<br />

{<br />

}<br />

m_lpLf[0]=m_lpLf[1]=m_lpLf[2]=NULL;<br />

m_ptrFont=NULL;<br />

We need to provide another callback function to retrieve the actual information for each font type. In<br />

the sample, this callback function is declared and implemented as follows:<br />

int CALLBACK EnumFontProc(LPLOGFONT, LPNEWTEXTMETRIC, DWORD, LPVOID);<br />

242

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

Saved successfully!

Ooh no, something went wrong!