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

for(i=0; iGetSafeHandle() != NULL);<br />

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

pDC->TextOut(10, nYPos, (m_lpLf[1]+i)->lfFaceName);<br />

nYPos+=(m_lpLf[1]+i)->lfHeight;<br />

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

ptrFont++;<br />

}<br />

nYPos+=20;<br />

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

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

nYPos+=20;<br />

}<br />

for(i=0; iGetSafeHandle() != NULL);<br />

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

pDC->TextOut(10, nYPos, (m_lpLf[2]+i)->lfFaceName);<br />

nYPos+=(m_lpLf[2]+i)->lfHeight;<br />

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

ptrFont++;<br />

}<br />

For each font family, all the font face names are listed. Three loops are used for this purpose. Within<br />

each loop, one of the enumerated font is selected into the target DC, and function CDC::TextOut(…) is<br />

called to output the font’s face name to the window. To avoid text from overlapping one another, a local<br />

variable nYPos is used as the vertical orgin of the output text, which will increment each time after a line of<br />

text is output to the window.<br />

Because the memory is allocated at the initialization stage, we need to free it when the application<br />

exits. In the sample, WM_DESTROY message handler is added to class CGDIView through using Class Wizard,<br />

and the corresponding member function is implemented as follows:<br />

void CGDIView::OnDestroy()<br />

{<br />

CScrollView::OnDestroy();<br />

if(m_lpLf[0] != NULL)delete [](BYTE *) m_lpLf[0];<br />

if(m_lpLf[1] != NULL)delete [](BYTE *) m_lpLf[1];<br />

if(m_lpLf[2] != NULL)delete [](BYTE *) m_lpLf[2];<br />

if(m_ptrFont != NULL)delete []m_ptrFont;<br />

}<br />

The application is now ready to enumerate all the available fonts in the system.<br />

9.3 Output Text Using CDC::ExtTextOut(…)<br />

Function CDC::ExtTextOut(…)<br />

Usually we use function CDC::TextOut(…) to output text. There is another powerful function CDC::<br />

ExtTextOut(…), which allows us to output the text to a specified rectange. We can use either transparent or<br />

opaque drawing mode. In the latter case, we can also specify a background color. Besides this, we can set<br />

the distances between neighboring characters of the text. One version of function CDC::ExtTextOut(…) has<br />

the following format:<br />

BOOL CDC::ExtTextOut<br />

(<br />

int x, int y,<br />

245

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

Saved successfully!

Ooh no, something went wrong!