Results 1 to 3 of 3

Thread: Fonts and API

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2002
    Posts
    23

    Question Fonts and API

    Howdy all,

    There is all sorts of postings about listing screen/printer fonts installed on your computer using the:

    For i = 0 To Printer.FontCount - 1
    lvFontList.ListItems.Add(i + 1, , Printer.Fonts(i)) 'populates a listView box...
    Next i

    ... type of syntax.

    However, I want to get extended information such as Font Family, the actual font file name, Font Style and where the font lives on the system. Do I need to use an API function? Do I simply loop through the Windows/Font directory and grab file attributes from each file?

    What would you do? I appreciate your help. I have come across CreateFont function and EnumFontFamilies function, but I do not know how to use them, let alone use them to populate a list box.

    Thanks.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Originally posted by fntzlnd
    I want to get extended information such as Font Family, the actual font file name, Font Style and where the font lives on the system.
    What do you mean by "Font Family" and "where the font lives on the system"?

  3. #3
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930

    EnumFontFamilies[Ex]

    This will show you how to fill a listbox with some font names using EnumFontFamilies[Ex]. The form you mentioned is kept for compatibility with older forms of windows, but either that or the Ex form is fine. This is a bit lengthy looking, but it really isn't very hard to work with.

    VB Code:
    1. 'in a module (since the API requires a callback)
    2. Option Explicit
    3.  
    4. Public Declare Function EnumFontFamiliesEx Lib "gdi32" Alias "EnumFontFamiliesExA" (ByVal hDC As Long, lpLogFont As LOGFONT, ByVal lpEnumFontProc As Long, ByVal lParam As Long, ByVal dwReserved As Long) As Long
    5. Public Declare Function SendMessageString Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
    6.  
    7. Public Const LB_ADDSTRING As Long = &H180
    8. Public Const LF_FACESIZE As Long = 32
    9. Public Const LF_FULLFACESIZE As Long = 64
    10. Public Const ANSI_CHARSET As Long = 0
    11.  
    12. Public Type NEWTEXTMETRIC
    13.    tmHeight As Long
    14.    tmAscent As Long
    15.    tmDescent As Long
    16.    tmInternalLeading As Long
    17.    tmExternalLeading As Long
    18.    tmAveCharWidth As Long
    19.    tmMaxCharWidth As Long
    20.    tmWeight As Long
    21.    tmOverhang As Long
    22.    tmDigitizedAspectX As Long
    23.    tmDigitizedAspectY As Long
    24.    tmFirstChar As Byte
    25.    tmLastChar As Byte
    26.    tmDefaultChar As Byte
    27.    tmBreakChar As Byte
    28.    tmItalic As Byte
    29.    tmUnderlined As Byte
    30.    tmStruckOut As Byte
    31.    tmPitchAndFamily As Byte
    32.    tmCharSet As Byte
    33.    ntmFlags As Long
    34.    ntmSizeEM As Long
    35.    ntmCellHeight As Long
    36.    ntmAveWidth As Long
    37. End Type
    38.  
    39. Public Type FONTSIGNATURE
    40.    fsUsb(4) As Long
    41.    fsCsb(2) As Long
    42. End Type
    43.  
    44. Public Type NEWTEXTMETRICEX
    45.    ntmTm As NEWTEXTMETRIC
    46.    ntmFontSig As FONTSIGNATURE
    47. End Type
    48.  
    49. Public Type LOGFONT
    50.    lfHeight As Long
    51.    lfWidth As Long
    52.    lfEscapement As Long
    53.    lfOrientation As Long
    54.    lfWeight As Long
    55.    lfItalic As Byte
    56.    lfUnderline As Byte
    57.    lfStrikeOut As Byte
    58.    lfCharSet As Byte
    59.    lfOutPrecision As Byte
    60.    lfClipPrecision As Byte
    61.    lfQuality As Byte
    62.    lfPitchAndFamily As Byte
    63.    lfFaceName(0 To (LF_FACESIZE - 1)) As Byte
    64. End Type
    65.  
    66. Public Type ENUMLOGFONTEX
    67.    elfLogFont As LOGFONT
    68.    elfFullName(0 To (LF_FULLFACESIZE - 1)) As Byte
    69.    elfStyle(0 To (LF_FACESIZE - 1)) As Byte
    70.    elfScript(0 To (LF_FACESIZE - 1)) As Byte
    71. End Type
    72.  
    73. Public Function EnumFontFamiliesExProc(ByRef lpelfe As ENUMLOGFONTEX, ByRef lpntme As NEWTEXTMETRICEX, ByVal FontType As Long, ByVal lParam As Long) As Long
    74.    Dim strFontName As String
    75.    'convert the font name into a unicode string
    76.    strFontName = StrConv(lpelfe.elfLogFont.lfFaceName, vbUnicode)
    77.    'trim off the null terminator
    78.    strFontName = Left$(strFontName, InStr(strFontName, vbNullChar) - 1)
    79.    'add the name to the listbox (lParam is the hWnd passed in)
    80.    SendMessageString lParam, LB_ADDSTRING, 0, strFontName
    81.    'return 1 to continue enumeration
    82.    EnumFontFamiliesExProc = 1
    83. End Function
    84.  
    85. 'in a form with a listbox (or wherever you want to start filling the listbox
    86. Private Sub Form_Load()
    87.    Dim lf As LOGFONT
    88.    'this will enumerate all fonts in the ANSI character set
    89.    lf.lfCharSet = ANSI_CHARSET
    90.    lf.lfFaceName(0) = 0
    91.    lf.lfPitchAndFamily = 0
    92.    'enumerate the fonts
    93.    EnumFontFamiliesEx Me.hDC, lf, AddressOf EnumFontFamiliesExProc, List1.hWnd, 0
    94. End Sub

    I'm not sure about how to actually retrieve what font family a given font resides in, but if you use CreateFont()
    or CreateFontIndirect() and only specify the font's name, you may be able to use the API GetObject() on that font handle
    again with a LOGFONT struct and then look at the lf.lfPitchAndFamily member. The system should have
    changed it to reflect what family the given font name is a part of and what pitch it has. This is a bit roundabout though,
    so there could be an easier way I'm just not seeing (I'm prone to doing things the hard way without much thought,
    and easily miss the obvious ). I have no clue on how to find out where the font resides though.
    I'm baaaack...
    VB5 Professional Edition, VC++ 6
    Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se

    I feel special because I finally figured out how to loop midis: Post link
    I'm a fanatic too

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width