Make Form Screen Resolution Independent

Posted on 1999-01-26
Last Modified: 2013-11-26
How do you make your form always proportional to the screen, regardless of the screen resolution?
Need working code, not just ideas.
Question by:maryjmiller

Expert Comment

ID: 1470742
With or without an MDI?

Expert Comment

ID: 1470743
See my previously asked question at :    (2 points)

Also, I managed to find some good code elsewhere that seems to work quite well - mail me at if you want it.



Expert Comment

ID: 1470744
There are several commercial OCXs available to do this.  They work pretty well and you avoid the eternal tweaking you need to do, to get the resizing logic correct.  Any interest in them?
LVL 12

Expert Comment

ID: 1470745
General opinion is that you should *NOT* auto resize. This robs the user of whatever new screen real estate that they gained by upping the rez. Design for your minimum and leave it at that. Give the user a break and don't just grow and eat more screen.



Expert Comment

ID: 1470746
It depends on what you're trying to do with the form... Move it or make it a certain size depending on screen res... Which?
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

LVL 10

Accepted Solution

caraf_g earned 350 total points
ID: 1470747
This is not terribly easy but it can be done.

I will give you ideas (sorry) but hopefully in such a way that you can easily convert them into working code.

OK - I'll start with the screen sizes.

First of all, the form will have to occupy the same proportion of the screen regardless of its "width" in pixels.

VB by default calculates widths in "twips" but for the purpose of keeping forms the same size, this doesn't matter.

What you'll have to do is to keep the values as private constants in the form. You'll have to decide how to keep the values, for example you could decide to keep the values as actual fractions of the screen's width

For example:
Private Const dblFormWidth As Double = 0.25
Private Const dblFormHeight As Double = .5
Private Const dblFormTop As Double = .25
Private Const dblFormLeft As Double = .3

In the above example your form would be displayed with it's top left corner at 1/4 from the top of the screen and 3/10 from the left of the screen, and the form's height would be 1/2 the height of the screen and the form's width would be 1/4 the width of the screen.

.more follows
LVL 10

Expert Comment

ID: 1470748
To set the form's co-ordinates to the correct values, regardless of the screen's resolution, you can then do the following:

Height = dblFormHeight * Screen.Height
Width = dblFormWidth * Screen.Width
Top = dblFormTop * Screen.Height
Left = dblFormLeft * Screen.Width

You could do this in the Form_Load procedure.
LVL 10

Expert Comment

ID: 1470749
Now you can do something very similar with the Height, Width, Top and Left properties of each control in the form.

For each control you would again define constants, but this time as a proportion of the control's properties with respect to the form's properties.

dblButton1Height = .25 would mean that the height of Button1 should be calculated as 1/4 of the height of the form.

Please remember that there are two height and width properties for a form.

Height and Width are the actual height and width of the form.
ScaleHeight and ScaleWidth are the height and width of the client area of the form.

This is easiest to understand if you look at the difference of Width and ScaleWidth. Width - ScaleWidth corresponds with the width of the form's borders. If you put a control on a form, and you set its Left property to 0, you'll see that the control is not flush against the left hand side of the form. There is a little gap creating the impression of a "border" around the form. The width of this "border" is calculated as
(Width - ScaleWidth) / 2

You would be best advised to calculate the relative positions and dimensions of controls with respect to the ScaleHeight and ScaleWidth values of the form.

These calculations should be done AFTER the calculations to calculate the new Height and Width of the form depending on the screen's dimensions.

LVL 10

Expert Comment

ID: 1470750
To loop through all the controls on a form:

Dim objControl As Control

For Each objControl In Controls

You could use this to process all the controls on your form.

OK... You can now resize all the controls on your form. But now comes the really hard bit.

The Font on each control will have to be resized according to the new dimensions of each control.

I'm not entirely sure how to do this, but one way of doing it may be to keep a constant in your form which corresponds with the original height of the form (as you designed it).

You can then calculate the proportion of the form's new height of the form's old height

dblProportion = Height / DesignedHeight

You may then get away with something like

Dim objControl As Control
Dim objFont As Font

For Each objControl In Controls
    Set objFont = objControl.Font
    objFont.Size = objFont.Size * dblProportion

OK - Hope this gives you an idea of all the issues involved with what you're trying to achieve.

