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.
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 * 8)
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