The EX05B Example

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:

  1. Run AppWizard to generate the EX05B project. The options and the default class names are shown here.

  2. Use ClassWizard to override the OnPrepareDC function in the CEx05bView class. Edit the code in ex05bView.cpp as shown below.

    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); } 

  3. Add a private TraceMetrics helper function to the view class. Add the following prototype in ex05bView.h:

    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); } 

  4. Edit the OnDraw function in ex05bView.cpp. AppWizard always generates a skeleton OnDraw function for your view class. Find the function, and edit the code as follows:

    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); } 

  5. Build and run the EX05B program. Run the program from the debugger to see the TRACE output. The program's window is shown here.

    click to view at full size.

    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.

The EX05B Program Elements

Following is a discussion of the important elements in the EX05B example.

The OnDraw Member Function

The OnDraw function displays character strings in four fonts, as follows:

  • fontTest1—The TrueType font Arial with default width selection.

  • fontTest2—The old-style font Courier with default width selection. Notice how jagged the font appears in larger sizes.

  • fontTest3—The generic Roman font for which Windows supplies the TrueType font Times New Roman with programmed width selection. The width is tied to the horizontal window scale, so the font stretches to fit the window.

  • fontTest4—The LinePrinter font is specified, but because this is not a Windows font for the display, the font engine falls back on the FF_MODERN specification and chooses the TrueType Courier New font.

The TraceMetrics Helper Function

The TraceMetrics helper function calls CDC::GetTextMetrics and CDC::GetTextFace to get the current font's parameters, which it prints in the Debug window.



Programming Microsoft Visual C++
Programming Microsoft Visual C++
ISBN: 1572318570
EAN: 2147483647
Year: 1997
Pages: 332

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net