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

int CALLBACK EnumFontProc<br />

(<br />

LPLOGFONT lplf,<br />

LPNEWTEXTMETRIC lpntm,<br />

DWORD FontType,<br />

LPVOID pLfArray<br />

)<br />

{<br />

static int nRaster=0;<br />

static int nVector=0;<br />

static int nTrueType=0;<br />

}<br />

LPLOGFONT *lpLogFont=(LPLOGFONT *)pLfArray;<br />

if(FontType & RASTER_FONTTYPE)<br />

{<br />

memcpy((*lpLogFont+nRaster), lplf, sizeof(LOGFONT));<br />

nRaster++;<br />

}<br />

else if(FontType & TRUETYPE_FONTTYPE)<br />

{<br />

memcpy((*(lpLogFont+2)+nTrueType), lplf, sizeof(LOGFONT));<br />

nTrueType++;<br />

}<br />

else<br />

{<br />

memcpy((*(lpLogFont+1)+nVector), lplf, sizeof(LOGFONT));<br />

nVector++;<br />

}<br />

return TRUE;<br />

Three static variables are declared here to act as the counters for each type of fonts. When this function<br />

is called, the information of the font is copied from lplf to the buffers allocated in CGDIView::<br />

OnInitialUpdate(), whose address is passed through user-defined parameter.<br />

In function CGDIView::OnInitialUpdate(), after the font families are enumerated, we need to allocate<br />

enough memory, implement the enumeration again for every single type of font:<br />

void CGDIView::OnInitialUpdate()<br />

{<br />

CClientDC dc(this);<br />

CScrollView::OnInitialUpdate();<br />

CSize sizeTotal=CSize(100, 80);<br />

int i;<br />

CFont *ptrFont;<br />

ASSERT(dc.GetSafeHdc());<br />

::EnumFontFamilies<br />

(<br />

dc.GetSafeHdc(),<br />

(LPCTSTR)NULL,<br />

(FONTENUMPROC)EnumFontFamProc,<br />

(LPARAM)m_nFontCount<br />

);<br />

if(m_nFontCount[0] != 0)<br />

{<br />

m_lpLf[0]=(LPLOGFONT) new BYTE[sizeof(LOGFONT)*m_nFontCount[0]];<br />

}<br />

if(m_nFontCount[1] != 0)<br />

{<br />

m_lpLf[1]=(LPLOGFONT) new BYTE[sizeof(LOGFONT)*m_nFontCount[1]];<br />

}<br />

if(m_nFontCount[2] != 0)<br />

{<br />

m_lpLf[2]=(LPLOGFONT) new BYTE[sizeof(LOGFONT)*m_nFontCount[2]];<br />

}<br />

::EnumFontFamilies<br />

(<br />

243

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

Saved successfully!

Ooh no, something went wrong!