This program is similar to EX05A except that it shows multiple fonts. The mapping mode is MM_ANISOTROPIC, with the scale dependent on the window size. The characters change size along with the window. This program effectively shows off some TrueType fonts and contrasts them with the old-style fonts. Here are the steps for building the application:
void CEx05bView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo) { CRect clientRect; GetClientRect(clientRect); pDC->SetMapMode(MM_ANISOTROPIC); // +y = down pDC->SetWindowExt(400, 450); pDC->SetViewportExt(clientRect.right, clientRect.bottom); pDC->SetViewportOrg(0, 0); }
private: void TraceMetrics(CDC* pDC);
Then add the function itself in ex05bView.cpp:
void CEx05bView::TraceMetrics(CDC* pDC) { TEXTMETRIC tm; char szFaceName[100]; pDC->GetTextMetrics(&tm); pDC->GetTextFace(99, szFaceName); TRACE("font = %s, tmHeight = %d, tmInternalLeading = %d," " tmExternalLeading = %d\n", szFaceName, tm.tmHeight, tm.tmInternalLeading, tm.tmExternalLeading); }
void CEx05bView::OnDraw(CDC* pDC) { CFont fontTest1, fontTest2, fontTest3, fontTest4; fontTest1.CreateFont(50, 0, 0, 0, 400, FALSE, FALSE, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, "Arial"); CFont* pOldFont = pDC->SelectObject(&fontTest1); TraceMetrics(pDC); pDC->TextOut(0, 0, "This is Arial, default width"); fontTest2.CreateFont(50, 0, 0, 0, 400, FALSE, FALSE, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_MODERN, "Courier"); // not TrueType pDC->SelectObject(&fontTest2); TraceMetrics(pDC); pDC->TextOut(0, 100, "This is Courier, default width"); fontTest3.CreateFont(50, 10, 0, 0, 400, FALSE, FALSE, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_ROMAN, NULL); pDC->SelectObject(&fontTest3); TraceMetrics(pDC); pDC->TextOut(0, 200, "This is generic Roman, variable width"); fontTest4.CreateFont(50, 0, 0, 0, 400, FALSE, FALSE, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_MODERN, "LinePrinter"); pDC->SelectObject(&fontTest4); TraceMetrics(pDC); pDC->TextOut(0, 300, "This is LinePrinter, default width"); pDC->SelectObject(pOldFont); }
Resize the window to make it smaller, and watch the font sizes change. Compare this window with the previous one.
If you continue to downsize the window, notice how the Courier font stops shrinking after a certain size and how the Roman font width changes.
Following is a discussion of the important elements in the EX05B example.
The OnDraw function displays character strings in four fonts, as follows:
The TraceMetrics helper function calls CDC::GetTextMetrics and CDC::GetTextFace to get the current font's parameters, which it prints in the Debug window.