EnumFontFamiliesEx Function

Declare Function EnumFontFamiliesEx Lib "gdi32.dll" Alias "EnumFontFamiliesExA" (ByVal hdc As Long, lpLogfont As LOGFONT, ByVal lpEnumFontFamExProc As Long, ByVal lParam As Long, ByVal dwFlags As Long) As Long

Platforms

Description & Usage

EnumFontFamiliesEx enumerates all of the fonts available for use on a device which match a basic description. The function enumerates fonts based on their character sets and/or typeface styles (or simply all fonts). The function treats identical fonts with different character sets as two different fonts (for example, Times New Roman with the ANSI character set is considered a different font than Times New Roman with the Cyrillic character set). The fonts are not enumerated in any obvious order.

Return Value

The function returns whatever the final call to the callback function returned.

Visual Basic-Specific Issues

None.

Parameters

hdc
A handle to a device context to the device to enumerate the available fonts of.
lpLogfont
Identifies the character set and/or typeface name (or neither) of the fonts to enumerate. The .lfCharset member identifies the character set of the fonts to enumerate, or DEFAULT_CHARSET to ignore the character set. The .lfFaceName member identifies the typeface name of the fonts to enumerate, or an empty string to ignore the typeface name. The .lfPitchAndFamily member is not used but must be zero. All other members are ignored.
lpEnumFontFamExProc
A pointer to the EnumFontFamExProc callback function which processes the information about each font that is enumerated.
lParam
A value to pass to the function identified by lpEnumFontFamExProc.
dwFlags
Reserved -- set to 0.

Example

' This code is licensed according to the terms and conditions listed here.

' Enumerate some of the fonts available for window Form1.
' These fonts must have the ANSI character set and have the
' Times New Roman typeface.  Display some information about
' each font as it is enumerated.

' *** Place the following code in a module. ***
' The following callback function processes the enumerated fonts.
Public Function EnumFontFamExProc (ByVal lpelfe As Long, ByVal lpntme As Long, ByVal FontType As Long, ByVal lParam As Long) As Long
  Dim elfx As ENUMLOGFONTEX  ' receives information about the font
  Dim ntmx As NEWTEXTMETRICEX  ' receives text metrics for TrueType fonts
  Dim tm As TEXTMETRIC  ' receives text metrics for non-TrueType fonts
  
  ' Copy the font information into the appropriate structure.
  CopyMemory elfx, ByVal lpelfe, Len(elfx)

  ' If the font is TrueType, use the following code.
  If (FontType And TRUETYPE_FONTTYPE) = TRUETYPE_FONTTYPE Then
    ' Copy the text metrics into the appropriate structure.
    CopyMemory ntmx, ByVal lpntme, Len(ntmx)
    ' Display the name of the font (removing empty space from it).
    Debug.Print "Font Name: "; Left(elfx.elfFullName, InStr(elfx.elfFullName, vbNullChar) - 1);
    Debug.Print "  (TrueType font)"
    ' Display the style of the font (again removing empty space).
    Debug.Print "Font Style: "; Left(elfx.elfStyle, InStr(elfx.elfStyle, vbNullChar) - 1)
    ' Display the average character width.
    Debug.Print "Average Character Width:"; ntmx.ntmTm.tmAveCharWidth
    ' Display the maximum character width.
    Debug.Print "Maximum Character Width:"; ntmx.ntmTm.tmMaxCharWidth

  ' If the font is not TrueType, use the following code.
  Else
    ' Copy the text metrics into the appropriate structure.
    CopyMemory tm, ByVal lpntme, Len(tm)
    ' Display the name of the font (removing empty space from it).
    Debug.Print "Font Name: ";
    Debug.Print Left(elfx.elfLogFont.lfFaceName, InStr(elfx.elfLogFont.lfFaceName, vbNullChar) - 1);
    ' Display whether the font is a device or a raster font.
    If FontType = DEVICE_FONTTYPE Then
      Debug.Print "  (Device font)"
    ElseIf FontType = RASTER_FONTTYPE Then
      Debug.Print "  (Raster font)"
    End If
    Debug.Print "Font Style does not apply for this font."
    ' Display the average character width.
    Debug.Print "Average Character Width:"; tm.tmAveCharWidth
    ' Display the maximum character width.
    Debug.Print "Maximum Character Width:"; tm.tmMaxCharWidth
  End If

  Debug.Print "***"  ' separator
  ' Tell EnumFontFamiliesEx to continue enumeration.
  EnumFontFamExProc = 1
End Function

' *** Place this code wherever you want the enumerate the fonts. ***
Dim lf As LOGFONT  ' describes enumeration attributes
Dim retval As Long  ' return value

' Initialize the structure to describe the fonts we want.
lf.lfCharset = ANSI_CHARSET  ' fonts with the ANSI character set
lf.lfFaceName = "Times New Roman" & vbNullChar  ' fonts with the Times New Roman typeface
lf.lfPitchAndFamily = 0  ' this must be 0
' Enumerate such fonts available on window Form1.
retval = EnumFontFamiliesEx(Form1.hDC, lf, AddressOf EnumFontFamExProc, 0, 0)
Debug.Print "Enumeration complete."

See Also

EnumFontFamilies

Category

Fonts & Text

Go back to the alphabetical Function listing.
Go back to the Reference section index.


Last Modified: October 23, 1999
This page is copyright © 1999 Paul Kuliniewicz. Copyright Information Revised October 29, 2000
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/e/enumfontfamiliesex.html