Question

Locked

Resize forms and controls based on screen resolution

By armontgomery721 ·
I have a visual basic 6 application that I am trying to change the size of the forms and the controls based on the screen resolution. I have figured out how to change the form size but my problem is that the controls (textboxes, labels, listbox, etc) are staying the same size. I need to figure out a way to make them proportional to the size of the form. Here is what the code I created looks like. I put it into a separate module and I just call the subroutines form the different forms that are included in the application.

<Code>
Option Explicit

Public iHeight As Integer
Public iWidth As Integer
Public x_size As Double
Public y_size As Double
Public List() As Control
Public curr_obj As Object

Public Type RECT
x1 As Long
y1 As Long
x2 As Long
y2 As Long
End Type

Public Declare Function GetDesktopWindow Lib "User32" () As Long
Public Declare Function GetWindowRect Lib "User32" _
(ByVal hWnd As Long, rectangle As RECT) As Long

Public Sub UserForm_Activate(UserForm1 As Form)
Dim r As RECT
Dim hWnd As Long
Dim RetVal As Long
Dim ScrRes As String
Dim FtSz As Integer
Dim curScreen As Object

hWnd = GetDesktopWindow()
RetVal = GetWindowRect(hWnd, r)

ScrRes = (r.x2 - r.x1) & "X" & (r.y2 - r.y1)

'Use ScrRes variable to decide how to size the userform.

' If ScrRes = "640X480" Then
' With UserForm1
' UserForm1.Width = 7680
' UserForm1.Height = 5760
' End With
If ScrRes = "800X600" Then
With UserForm1
UserForm1.Width = 12000
UserForm1.Height = 9000
End With
ElseIf ScrRes = "1024X768" Then
With UserForm1
UserForm1.Width = 12288
UserForm1.Height = 9612
End With
ElseIf ScrRes = "1280X800" Then
With UserForm1
UserForm1.Width = 15360
UserForm1.Height = 9600
End With
ElseIf ScrRes = "1360X768" Then
With UserForm1
UserForm1.Width = 16320
UserForm1.Height = 9612
End With
ElseIf ScrRes = "1680X1050" Then
With UserForm1
UserForm1.Width = 16800
UserForm1.Height = 10500
End With
End If

' For Each curScreen In UserForm1
' ScrRes = curScreen.WorkingArea.Width.ToString + "X" + curScreen.WorkingArea.Height.ToString
'
' Select Case ScrRes
' Case "1024X768"
' End Select
' Next

End Sub

Public Function FontSize()

' Make sure x_size is greater than zero
If Int(x_size) > 0 Then
'Set the font size
FontSize = Int(x_size *
End If
End Function

Public Sub ResizeFormControls(frm As Form)

Dim i As Integer

'Set the forms height
' frm.Height = Screen.Height / 2
' 'Set the forms width
' frm.Width = Screen.Width / 2

iHeight = frm.Height
iWidth = frm.Width

'Get ratio of initial form size to current form size
x_size = frm.Height / iHeight
y_size = frm.Width / iWidth

'Loop through all the objects on the form
'Based on the upper bound of the # of controls
For i = 0 To UBound(List)
'Each control individually
For Each curr_obj In frm
'Check to make sure its the right control
If curr_obj.TabIndex = List(i).Index Then
'Resize control
With curr_obj
.Left = List(i).Left * y_size
.Width = List(i).Width * y_size
.Height = List(i).Height * x_size
.Top = List(i).Top * x_size
End With
End If
'Get next control
Next curr_obj
Next i

End Sub

</Code>

This conversation is currently closed to new comments.

2 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Answers

Collapse -

Resize Form & Controls For Screen Size - VB6

Hope this helps you out. :)

Tutorial on resizing a form based on screen size in Visual Basic 6.

* Resize a form based on screen size
* Resize the controls on the form based on the form size
* Resize the font size of all controls based on the above 2 items

CODE

Private List() As Control
Private curr_obj As Object
Private iHeight As Integer
Private iWidth As Integer
Private x_size As Double
Private y_size As Double

As you will see, the Globals are Private
The first Global is an Array or type Control. This is a private Type I have created to hold all the properties of the controls on the form. Creating your own user defined type saves a lot of headaches down the road, and allows for resizing all controls in a single loop, referencing your type:
.......................
CODE

Private Type Control
Index As Integer
Name As String
Left As Integer
Top As Integer
width As Integer
height As Integer
End Type

As you can see, this Type holds all the information needed about a control: Index, Name, Left, Top, Width and Height, these items will come in use later, when we write the procedure to resize all the controls at once. Now lets talk about resizing a form based on the current screen resolution we will be referencing the Screen Object available to us in Visual Basic 6.

The 2 properties we're concerned with are Width and Height. These properties of the Screen Object give us access to the screen size available to us. So, to set the Forms size in relationship to the Screen size, we will be accessing the properties of the Form Object, mainly the width and height properties. In my Module I set the forms size to the screen's size divided by 2, you may want to test and find your own resolution. Here's the simple procedure for resizing your form in relationship to the available screen resolution:

