VB3 Adjust Form Size for Different Video Screen Resolutions

ID: Q103646


The information in this article applies to:


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:

In addition, the following techniques will prove useful when creating forms that are "resolution independent."


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