VB3 Adjust Form Size for Different Video Screen Resolutions (103646)
The information in this article applies to:
- Microsoft Visual Basic Standard Edition for Windows 2.0
- Microsoft Visual Basic Standard Edition for Windows 3.0
- Microsoft Visual Basic Professional Edition for Windows 2.0
- Microsoft Visual Basic Professional Edition for Windows 3.0
This article was previously published under Q103646 SUMMARY
Different display devices can have different resolutions (twips per pixel
ratios). These differences can cause form and control sizes and locations
to appear differently than when they were created. Two solutions to this
problem are:
- Adjust your form and control sizes and locations at run time to match
visual elements which are not affected by the screen resolution. For
example, the sample program given below adjusts the width of the client
area of a form to match a bitmap which is a fixed number of pixels wide
and is therefore not affected by screen resolution.
In addition, the following techniques will prove useful when creating forms
that are "resolution independent."
- Design your forms for a VGA mode resolution, that is, for the "least
common denominator." Forms created at higher resolution may not size
well at smaller resolutions.
- You can make use of the TwipsPerPixelX and TwipsPerPixelY properties of
both the printer and the screen for positioning controls correctly. The
TextWidth and TextHeight methods are also handy for calculating the size
of text to be displayed. They return the size of a given piece of text
for the current scale mode of the object they are called with; that is,
form or printer--and for the current font and size. The code below
demonstrates how to calculate the size of a single character:
Dim FontHeight%, FontWidth%
FontWidth% = TextWidth ( "X" )
FontHeight% = TextHeight( "Xg" )
- Beware of resizing your form in the form's resize event. This can create
a cascading resize event, which will quickly exhaust stack space and
crash your program. The code fragment below demonstrates how to avoid
this:
Sub Form1_Resize()
If InResize% <> -1 Then
InResize% = -1
' Sample code which would trigger a resize event
form1.Width = frmWidth%
form1.Height = frmHeight%
InResize% = 0
End If
End Sub
- Make use of the form's move method to minimize calls to the resize
event. The following code issues four separate calls to the resize event
form1.Top = 0
form1.left = 0
form1.Width = 100
form1.Height = 100
as opposed to this code which triggers only one resize event:
Dim bTop%, bLeft%, bWidth%, bHeight%
bTop% = 0
bleft% = 0
bWidth% = 100
bHeight% = 100
form1.move bLeft%, bTop%, bWidth%, bHeight%
Modification Type: | Major | Last Reviewed: | 12/9/2003 |
---|
Keywords: | kbcode kbWndw KB103646 |
---|
|