The information in this article applies to:
- Standard and Professional Editions of Microsoft Visual Basic for
Windows, versions 2.0 and 3.0
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."
- 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%
MORE INFORMATION
Step-by-Step Example
- Start Visual Basic or from the File menu, choose New Project (ALT, F, N)
if Visual Basic is already running. Form1 is created by default.
- Add two labels, one command button, and one picture control to Form1.
- Set Picture1's picture property to C:\WINDOWS\WINLOGO.BMP.
- Add the following code in the Form Load event procedure of Form1:
Sub Form_Load ()
' Set up a picture box:
Picture1.AutoSize = True
Picture1.Move 0, 0
' Set up the labels and command button:
Xtwips& = Screen.TwipsPerPixelX
Ytwips& = Screen.TwipsPerPixelY
Ypixels& = Screen.Height / Ytwips&
Xpixels& = Screen.Width / Xtwips&
label1.Caption = "Below is resolution that you are running in"
label2.Caption = Str$(Xpixels&) + " by " + Str$(Ypixels&)
label1.Width = Picture1.Width
label2.Width = Picture1.Width
label1.Left = 0
label2.Left = 0
label1.Top = Picture1.Height + 10
label2.Top = label1.Top + label1.Height + 10
command1.Top = label2.Top + label2.Height + 10
command1.Left = (Picture1.Width - command1.Width) / 2
' Size the form to fit the picture box, labels, and command button
ScaleMode = 1 ' twips
Width = Width - ScaleWidth + Picture1.Width
'VB3Line: Enter the following lines as one line
Height = Height - ScaleHeight + Picture1.Height + label1.Height _
+ label2.Height + command1.Height
End Sub
- Add the following code in the Command1 Click event procedure:
Sub Command1_Click ()
End
End Sub
- Press the F5 key to run the program. Click the Command1 button to exit
from the example.
|