Public Declare Function EnumFontFamilies Lib "gdi32" _
Alias "EnumFontFamiliesA" (ByVal hdc As Long, ByVal lpszFamily As _
String, ByVal lpEnumFontFamProc As Long, ByVal lParam As Long) As Long
Public Declare Sub CopyToPtr Lib "kernel32" Alias "RtlMoveMemory" _
(ByVal ptrDest As Long, Source As Any, ByVal Length As Long)
Public Declare Sub DeRefPtr Lib "kernel32" Alias "RtlMoveMemory" _
(Destination As Any, ByVal PtrSource As Long, ByVal Length As Long)
'font constants
Public Const LF_FULLFACESIZE = 64
Public Const LF_FACESIZE = 32
Public Const TMPF_FIXED_PITCH = &H1
'font types
Public 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(1 To LF_FACESIZE) As Byte
End Type
Public 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
Public Type ENUMLOGFONT
elfLogFont As LOGFONT
elfFullName(LF_FULLFACESIZE) As Byte
elfStyle(LF_FACESIZE) As Byte
End Type
'private array that will hold our list of fixed-width fonts
Public FixedFonts() As String
'callback procedure that windows will call for each font installed on the system
Private Function EnumFontsProc(ByVal ptrLogicalData As Long, _
ByVal ptrPhysicalData As Long, ByVal dwFontType As Long, ByVal _
ptrLpData As Long) As Long
Dim nlf As ENUMLOGFONT
Dim ntm As NEWTEXTMETRIC
'dereference the pointers (essentially)
DeRefPtr nlf, ptrLogicalData, Len(nlf)
DeRefPtr ntm, ptrPhysicalData, Len(ntm)
'if its a fixed-pitch font, add it to the array.
If Not (ntm.tmPitchAndFamily And TMPF_FIXED_PITCH) = TMPF_FIXED_PITCH Then
FixedFonts(UBound(FixedFonts)) = GetNameFromByteArray(nlf.elfLogFont.lfFaceName)
ReDim Preserve FixedFonts(UBound(FixedFonts) + 1)
End If
'if we had set false here, the api would have terminated without enumerating
'any further
EnumFontsProc = True
End Function
'converts a byte array string to a vb unicode string, and strips
'ending null character and/or garbage.
Private Function GetNameFromByteArray(src() As Byte) As String
Dim t As String
Dim zeropos As Long
t = StrConv(CStr(src), vbUnicode)
zeropos = InStr(t, Chr(0))
If zeropos > 1 Then t = Left(t, zeropos - 1)
GetNameFromByteArray = t
End Function
'public function called by the user.
Public Sub GetFixedFonts()
Dim lRet As Long
'set our array to 0 elements
ReDim FixedFonts(0)
lRet = EnumFontFamilies(frmMain.hdc, vbNullString, AddressOf EnumFontsProc, 1)
'the last element will always be blank, so knock it off the end
ReDim Preserve FixedFonts(UBound(FixedFonts) - 1)
End Sub