Versions : 2000 et supérieures
Voici un moyen de permettre à l'utilisateur de sélectionner une police. Cet exemple utilise l'API Windows ChooseFont qui affiche la boîte de dialogue commune Windows intitulée Sélectionner une police.
Dans un module :
Public Type Police
Nom As String
Taille As Long
Souligne As Boolean
Italique As Boolean
Gras As Boolean
Barre As Boolean
Couleur As Long
End Type
Const LOGPIXELSY = 90
Const FW_NORMAL = 400
Const FW_GRAS = 700
Const DEFAULT_CHARSET = 1
Const OUT_DEFAULT_PRECIS = 0
Const CLIP_DEFAULT_PRECIS = 0
Const DEFAULT_QUALITY = 0
Const DEFAULT_PITCH = 0
Const FF_ROMAN = 16
Const CF_PRINTERFONTS = &H2
Const CF_SCREENFONTS = &H1
Const CF_BOTH = (CF_SCREENFONTS Or CF_PRINTERFONTS)
Const CF_EFFECTS = &H100&
Const CF_FORCEFONTEXIST = &H10000
Const CF_INITTOLOGFONTSTRUCT = &H40&
Const CF_LIMITSIZE = &H2000&
Const CF_NOSCRIPTSEL = &H800000
Const REGULAR_FONTTYPE = &H400
Const LF_FACESIZE = 32
Const GMEM_MOVEABLE = &H2
Const GMEM_ZEROINIT = &H40
Private Type CHOOSEFONT
lStructSize As Long
hwndOwner As Long
hDC As Long
lpLogFont As Long
iPointSize As Long
flags As Long
rgbColors As Long
lCustData As Long
lpfnHook As Long
lpTemplateName As String
hInstance As Long
lpszStyle As String
nFontType As Integer
MISSING_ALIGNMENT As Integer
nSizeMin As Long
nSizeMax As Long
End Type
Private Type LOGFONT
lfHeight As Long
lfWidth As Long
lfEscapement As Long
lfOrientation As Long
lfWeight As Long
lfItalic As Byte
lfUnderline As Byte
lfStrikeOut As Byte
lfCharSet As Byte
lfOutPrecision As Byte
lfClipPrecision As Byte
lfQuality As Byte
lfPitchAndFamily As Byte
lfFaceName As String * 31
End Type
Private Declare Function CHOOSEFONT Lib "comdlg32.dll" _
Alias "ChooseFontA" (pChoosefont As CHOOSEFONT) As Long
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, _
ByVal nIndex As Long) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias _
"RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
Private Declare Function GlobalLock Lib "kernel32" _
(ByVal hMem As Long) As Long
Private Declare Function GlobalUnlock Lib "kernel32" _
(ByVal hMem As Long) As Long
Private Declare Function GlobalAlloc Lib "kernel32" _
(ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalFree Lib "kernel32" _
(ByVal hMem As Long) As Long
Public Function ChoisirPolice(Handle As Long, _
PoliceParDefaut As Police) As Police
Dim Boite As CHOOSEFONT, LaPolice As LOGFONT, _
hMem As Long, pMem As Long
Dim resultat As Long, Retour As Police
LaPolice.lfStrikeOut = PoliceParDefaut.Barre
LaPolice.lfWeight = IIf(PoliceParDefaut.Gras, _
FW_GRAS, FW_NORMAL)
LaPolice.lfItalic = PoliceParDefaut.Italique
LaPolice.lfUnderline = PoliceParDefaut.Souligne
LaPolice.lfHeight = -PoliceParDefaut.Taille * _
GetDeviceCaps(GetDC(Handle), LOGPIXELSY) / 72
If PoliceParDefaut.Nom = "" Then _
PoliceParDefaut.Nom = "Tahoma"
LaPolice.lfFaceName = PoliceParDefaut.Nom & _
vbNullChar
hMem = GlobalAlloc(GMEM_MOVEABLE _
Or GMEM_ZEROINIT, Len(LaPolice))
pMem = GlobalLock(hMem)
CopyMemory ByVal pMem, LaPolice, Len(LaPolice)
Boite.lStructSize = Len(Boite)
Boite.hwndOwner = Handle
Boite.lpLogFont = pMem
Boite.iPointSize = 120
Boite.flags = CF_BOTH Or CF_EFFECTS Or CF_FORCEFONTEXIST Or _
CF_INITTOLOGFONTSTRUCT Or CF_LIMITSIZE Or _
CF_NOSCRIPTSEL
Boite.rgbColors = PoliceParDefaut.Couleur
Boite.nFontType = REGULAR_FONTTYPE
Boite.nSizeMin = 6
Boite.nSizeMax = 72
resultat = CHOOSEFONT(Boite)
If resultat <> 0 Then
CopyMemory LaPolice, ByVal pMem, Len(LaPolice)
Retour.Nom = Left(LaPolice.lfFaceName, _
InStr(LaPolice.lfFaceName, vbNullChar) - 1)
Retour.Taille = Boite.iPointSize \ 10
Retour.Couleur = Boite.rgbColors
Retour.Gras = LaPolice.lfWeight > FW_NORMAL
Retour.Italique = LaPolice.lfItalic
Retour.Souligne = LaPolice.lfUnderline
Retour.Barre = LaPolice.lfStrikeOut
End If
resultat = GlobalUnlock(hMem)
resultat = GlobalFree(hMem)
ChoisirPolice = Retour
End Function
Puis pour changer la police d'un contrôle, appliquer le code suivant sur un bouton :
Dim P As Police
With P
.Barre = False
.Couleur = Lbl_Texte.ForeColor
.Gras = Lbl_Texte.FontBold
.Italique = Lbl_Texte.FontItalic
.Souligne = Lbl_Texte.FontUnderline
.Taille = Lbl_Texte.FontSize
.Nom = Lbl_Texte.fontname
End With
P = ChoisirPolice(Me.Hwnd, P)
With Lbl_Texte
If P.Taille <> 0 Then
.ForeColor = P.Couleur
.FontBold = P.Gras
.FontItalic = P.Italique
.fontname = P.Nom
.FontUnderline = P.Souligne
.FontSize = P.Taille
End If
End With
Le principe est simple, il suffit de créer un type Police contenant les informations actuelles du contrôle puis d'appeler la boîte de dialogue avec cette police par défaut. Enfin, il faut appliquer le résultat reçu de la boîte de dialogue au contrôle en question.
Pour plus d'informations, n'hésitez pas à télecharger le fichier source zippé.
|