CODE

Public Sub ResizeForm(frm As Form)
'Set the forms height
frm.height = Screen.height / 2
'Set the forms width
frm.width = Screen.width / 2
'Resize all of the controls
'based on the forms new size
ResizeControls frm
End Sub

Simple isn't it, we change the forms size based on the screen size, then we reference a procedure called ResizeControls, we do this because resizing the form alone isn't what we're after. Well doing only this will cause some pretty ugly user interfaces, simply because you may be resizing your form, but the controls are staying the same size, which isn't a good thing.

So, you can either write a really long and ugly procedure to resize each control individually,not very maintainable, especially if you rename or add controls, or you can write a nice neat little procedure, based on a user defined type, which we have in our Code Module, and loop through them like this:

CODE

Public Sub ResizeControls(frm As Form)
Dim i As Integer
' Get ratio of initial form size to current form size
x_size = frm.height / iHeight
y_size = frm.width / iWidth

'Loop though all the objects on the form
'Based on the upper bound of the # of controls
For i = 0 To UBound(List)
'Grad each control individually
For Each curr_obj In frm
'Check to make sure its the right control
If curr_obj.TabIndex = List(i).Index Then
'Then resize the control
With curr_obj
.Left = List(i).Left * y_size
.width = List(i).width * y_size
.height = List(i).height * x_size
.Top = List(i).Top * x_size
End With
End If
'Get the next control
Next curr_obj
Next i
End Sub


Here we use the UBound Function To get the highest index on the form, which gives us the last control's index. We then loop through all the controls, stopping at the highest index, and change the size of the control. Before this procedure can actually work, we need to know the current location of each control, well I have a solution for that as well.

In this Module is a method called GetLocation. What this method does is it logs the current position of each control, looping through all the controls located in our List() Array, which is populated with the controls on the form. On each iteration of the loop, we use the ReDim Statement and the Preserve keyword to increment the size of the array by 1 and preserve the objects already in the array. The code for that method is as follows:

CODE

Public Sub GetLocation(frm As Form)
Dim i As Integer
' Load the current positions of each object into a user defined type array.
' This information will be used to rescale them in the Resize function.

'Loop through each control
For Each curr_obj In frm
'Resize the Array by 1, and preserve
'the original objects in the array
ReDim Preserve List(i)
With List(i)
.Name = curr_obj
.Index = curr_obj.TabIndex
.Left = curr_obj.Left
.Top = curr_obj.Top
.width = curr_obj.width
.height = curr_obj.height
End With
i = i + 1
Next curr_obj

' This is what the object sizes will be compared to on rescaling.
iHeight = frm.height
iWidth = frm.width
End Sub


Our Code Module is almost complete, there is but one thing we need to think about resizing, and that is the font size used in the application. Resizing, say a Label, and not resizing the font being used on the label will make for an ugly User Interface as well. So what we do is, we get the value of x_size, which is one of our Globals, and set in the ResizeControls method, we multiply that by 8 and that gives the font size I'm looking for, you needs may vary. Here is the code for resizing the font size:

CODE

Public Function SetFontSize() As Integer
'Make sure x_size is greater than 0
If Int(x_size) > 0 Then
'Set the font size
SetFontSize = Int(x_size *
End If
End Function


That's all the code you need for your form and control resizing based on the users screen resolution. It's all a matter of getting the mathematics right, then resetting the size of everything on the form to fit nicely in the current screen.

Now for how to use it, in the Form_Load Event you add a call to:

* GetLocation
* ResizeForm

And set the FontSize property of your controls, like this:

CODE

Private Sub Form_Load()
GetLocation Me
CenterForm Me
ResizeForm Me

lblInstructions.Font = SetFontSize()
End Sub


This Code Module also offers another feature. If someone were the resize your form while havin the application running, if you put a call to ResizeControls in the Form_Resize Event, this will take care of resizing everthing:

CODE

Private Sub Form_Resize()
ResizeControls Me
lblInstructions.FontSize = SetFontSize()
End Sub


I am providing the Code Module this code is in, this Module and code is under the GNU General Public License, so you can use, modify or distribute as you see fit, but the license header must stay intact. I hope you have found this tutorial informative and useful. For all your Visual Basic 6 Reference Needs go to the Visual Basic 6.0 Resource Center.
http://www.dreamincode.net/forums/showtopic34776.htm

Please post back if you have any more problems or questions.
If this information is useful, please mark as helpful. Thanks.

Collapse -

Reponse To Answer

by programmermja In reply to Resize Form & Controls Fo ...

Hello Peconet Tietokoneet, Your code worked but not that pretty. It does not work with all controls because not all controls have the TabIndex and Font property.

Controls such as Shape, Timer, Winsock, CommoDialog, WindowMedia, ect. do not have TabIndex or Font or both. Try to add up these controls and in your code and try it. looking forward for the corrections. thanks in advance.

Back to Software Forum
2 total posts (Page 1 of 1)  

Related Forums