To be honest, I agree with mark2150. You're really better off not doing this. But if you must, I hope my ideas help.
LVL 14

Expert Comment

ID: 1470751
Use this class (I have also other class to do this easily):

' Name: class_Resize
' Description:Automatically resizes all controls on a form to provide resolution independence.
' By: Michael D. Long
' Inputs:Requires that the class be declared on each form, and the .Resize method to be called from each forms Resize event.
' Returns:None
' Assumes:All controls on a managed form will be proportionally scale when the form is resized. Limitations: no font sizing support, and the ComboBox Height property does not resize.
' Side Effects:None

'The class_Resize class proportionally scales all controls on a form.
'This is used to allow automatic adjustment for different screen
' resolutions. Also, implementation in code reduces resource
' consumption and distribution requirements.
'To implement, simply add a declaration to each form that you
'want resized and a call to Resize method in each Form_Resize
' event.
' ------------------------------------
' Code to add to each form
' Private m_class_Resize As New class_Resize
' Private Sub Form_Resize()
'    m_class_Resize.Resize Me
' End Sub
' ------------------------------------
' Class class_ObjectSize - member variable declarations only
'  Object size properties
'Public Height     As Long
'Public Left       As Long
'Public Top        As Long
'Public Width      As Long

' ------------------------------------
' Class class_Resize - declarations and control resizing code
Option Explicit
Private m_cSize         As New Collection ' Collection of size properties
Private m_iNbrControls  As Integer ' Previous number of controls on form
Private m_nFormHeight   As Long ' Original form height
Private m_nFormWidth    As Long ' Original form width

Private Sub Class_Initialize()
   m_nFormHeight = 0
   m_nFormWidth = 0
   m_iNbrControls = 0

End Sub

Public Sub Resize(ByRef frmToResize As Form)
   ' Expose the method to perform scaling of the controls on the form
   Dim cControls     As class_ObjectSize
   Dim dblScaleH     As Double
   Dim dblScaleW     As Double
   Dim iCnt          As Integer
   Dim iItm          As Integer
   Dim iNbrControls  As Integer
   On Error Resume Next
   iNbrControls = frmToResize.Controls.Count
   If iNbrControls <> m_iNbrControls Then
      ' Save off the original controls size properties
      For iCnt = m_iNbrControls To iNbrControls - 1
         Set cControls = New class_ObjectSize
         cControls.Height = frmToResize.Controls(iCnt).Height
         cControls.Left = frmToResize.Controls(iCnt).Left
         cControls.Top = frmToResize.Controls(iCnt).Top
         cControls.Width = frmToResize.Controls(iCnt).Width
         m_cSize.Add cControls
      m_iNbrControls = iNbrControls
   End If
   ' Check to see if the form has just been loaded; we only
   ' need to resize the controls on subsequent events.
   If (m_nFormHeight <> 0 And m_nFormWidth <> 0) Then
      ' Calculate the scaling factor; doubles used for precision
      dblScaleH = frmToResize.ScaleHeight / m_nFormHeight
      dblScaleW = frmToResize.ScaleWidth / m_nFormWidth
      ' Loop through the controls and resize
      For iCnt = 0 To iNbrControls - 1
         iItm = iCnt + 1 ' Compensate for collection being 1 based
         If TypeOf frmToResize.Controls(iCnt) Is Timer Then
            ' do nothing
         ElseIf TypeOf frmToResize.Controls(iCnt) Is ComboBox Then
            ' attempting to resize height results in error
            frmToResize.Controls(iCnt).Move _
            m_cSize.Item(iItm).Left * dblScaleW, _
            m_cSize.Item(iItm).Top * dblScaleH, _
            m_cSize.Item(iItm).Width * dblScaleW
            frmToResize.Controls(iCnt).Move _
            m_cSize.Item(iItm).Left * dblScaleW, _
            m_cSize.Item(iItm).Top * dblScaleH, _
            m_cSize.Item(iItm).Width * dblScaleW, _
            m_cSize.Item(iItm).Height * dblScaleH
         End If
      Next iCnt
      ' Store original form height and width; used for scale calcs
      m_nFormHeight = frmToResize.Height
      m_nFormWidth = frmToResize.Width
   End If
End Sub


Author Comment

ID: 1470752
Thanks to all. I will digest them and then award points to those who made contribution

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now