HOWTO: VB3: Adjust FontSize at Run-time for Diff. Video Drivers

Last reviewed: September 29, 1997
Article ID: Q106164
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, version 3.0

SUMMARY

Text on controls and labels may have a different size or appearance at run time depending on the video device and driver. For example, many video drivers have a large-fonts option and a small-fonts option. This article describes how to adjust text at run-time automatically to fit the design- time label size, independent of the video driver.

MORE INFORMATION

Predicting how fonts will appear on different display devices is not easy. However, you can calibrate the appropriate FontSize to use at run time by using the following example. This example adjusts the form's FontSize so that a particular label caption fits best inside its Width. The label's Width can be set at design time to adjust how big the fonts ought to appear at run time. This example assumes a True Type FontName setting, which can be set to almost any size needed.

Step-by-Step Example

  1. Start Visual Basic, and begin a new project.

  2. Draw a label on a form.

  3. Add the following code to the form's Load event:

       Sub Form_Load ()
    
          ' This procedure determines the appropriate FontSize to display
          ' text in a label that was sized at design time.
    
          Const SMALLESTFONTSIZE = 3  'Enter smallest available font size
    
          ' 1. Assign the FontName properties of the label to the Form:
          '    use Me (a reserved word describing the current form) instead of
          '    the name of the label control because you are comparing sizes
          '    using the TextWidth property. TextWidth returns the width as if
          '    it was printed directly on an object (the form). The TextWidth
          '    property does not apply to controls.
          Me.FontName = Label1.FontName
          Me.FontSize = Label1.FontSize
    
          ' 2. Increase FontSize until Caption is too wide too fit in label:
          i = Me.FontSize
          Do Until Me.TextWidth(Label1.Caption) >= Label1.Width
             i = i + 1
             Me.FontSize = i
             'Debug.Print Me.FontSize
          Loop
    
          ' 3. Decrease FontSize until Caption fits width-wise in label.
          '    NOTE: If the fontsize becomes less than SMALLESTFONTSIZE below,
          '    the Caption is too big for the current label size, even with
          '    the smallest available fontsize.
    
          i = Me.FontSize
          Do Until Me.TextWidth(Label1.Caption) <= Label1.Width
             i = i - 1
             If i < SMALLESTFONTSIZE Then
                MsgBox "Caption width truncated to fit label - smallest font."
                Exit Sub
             End If
             Me.FontSize = i
             'Debug.Print "width:" & i; Me.FontSize
          Loop
    
          ' 4. Decrease FontSize until Caption fits height-wise in label:
          i = Me.FontSize
          Do Until Me.TextHeight(Label1.Caption) <= Label1.Height
             i = i - 1
             If i < SMALLESTFONTSIZE Then
                MsgBox "Caption height truncated to fit label - smallest font."
                Exit Sub
             End If
             Me.FontSize = i
             'Debug.Print "height" & i; Me.FontSize
          Loop
    
          ' 5. Assign Font properties from the Form back to the label:
          Label1.FontName = Me.FontName
          Label1.FontSize = Me.FontSize
    
       End Sub
    
    
If you need several different font sizes, set up a label to calibrate each font size needed. The labels used to make this adjustment do not have to be visible. Optionally, you can set the Visible property to False.

You can also have the program size label controls depending on the Screen.Height and Screen.Width properties at run time. Once you determine the correct size of the label, size the text inside the label.

Example of How Fonts May Differ on Different Hardware

You can call Windows API functions to obtain the enumerated FontSize list. This is useful to know for fixed, non-TrueType fonts. Visual Basic also offers font properties (Fonts, FontName, FontSize, and FontCount) to determine font information.

The enumerated FontSize list for non-TrueType fonts may vary from one screen resolution to another. This can happen because the number of logical pixels per inch can vary between resolutions. This means that the number of points per pixel can also vary. The point size of a font is adjusted to the nearest pixel.

The point size on a screen is based on logical inches. Logical inches are somewhat arbitrary because Windows has no way of really knowing how big a pixel is on your screen. For example, you could be hooked up to a projection TV or a tiny monitor. Usually the logical inch is overly large; tiny text is often difficult to read on a video display.

Because the point size is based on the logical pixels per inch of a device, not all point sizes can be represented. For example, on a standard VGA, Windows will tell you that the device has 96 pixels per logical inch (according to the Windows API GetDeviceCaps(hDC, LOGPIXELSY) function). A 96-pixel tall glyph is 72 points because each point is about 1/72 of an inch. This means each pixel is 72/96 point or 0.75 point per pixel. The system could theoretically represent fonts of the following heights:

   1 pixel = 0.75 point
   2 pixels = 1.50 point
   3 pixels = 2.25 point
   4 pixels = 3.00 point

Rounding errors are unavoidable in this scheme. Even if the device displayed exactly 96 dots per inch (DPI), it could not represent a font that was exactly 2 points. The closest it could come would be 2.25 points. Usually, this small difference is not noticeable. However, if two screen drivers have different logical pixels-per-inch, you might see different point sizes in the enumerated list in Windows.
Keywords          : PrgCtrlsStd vbwin GnrlVb kbprg kbfasttip
Technology        : kbvba
Version           : WINDOWS:3.00
Platform          : WINDOWS
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 29, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.