PRB: StdFont Rescales When Shared with Printer Object

ID: Q190223


The information in this article applies to:
  • Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, versions 5.0, 6.0
  • Microsoft Visual Basic Standard, Professional, and Enterprise Editions, 16-bit and 32-bit, for Windows, version 4.0


SYMPTOMS

Using a StdFont object and printing to a Form or PictureBox, the font appears to be the wrong size, but the Size property is correct.


CAUSE

The Form or PictureBox is sharing the StdFont object with the Printer object which silently changes the Scale of fonts for proper printing.


RESOLUTION

Do not share a StdFont object between the Printer object and any other object.


STATUS

This behavior is by design.


MORE INFORMATION

When the Printer object gets a new font, it adjusts the scaling factor on the font for the current printer. So if another object is set to the same StdFont, this scaling adjustment will affect it as well. The scaling factor is not exposed to the user, so there is no way to determine that this has happened.

Steps to Reproduce Behavior

  1. Create a new Standard EXE project. Form1 is created by default.


  2. Paste the following code into the module of Form1:
    
          Private Sub Form_Click()
             Dim MyFont As New StdFont
             Dim outstring As String
    
             MyFont.Name = "Arial"
             MyFont.Size = 12
             MyFont.Bold = False
             MyFont.Italic = False
             Me.Cls
    
             Set Me.Font = MyFont
             outstring = Me.Font.Name & ", " & Me.Font.Size
             Me.Print outstring    ' This prints in normal size
             Set Printer.Font = MyFont
    
             ' Since VB caches the font information, we need to refresh it
             Me.Font.Italic = True ' Change Italic to True for demonstration
    
             Me.Print outstring    ' prints out much larger than the first line
         End Sub 


  3. Run the project and click on the form. The following line appears twice on the form:
    
          Arial, 12 
    The first line is in a normal 12 point font, but the second line is obviously much larger, even though it reports still being 12 point. This is because the Printer object has changed the font's scaling.


When the code executes "Set Me.Font = MyFont" it is not copying the font into the form's font property, instead it is setting the form's font to the actual instance of the font object. Both the Printer object and the form are referencing the same font object rather than having their own copies. So if one owner changes the object, it changes for both owners.

Additional query words: kbDSupport kbDSD FontSize kbVBp kbVBp400 kbVBp500 kbVBp600 kbVBp kbdsd kbDSupport

Keywords : kbGrpVB
Version :
Platform : WINDOWS
Issue type : kbprb


Last Reviewed: January 5, 2000
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.