PRB: Setting Printer.Font.Size with StdFont Gives Wrong Values
This article was previously published under Q194323
This article has been archived. It is offered "as is" and will no longer be updated.
When you set a font size by assigning a value directly to Printer.FontSize,or Printer.Font.Size, you get a different value than when using a StdFontobject.
The issue here is that the default StdFont object is always based on thedisplay hDC (Device Context handle), not the printer hDC. So when executinga line such as this:
Set Printer.Font = MyStdFont
the actual font handle created is using the display Device Context (DC).Because the printer has a slightly different scale than the display, thefont size generated is also a little different.
As long as you consistently use a StdFont object (late binding) orconsistently set the FontSize directly (early binding) and do not try touse both techniques in your code, this behavior should have no noticeableeffect.
This behavior is by design.
A Device Context (DC) is defined as a link between a Windows-basedapplication, a device driver, and an output device, such as a display,printer, or plotter. These DCs are created as needed and are identified bya device context handle, or hDC.
When a StdFont object is instantiated, it has no way of knowing whatdevices will be using it. Therefore, to be consistent, it is always basedon the display Device Context. If it were to try and change its hDC to thatof the device to which it was being assigned, undesired behavior wouldresult for shared Font objects that are used across different devices.
Steps to Reproduce Behavior
- Create a Standard EXE project in Visual Basic. Form1 is created by default.
- Add a CommandButton to Form1.
- Place the following code into Form1's code window:
Private Sub Command1_Click() Printer.FontName = "Arial" Printer.FontSize = 8 Debug.Print Printer.FontName; Debug.Print Printer.FontSize; Debug.Print Printer.TextWidth("TEST") Printer.Font.Name = "Arial" Printer.Font.Size = 8 Debug.Print Printer.FontName; Debug.Print Printer.FontSize; Debug.Print Printer.TextWidth("TEST") Dim f As New StdFont f.Name = "Arial" f.Size = 8 Set Printer.Font = f Debug.Print Printer.FontName; Debug.Print Printer.FontSize; Debug.Print Printer.TextWidth("TEST") End Sub
- Run the project and click on Command1. You should see something similar to the following output in the Immediate window:
Arial 7.92 413
Arial 7.92 413
Arial 8.16 422
The first two values match, but the value when using StdFont is different.
For more information on Device Contexts, search Help for "hDC property" or"device context handle."
For additional information, please see the following article in theMicrosoft Knowledge Base:
190223 : PRB: StdFont Rescales When Shared with Printer Object
(c) Microsoft Corporation 1998. All Rights Reserved.
Contributions by Chris E. Jolley, Microsoft Corporation.
kbDSupport kbDSD kbVBp kbVBp600 kbVBp500 kbFont kbPrinting
Article ID: 194323 - Last Review: 02/28/2014 04:17:04 - Revision: 3.0
Microsoft Visual Basic 5.0 Learning Edition, Microsoft Visual Basic 6.0 Learning Edition, Microsoft Visual Basic 5.0 Professional Edition, Microsoft Visual Basic 6.0 Professional Edition, Microsoft Visual Basic 5.0 Enterprise Edition, Microsoft Visual Basic 6.0 Enterprise Edition
- kbnosurvey kbarchive kbprb KB194323