Resize form, controls, fonts

At bottom of the form load event put ResizeFormClass.SubResize(Me, percentW, percentH) where percentW is ratio form.width/Int((Screen.PrimaryScreen.WorkingArea.Width), and percentH is ratio form.height/Int((Screen.PrimaryScreen.WorkingArea.Height). If form is maximized, below ResizeFormClass.SubResize(Me, percentW, percentH) put Me.WindowState = FormWindowState.Maximized, at runtime.
At the resolution 1 form resizes and the form width becomes: Int((Screen.PrimaryScreen.WorkingArea(1).Width) * (percentW / 100)), while the form height becomes: Int((Screen.PrimaryScreen.WorkingArea(1).Height) * (percentH / 100)).
When I run the application, at resolution 2, the form width becomes: Int((Screen.PrimaryScreen.WorkingArea(2).Width* (percentW / 100) and the form height becomes: Int((Screen.PrimaryScreen.WorkingArea(2).Height) * (percentH / 100)).
The form resizes not exactly proportionally because Int((Screen.PrimaryScreen.WorkingArea(1).Width) * (percentW / 100))/Int((Screen.PrimaryScreen.WorkingArea(2).Width* (percentW / 100))<>Int((Screen.PrimaryScreen.WorkingArea(1).Height) * (percentH / 100))/Int((Screen.PrimaryScreen.WorkingArea(2).Height) * (percentH / 100)),
but the controls and the fonts resize proportionally.
With this method we resize the form better than other methods because we can set both width and height of the form and because with percentW=100 and percentH=100 the form fills all the working area in the maximized mode and we can set the WindowState property=Maximized without the risk to "lose" any control.
On the other hand, going from the resolution 1280x1024 to the resolution 1024x768 FormHeight1/FormHeight2= Int((Screen.PrimaryScreen.WorkingArea(1).Height) * (percentH / 100))/ Int((Screen.PrimaryScreen.WorkingArea(2).Height) * (percentH / 100))=1.347 and FormWidth1/FormWidth2= Int((Screen.PrimaryScreen.WorkingArea(1).Width) * (percentW / 100))/Int((Screen.PrimaryScreen.WorkingArea(2).Width* (percentW / 100))=1.25. If we make the ratio 1.347/1.25 we find 1.0776. That is the form stretch out about by 7.76%, that isn't so much!
Going form 1280x1024 resolution to 800x600 resolution we have similarly to make the ratio 1.74/1.6=1.0875, that is the form stretch out about by 8.75%.
If we go form 1024x768 resolution to 800x600 resolution similarly we have to make the ratio 1.29/1.28, that is the form stretch out by 0.78%!
In a module paste code below:

Module Module1

Public Class ResizeFormClass
'Original form width.
Private Shared m_FormWidth As Long
Private Shared m_FormHeight As Long



Public Shared Sub SubResize(ByVal F As Form, ByVal percentW As Double, ByVal percentH As Double)
Dim FormHeight As Long
Dim FormWidth As Long
Dim HeightChange As Double, WidthChange As Double



Call SaveInitialStates(F)




'Calculate the new height and width the form needs to be resized to, based on the current avaible screen area.
FormHeight = Int((Screen.PrimaryScreen.WorkingArea.Height) * (percentH / 100))
FormWidth = Int((Screen.PrimaryScreen.WorkingArea.Width) * (percentW / 100))



'Use the Form that is to be resized.
With F
'Change the demensions and position of the form.

.Height = FormHeight
.Width = FormWidth

HeightChange = .ClientSize.Height / m_FormHeight
WidthChange = .ClientSize.Width / m_FormWidth

End With
'Calculate ratio current avaible screen area/form size

'Notify the class that the form has been resized.
SubChangeWithRatio(F, WidthChange, HeightChange)

End Sub

Private Shared Sub SaveInitialStates(ByVal F As Form)


'Use the form that is being resized.
With F
'Check if the form is a MDI form.

'Set the FormWidth and FormHeight variables to the Form's Scale Width and Height.
m_FormWidth = .ClientSize.Width
m_FormHeight = .ClientSize.Height

End With

End Sub


Public Shared Sub SubChangeWithRatio(ByVal F As Form, ByVal RapportoW As Single, ByVal RapportoH As Single)
'uses a recursive routine
For Each ctl As Control In F.Controls
ResizeControlAndIncludedControls(ctl, RapportoW, RapportoH)
Next

End Sub

Private Shared Sub ResizeControlAndIncludedControls(ByRef ctl As Control, ByVal RapportoW As Single, ByVal RapportoH As Single)



Dim ChildCtl As Control

For Each ChildCtl In ctl.Controls

ResizeControlAndIncludedControls(ChildCtl, RapportoW, RapportoH)

Next
ResizeControl(ctl, RapportoW, RapportoH)
End Sub

Private Shared Sub ResizeControl(ByRef ctl As Control, ByVal RapportoW As Single, ByVal RapportoH As Single)
Dim lb As New ListBox, intlH As Boolean
Try
If TypeOf ctl Is ListBox Then


lb = CType(ctl, ListBox)
intlH = lb.IntegralHeight
lb.IntegralHeight = False

ctl.Left = CInt(ctl.Left * RapportoW)
ctl.Top = CInt(ctl.Top * RapportoH)
ctl.Width = CInt(ctl.Width * RapportoW)
ctl.Height = CInt(ctl.Height * RapportoH)

Else

ctl.Left = CInt(ctl.Left * RapportoW)
ctl.Top = CInt(ctl.Top * RapportoH)
ctl.Width = CInt(ctl.Width * RapportoW)
ctl.Height = CInt(ctl.Height * RapportoH)

End If

lb.IntegralHeight = intlH
If TypeOf ctl Is ListView Then
Try
ResizeColumns(ctl, RapportoW, RapportoH)
Catch ex As Exception
End Try
End If
Try
ResizeControlFont(ctl, RapportoW, RapportoH)

Catch ex As Exception
End Try
Catch ex As Exception
End Try

End Sub

Private Shared Sub ResizeControlFont(ByRef Ct As Control, ByVal RapportoW As Single, ByVal RapportoH As Single)

'Resizes the control font and, in the case of some controls, as the listview
' resizes the columns also

Try

Dim FSize As Double = Ct.Font.Size
Dim FStile As FontStyle = Ct.Font.Style
Dim FNome As String = Ct.Font.Name
Dim NuovoSize As Double = FSize



NuovoSize = FSize * Math.Sqrt(RapportoW * RapportoH)
Dim NFont As New Font(FNome, CSng(NuovoSize), FStile)
Ct.Font = NFont

Catch

End Try

End Sub

Private Shared Sub ResizeColumns(ByRef ct As Control, ByVal RapportoW As Single, ByVal RapportoH As Single)

Dim c As ColumnHeader
For Each c In CType(ct, ListView).Columns
c.Width = CInt(c.Width * RapportoW)
Next

End Sub
End Class
End Module

0 comments:

Newer Post Older Post