VBA Tipp: Liste aller Schriftarten

Aus DBWiki
Wechseln zu: Navigation, Suche

Problem

Ich möchte in Access 2000 alle Schriften in einem Listen- oder Kombinationsfeld zur Auswahl stellen. Das Beispiel, das ich gefunden habe, verwendet aber merkwürdige Methoden bzw. Eigenschaften wie Clear oder hWnd, die mein Listenfeld nicht hat.

Lösung

Das Beispiel in der Hilfe bezieht sich auf VB und nicht auf VBA, das Access-Listenfeld kennt keine hWnd-Eigenschaft oder Clear-Methode.

In Access 2000 geht das so:

'Font enumeration types
Public Const LF_FACESIZE = 32
Public Const LF_FULLFACESIZE = 64
 
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
 
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
Public Const NTM_REGULAR = &H40&
Public Const NTM_BOLD = &H20&
Public Const NTM_ITALIC = &H1&
 
'  tmPitchAndFamily flags
Public Const TMPF_FIXED_PITCH = &H1
Public Const TMPF_VECTOR = &H2
Public Const TMPF_DEVICE = &H8
Public Const TMPF_TRUETYPE = &H4
 
Public Const ELF_VERSION = 0
Public Const ELF_CULTURE_LATIN = 0
 
'  EnumFonts Masks
Public Const RASTER_FONTTYPE = &H1
Public Const DEVICE_FONTTYPE = &H2
Public Const TRUETYPE_FONTTYPE = &H4
 
Declare Function EnumFontFamilies Lib "gdi32" Alias "EnumFontFamiliesA" _
     (ByVal hDC As Long, ByVal lpszFamily As String, _
     ByVal lpEnumFontFamProc As Long, LParam As Any) As Long
 
Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
 
Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, _
     ByVal hDC As Long) As Long
 
Private Res As String
 
Private Function EnumFontFamProc(lpNLF As LOGFONT, lpNTM As NEWTEXTMETRIC, _
     ByVal FontType As Long, LParam As ListBox) As Long
Dim FaceName As String, FullName As String
  FaceName = StrConv(lpNLF.lfFaceName, vbUnicode)
  Res = Res & ";" & left$(FaceName, InStr(FaceName, vbNullChar) - 1)
  EnumFontFamProc = 1
End Function
 
Public Function FillListWithFonts()
Dim hDC As Long, hWnd As Long
  Res = ""
  hWnd = Application.hWndAccessApp
  hDC = GetDC(hWnd)
  EnumFontFamilies hDC, vbNullString, AddressOf EnumFontFamProc, 0
  ReleaseDC hWnd, hDC
  FillListWithFonts = Mid(Res, 2)
End Function

Die Funktion FillListWithFonts kann man dann z.B. für die RowSource (Datensatzherkunft) eines Listenfelds verwenden Herkunftstyp auf "Wertliste" einstellen!)

Private Sub Form_Load()
  Me!MeinListenfeld.RowSource = FillListWithFonts()
End Sub