Imports System.Drawing
Imports System.Convert
Imports System.Windows.Forms
Public Class clsResize
Implements IDisposable
#Region " Declare private vars "
Private blnDisposed As Boolean
Private objFormSize As Size
Private colControls As New Collection()
Private Structure Properties
Dim Left As Integer
Dim Top As Integer
Dim Width As Integer
Dim Height As Integer
Dim FontSize As Single
End Structure
#End Region
#Region " Initialize and dispose "
Public Sub New(ByRef frm As Form)
' initialize the resize class by collecting control properties
Dim objControl As Control
Dim stuCtrlProp As Properties
' store original form size
objFormSize = frm.Size
' for controls who don't support all the properties resume on error
On Error Resume Next
For Each objControl In frm.Controls
With stuCtrlProp
.Left = objControl.Left
.Top = objControl.Top
.Width = objControl.Width
.Height = objControl.Height
.FontSize = objControl.Font.Size
End With
colControls.Add(stuCtrlProp, objControl.Name)
Next
' clean up
objControl = Nothing
' we made a new instance so set blnDisposed to false
blnDisposed = False
End Sub
Public Sub Dispose() Implements IDisposable.Dispose
' clean up objects
If Not blnDisposed Then
objFormSize = Nothing
colControls = Nothing
blnDisposed = True
End If
End Sub
#End Region
#Region " Resize the from "
Public Sub Resize(ByRef frm As Form)
Dim dblRatioX As Double
Dim dblRatioY As Double
Dim intWinMax As Integer
Dim objControl As Control
Dim stuCtrlProp As Properties
' setup scaling ratios
intWinMax = 40
dblRatioY = 1.0# * (objFormSize.Height - intWinMax) / (frm.Height - intWinMax)
dblRatioX = 1.0# * objFormSize.Width / frm.Width
' for comboboxes, timeres and other nonsizible controls resume on error next
On Error Resume Next
' reposition and resize the controls
For Each objControl In frm.Controls
stuCtrlProp = CType(colControls.Item(objControl.Name), Properties)
With stuCtrlProp
objControl.SetBounds(ToInt32(.Left / dblRatioX), ToInt32(.Top / dblRatioY), ToInt32(.Width / dblRatioX), ToInt32(.Height / dblRatioY))
objControl.Font = New Font(objControl.Font.Name, ToSingle(.FontSize / dblRatioX) + ToSingle(.FontSize / dblRatioX) Mod 2)
End With
Next
' clean up
objControl = Nothing
End Sub
#End Region
End Class