FIX: Print Preview Doesn't Honor Font Width Setting (173249)



The information in this article applies to:

  • The Microsoft Foundation Classes (MFC), when used with:
    • Microsoft Visual C++, 32-bit Editions 2.0
    • Microsoft Visual C++, 32-bit Editions 2.1
    • Microsoft Visual C++, 32-bit Editions 2.2
    • Microsoft Visual C++, 32-bit Editions 4.0
    • Microsoft Visual C++, 32-bit Editions 4.1
    • Microsoft Visual C++, 32-bit Enterprise Edition 4.2
    • Microsoft Visual C++, 32-bit Professional Edition 4.2
    • Microsoft Visual C++, 32-bit Enterprise Edition 5.0
    • Microsoft Visual C++, 32-bit Professional Edition 5.0
    • Microsoft Visual C++ for Windows, 16-bit edition 1.52

This article was previously published under Q173249

SYMPTOMS

MFC has a Print Preview feature that shows you on the screen what your document should look like when it is printed. However, if you use a font with a non-zero width in your OnDraw routine to make the text appear "condensed" or "squished", then the preview MFC shows you on the screen does not appear as the document will look when it is actually printed.

CAUSE

A bug in the MFC CPreviewDC::MirrorFont forces a zero value for the font width which causes a normal-looking font to be used in the Print Preview.

RESOLUTION

Insert an exact copy of the MFC function into one of your project source files and make the needed corrections. A linker option override will force it to use your corrected version of the function and ignore MFC's function.

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This problem was corrected in Visual C++ version 6.0.

MORE INFORMATION

Steps to Reproduce Behavior

   void CFntWidthView::OnDraw(CDC* pDC)
   {
      pDC->SetMapMode(MM_LOENGLISH);

      LOGFONT lfFont;
      CFont fontNew, *fontOld;
      ZeroMemory(&lfFont, sizeof(lfFont));
      lstrcpy(lfFont.lfFaceName, "Arial");
      lfFont.lfWidth = 10;
      lfFont.lfHeight = 50;
      fontNew.CreateFontIndirect(&lfFont);

      fontOld = pDC->SelectObject(&fontNew);
      pDC->TextOut(0, -100, "Font Width lfWidth = 10.", 24);
      pDC->SelectObject(fontOld);
   }
				

Steps to Workaround

You can work around the problem only for projects statically linked to MFC. The workaround uses the linker /FORCE:MULTIPLE option to override MFC's version of CPreviewDC::MirrorFont with your corrected version. Follow the steps below to work around this problem:

  1. Insert the following statements at the very end of one of your source files:
          #include "afxpriv.h"
          void AFXAPI AfxDeleteObject(HGDIOBJ* pObject);
    					
  2. Locate MFC's CPreviewDC::MirrorFont function (in source file dcprev.cpp) and copy the entire function just after the statements listed above.
  3. Make the following corrections to the function: Change:
    logFont.lfWidth = 0;
    To:
    logFont.lfWidth = tm.tmAveCharWidth;
  4. From the Build menu, click Settings, and on the General tab, select "Use MFC in a static library" in the Microsoft Foundation Classes drop-down box.
  5. Click the Project Settings Link tab. In the Category drop-down box, select Customize and make sure the "Force file output" option is set.
  6. Rebuild your application. When you rebuild, you will get three warnings that can be ignored:
         LINK : warning LNK4075: ignoring /INCREMENTAL due to /FORCE
           specification
    						
         nafxcwd.lib(dcprev.obj) : warning LNK4006: "protected: void __thiscall
           CPreviewDC::MirrorFont(void)"(?MirrorFont@CPreviewDC@@IAEXXZ)
           already defined in FntWidthView.obj; second definition ignored
    						
         Debug/FntWidth.exe : warning LNK4088: image being generated due to
           /FORCE option; image may not run
    						
Now your application should behave correctly. The force option causes the linker to ignore MFC's version of the function and to use your function instead.

For dynamically linked projects, there are no good workarounds other than avoiding custom-width fonts.

Modification Type:MajorLast Reviewed:12/10/2003
Keywords:kbBug kbDocView kbfix kbNoUpdate KbUIDesign kbVC600fix KB173249 kbAudDeveloper