Link to home
Start Free TrialLog in
Avatar of maryjmiller
maryjmiller

asked on

Make Form Screen Resolution Independent

How do you make your form always proportional to the screen, regardless of the screen resolution?
Need working code, not just ideas.
Thanx.
Avatar of ZenMaster
ZenMaster

With or without an MDI?
See my previously asked question at :
https://www.experts-exchange.com/topics/comp/lang/visualbasic/Q.10120847    (2 points)

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

Regards
Dave

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

M

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?
ASKER CERTIFIED SOLUTION
Avatar of caraf_g
caraf_g

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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.

.more
To loop through all the controls on a form:

Dim objControl As Control

For Each objControl In Controls
Next

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
Next

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.
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
      Next
     
      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
         Else
            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
     
   Else
      ' Store original form height and width; used for scale calcs
      '
      m_nFormHeight = frmToResize.Height
      m_nFormWidth = frmToResize.Width
   End If
End Sub


Avatar of maryjmiller

ASKER

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