' les délires de patricktoulon
'centrer verticalement le texte d'un label +alignement horizontal left,center,right
Private Sub CommandButton1_Click()
VerticalLabelTextAlign Label1, aGauche:=True, margin:=True
End Sub
Private Sub CommandButton2_Click()
VerticalLabelTextAlign Label1
End Sub
Private Sub CommandButton3_Click()
VerticalLabelTextAlign Label1, aDroite:=True
End Sub
Function VerticalLabelTextAlign(lab As MSForms.Label, Optional aGauche As Boolean = False, Optional aDroite As Boolean = False, Optional margin As Boolean = False)
Dim chemin, LargeChar&, lblTemp As Object, restelarge, Nb&
chemin = ThisWorkbook.Path & "\centre.jpg"
lab.Caption = Trim(lab.Caption)
With ActiveSheet.ChartObjects.Add(0, 0, 0.05, 0.05).Chart
.Parent.ShapeRange.Line.Visible = msoFalse
.Export chemin, "jpg"
Label1.Picture = LoadPicture(chemin)
.Parent.Delete
End With
If aGauche Then lab.Caption = String(Abs(margin), " ") & Label1.Caption & String(Int(Label1.Width), " ")
If aDroite Then
Set lblTemp = lab.Parent.Controls.Add("Forms.Label.1")
' 3?? Configuration du Label temporaire
lblTemp.Width = 300 ' on le met bien large sinon on risque le wrap si le text est plus long que les 60 points automatique de large a l'insertion
Set lblTemp.Font = lab.Font ' on recupère le font et tout ses membres
lblTemp.Caption = lab.Caption ' maintenant on met le texte en entier dans le label temporaire
lblTemp.AutoSize = True 'cette fois ci oui on autosize
restelarge = lab.Width - lblTemp.Width 'récupère la diférence du width
lblTemp = "i" 'on met un caractère dans le label temporaire pas d'espace le calcul ne se fait pas sinon donc in "i"qui est un caractère pas trop large
LargeChar = lblTemp.Width 'on recupère la 2d largeur du label temporaire
Nb = restelarge / LargeChar ' on fait une simple division pour avoir le nombre de caractères
lab.Caption = String(Nb, " ") & Label1.Caption 'on applique le string(" " au nombre NB
lab.Parent.Controls.Remove lblTemp.Name 'suppression du label temporaire utilisé pour le calcul
End If
Kill chemin
End Function