• Status: Solved
• Priority: Medium
• Security: Public
• Views: 374

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.
0
maryjmiller
1 Solution

Commented:
With or without an MDI?
0

Commented:
See my previously asked question at :
http://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

0

Commented:
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?
0

Commented:
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

0

Commented:
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?
0

Commented:
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.

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
0

Commented:
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.
0

Commented:
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
0

Commented:
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.
0

Commented:
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
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

0

Author Commented:
Thanks to all. I will digest them and then award points to those who made contribution
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.