Microsoft KB Archive/138799

{| = How to Adjust Form Size for Different Video Screen Resolutions =
 * width="100%"|

Last reviewed: October 30, 1995

Article ID: Q138799 The information in this article applies to:
 * Professional and Enterprise Editions of Microsoft Visual Basic, 16-bit only, for Windows, version 4.0

SUMMARY
Different display devices can have different resolutions (twips per pixel ratios). These differences can cause form and control sizes and locations to look different from the way they looked than when they were created. This article recommends solutions and gives examples.

MORE INFORMATION
Two solutions to this situation are:


 * Set the ScaleMode on all forms and picture boxes to Pixels (3). This unit of measurement does not depend on screen resolution, so forms and controls will always appear the same size and location relative to each other.
 * Adjust your form and control sizes and locations at run time to match the visual elements unaffected by the screen resolution. For example, the sample program given in this article adjusts the width of the client area of a form to match a bitmap that is a fixed number of pixels wide and is therefore not affected by screen resolution.

In addition, the following techniques will prove useful when you create 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.  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 -- form or printer -- and for the current font and size. The following code 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 cause your program to quit. The following code fragment demonstrates how to avoid this: Private Sub Form1_Resize

if InResize% <> -1 then InResize% = -1

' Sample code that 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 Whereas the following code triggers only a single resize event: Dim bTop%, bLeft%, bWidth%, bHeight% bTop% = 0 bleft% = 0 bWidth% = 100 bHeight% = 100 form1.move bLeft%, bTop%, bWidth%, bHeight% 

Step-by-Step Example
 Start a new project in Visual Basic. 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\Xxxxx.bmp.</li>  Add the following code in the Form1 Load event procedure: Private 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

' Enter the Height statement as one, single line: Height = Height - ScaleHeight + Picture1.Height + label1.Height + label2.Height + command1.Height End Sub </li>  Add the following code to the Command1 Click event procedure: Private Sub Command1_Click End End Sub </li> Press the F5 key to run the program. Click the Command1 button to exit from the example.</li></ol>
 * }