Carte du site
 Remerciements
 Netiquette
 Bugs
 Tables
 Requêtes
 Formulaires
 États (rapports)
 Modules
 APIs
 Chaînes
 Date/Time
 Général
 Ressources
 Téléchargeables

 Termes d'usage

APIs: Enumération des polices de caractères (fontes) du système

Author(s)
Dev Ashish

Enumération des polices de caractères (fontes) du système.

Note: Pour tester le code de cet article, vous devez avoir également le code pour AddressOf.

    Une autre utilisation possible de AddressOf consiste à énumérer les fontes du système via la fonction de l'API:  EnumFontFamilies.

    Trouvez ici une version modifiée du code disponible en exemple dans le ficher d'aide de VB. Créer une liste sur un nouveau formulaire, puis soit remplir la liste de l'énumération par un appel à  FillListWithFonts, soit appeler cette dernière depuis la procédure événementielle OnOpen du formulaire ou depuis un   OnClick d'un bouton de commande.

Call FillListWithFonts(Me!List0)

'************* Code Start **************
Private Const LF_FACESIZE = 32
Private Const LF_FULLFACESIZE = 64

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(LF_FACESIZE) As Byte
End Type

Private Type NEWTEXTMETRIC
        tmHeight As Long
        tmAscent As Long
        tmDescent As Long
        tmInternalLeading As Long
        tmExternalLeading As Long
        tmAveCharWidth As Long
        tmMaxCharWidth As Long
        tmWeight As Long
        tmOverhang As Long
        tmDigitizedAspectX As Long
        tmDigitizedAspectY As Long
        tmFirstChar As Byte
        tmLastChar As Byte
        tmDefaultChar As Byte
        tmBreakChar As Byte
        tmItalic As Byte
        tmUnderlined As Byte
        tmStruckOut As Byte
        tmPitchAndFamily As Byte
        tmCharSet As Byte
        ntmFlags As Long
        ntmSizeEM As Long
        ntmCellHeight As Long
        ntmAveWidth As Long
End Type

' ntmFlags field flags
Private Const NTM_REGULAR = &H40&
Private Const NTM_BOLD = &H20&
Private Const NTM_ITALIC = &H1&

'  tmPitchAndFamily flags
Private Const TMPF_FIXED_PITCH = &H1
Private Const TMPF_VECTOR = &H2
Private Const TMPF_DEVICE = &H8
Private Const TMPF_TRUETYPE = &H4

Private Const ELF_VERSION = 0
Private Const ELF_CULTURE_LATIN = 0

'  EnumFonts Masks
Private Const RASTER_FONTTYPE = &H1
Private Const DEVICE_FONTTYPE = &H2
Private Const TRUETYPE_FONTTYPE = &H4

Private Declare Function EnumFontFamilies Lib "gdi32" Alias _
     "EnumFontFamiliesA" _
     (ByVal hDC As Long, _
     ByVal lpszFamily As String, _
     ByVal lpEnumFontFamProc As Long, _
     LParam As Any) _
     As Long

Private Declare Function GetDC Lib "user32" _
        (ByVal hWnd As Long) _
        As Long

Private Declare Function ReleaseDC Lib "user32" _
        (ByVal hWnd As Long, _
        ByVal hDC As Long) _
        As Long

Private Declare Function apiGetFocus Lib "user32" _
        Alias "GetFocus" _
         () As Long

Function fhWnd(ctl As Control) As Long
    On Error Resume Next
    ctl.SetFocus
    If Err Then
        fhWnd = 0
    Else
        fhWnd = apiGetFocus
    End If
    On Error GoTo 0
End Function

Function EnumFontFamProc(lpNLF As LOGFONT, _
                                    lpNTM As NEWTEXTMETRIC, _
                                    ByVal FontType As Long, _
                                    LParam As Control) _
                                    As Long
Dim FaceName As String
Dim FullName As String
Dim strOut As String, strFont As String
    On Error Resume Next
    FaceName = StrConv(lpNLF.lfFaceName, vbUnicode)
    strOut = LParam.RowSource
    strFont = left$(FaceName, InStr(FaceName, vbNullChar) - 1)
    If strOut = vbNullString Then
        strOut = strFont
    Else
        strOut = strOut & ";" & strFont
    End If
    LParam.RowSource = strOut
    EnumFontFamProc = 1
End Function

Sub FillListWithFonts(ctl As Control)
Dim hDC As Long
    hDC = GetDC(fhWnd(ctl))
    ctl.RowSource = vbNullString
    EnumFontFamilies hDC, vbNullString, AddrOf("EnumFontFamProc"), ctl
    ReleaseDC fhWnd(ctl), hDC
End Sub
'************* Code End **************

© 1998-2001, Dev Ashish, All rights reserved. Optimized for Microsoft Internet Explorer