Microsoft KB Archive/173249

= FIX: Print Preview Doesn't Honor Font Width Setting =

Article ID: 173249

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 2.0 Professional Edition

 Microsoft Visual C++ 2.1

 Microsoft Visual C++ 2.2

 Microsoft Visual C++ 4.0 Standard Edition</li></ul>

 Microsoft Visual C++ 4.1 Subscription</li></ul>

 Microsoft Visual C++ 4.2 Enterprise Edition</li></ul>

 Microsoft Visual C++ 4.2 Professional Edition</li></ul>

 Microsoft Visual C++ 5.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 5.0 Professional Edition</li></ul>

 Microsoft Visual C++ 1.52 Professional Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q173249

<div class="symptoms_section">

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.

<div class="cause_section">

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.

<div class="resolution_section">

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.

<div class="status_section">

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.

<div class="moreinformation_section">

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:

<ol>  Insert the following statements at the very end of one of your source files: #include "afxpriv.h"     void AFXAPI AfxDeleteObject(HGDIOBJ* pObject); </li> Locate MFC's CPreviewDC::MirrorFont function (in source file dcprev.cpp) and copy the entire function just after the statements listed above.</li>  Make the following corrections to the function: Change: logFont.lfWidth = 0; To: logFont.lfWidth = tm.tmAveCharWidth; </li> 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.</li> <li>Click the Project Settings Link tab. In the Category drop-down box, select Customize and make sure the "Force file output" option is set.</li> <li>Rebuild your application. When you rebuild, you will get three warnings that can be ignored:

<pre class="fixed_text">    LINK : warning LNK4075: ignoring /INCREMENTAL due to /FORCE specification

<pre class="fixed_text">    nafxcwd.lib(dcprev.obj) : warning LNK4006: "protected: void __thiscall       CPreviewDC::MirrorFont(void)"(?MirrorFont@CPreviewDC@@IAEXXZ) already defined in FntWidthView.obj; second definition ignored

<pre class="fixed_text">    Debug/FntWidth.exe : warning LNK4088: image being generated due to       /FORCE option; image may not run

</li></ol>

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.

Keywords: kbbug kbdocview kbfix kbnoupdate kbuidesign kbvc600fix KB173249

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.