VB3 Adjust Form Size for Different Video Screen Resolutions

ID: Q103646


The information in this article applies to:
  • Microsoft Visual Basic Standard and Professional Editions for Windows, versions 2.0, 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."
  • 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%
     



MORE INFORMATION

Step-by-Step Example

  1. 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.


  2. Add two labels, one command button, and one picture control to Form1.


  3. Set Picture1's picture property to C:\WINDOWS\WINLOGO.BMP.


  4. 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
     


  5. Add the following code in the Command1 Click event procedure:
    
         Sub Command1_Click ()
             End
         End Sub 


  6. Press the F5 key to run the program. Click the Command1 button to exit from the example.


Additional query words: 3.00

Keywords : kbcode kbWndw
Version : 3.00
Platform : WINDOWS
Issue type :


Last Reviewed: June 2, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.