Torna al Thread

Option Explicit On Option Strict On Public Class ClasseRidimensionaForm Public Enum Dimensione Verticale Orizzontale End Enum Private Shared TmpModificoFont As Boolean Private Shared TmpMettoGrassetto As Boolean Private Shared FlagEscludoMettoGrassettoSuButtonsLabels As Boolean Public Shared Sub SubRidimensiona(ByRef F As Form, ByVal Coefficiente As Int32, ByVal ModificoFont As Boolean, ByVal MettoGrassetto As Boolean, ByVal MettoGrassettoAncheSuBottoniELabel As Boolean) TmpModificoFont = ModificoFont TmpMettoGrassetto = MettoGrassetto FlagEscludoMettoGrassettoSuButtonsLabels = Not MettoGrassettoAncheSuBottoniELabel 'Si ricava il rapporto fra dimensione dello schermo e dimensione della form Dim RO As Double = FnzRapportoTraSchermoEForm(F, Dimensione.Orizzontale) Dim RV As Double = FnzRapportoTraSchermoEForm(F, Dimensione.Verticale) Dim R As Double = (RO) Dim R1 As Double = (RV) Dim K As Double = Coefficiente / 100 ' Math.Round(Double.Parse((Coefficiente / 100).ToString), 2) ' Un valore 90, ad esempio, diventa 0.9 R = R * K R1 = R1 * K If R > R1 Then R = R1 SubVariaSecondoRapporto(F, R) End Sub Public Shared Function FnzRapportoTraSchermoEForm(ByRef F As Form, ByVal D As Dimensione) As Double 'Restituisce il rapporto tra risoluzione orizzontale dello schermo e ' dimensione orizzontale della Form, oppure fa lo stesso con le componenti verticali. Dim orizz As Integer Dim vert As Integer Dim R As Double ClasseRidimensionaForm.SubRilevaRisoluzione(F, orizz, vert) 'Dal numero della chiave, se ne ottiene il nome, così come scritto nella enumerazione 'Dim Chiave As String = [Enum].GetName(GetType(ChiaviDelFileDiSetup), NumeroChiave) Select Case D Case Dimensione.Orizzontale R = orizz / F.Width Case Dimensione.Verticale R = vert / F.Height End Select Return R End Function Private Shared Sub SubRilevaRisoluzione(ByRef IlForm As Form, ByRef Orizzontale As Integer, ByRef Verticale As Integer) Dim Punto As System.Drawing.Point Dim AreaSchermo As System.Drawing.Rectangle = Screen.GetWorkingArea(Punto) Verticale = AreaSchermo.Height Orizzontale = AreaSchermo.Width End Sub Public Shared Sub SubVariaSecondoRapporto(ByRef F As Form, ByVal Rapporto As Double) Try F.Width = CInt(F.Width * Rapporto) F.Height = CInt(F.Height * Rapporto) Catch ex As Exception End Try For Each ctl As Control In F.Controls RidimensionoControlloEControlliEventualmenteContenuti(ctl, Rapporto) Next End Sub Private Shared Sub RidimensionoControlloEControlliEventualmenteContenuti(ByRef ctl As Control, ByVal Rapporto As Double) RidimensionaIlControllo(ctl, Rapporto) Dim CtlFiglio As Control For Each CtlFiglio In ctl.Controls RidimensionoControlloEControlliEventualmenteContenuti(CtlFiglio, Rapporto) Next End Sub Private Shared Sub RidimensionaIlControllo(ByRef ctl As Control, ByVal Rapporto As Double) Try ctl.Left = CInt(ctl.Left * Rapporto) ctl.Top = CInt(ctl.Top * Rapporto) ctl.Width = CInt(ctl.Width * Rapporto) ctl.Height = CInt(ctl.Height * Rapporto) If TypeOf ctl Is ListView Then Try AdattaColonneSeCiSono(ctl, Rapporto) Catch ex As Exception End Try End If Try RidimensionaFontDelControllo(ctl, Rapporto) Catch ex As Exception End Try Catch ex As Exception End Try End Sub Private Shared Sub RidimensionaFontDelControllo(ByRef Ct As Control, ByVal R As Double) 'Ridimensiona il font del controllo e, per alcuni controlli, come il listview ' provvede anche a ridimensionare le colonne 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 If TmpMettoGrassetto Then FStile = FontStyle.Bold Else If FlagEscludoMettoGrassettoSuButtonsLabels = True Then If (Not TypeOf Ct Is Button And Not TypeOf Ct Is Label) Then FStile = FontStyle.Regular Else FStile = FontStyle.Bold End If Else FStile = FontStyle.Regular End If End If If TmpModificoFont Then NuovoSize = FSize * R Else NuovoSize = FSize End If Dim NFont As New Font(FNome, CSng(NuovoSize), FStile) Ct.Font = NFont 'Qui si controlla se ci sono colonne che hanno bisogno di ridimensionamento 'AdattaColonneSeCiSono(Ct, R) Catch End Try End Sub Private Shared Sub AdattaColonneSeCiSono(ByRef ct As Control, ByVal R As Double) Dim c As ColumnHeader For Each c In CType(ct, ListView).Columns c.Width = CInt(c.Width * R) Next End Sub Public Sub New() End Sub End Class
Copyright © dotNetHell.it 2002-2025
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5