How to Adjust FontSize at Run Time for Different Video DriversID: Q138801
|
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 automatically adjust text at run time to fit the design- time label size, independent of the video driver.
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.
Private 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
1 pixel = 0.75 point
2 pixels = 1.50 point
3 pixels = 2.25 point
4 pixels = 3.00 point
Additional query words: 4.00 vb4win vb416
Keywords :
Version :
Platform :
Issue type :
Last Reviewed: June 17, 1999