Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 14,206 times

Related Categories

Loading Fonts into Comboboxes via API

Here's an alternative method to your everyday:

For I = 1 To Printer.FontCount - 1
       Combo.AddItem Printer.Fonts(I)
Next

This method uses the EnumFontFamilies API function. Its simply faster than inserting one by one each font on a users system. This itself may decrease loading times on your application by 2/3z.

So here's the code you will need. First, insert this to a module:

'//= START MODULE =\\
Private Const LF_FACESIZE = 32
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
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

Public Sub AddFonts(Combo As ComboBox)
   Dim hdc As Long
   Combo.Clear
   hdc = GetDC(Combo.hwnd)
   EnumFontFamilies hdc, vbNullString, AddressOf EnumFontFamProc, Combo
   ReleaseDC Combo.hwnd, hdc
End Sub
Private Function EnumFontFamProc(lpNLF As LOGFONT, lpNTM As NEWTEXTMETRIC, ByVal FontType As Long, lParam As ComboBox) As Long
   Dim FaceName As String
   FaceName = StrConv(lpNLF.lfFaceName, vbUnicode)
   lParam.AddItem Left$(FaceName, InStr(FaceName, vbNullChar) - 1)
   EnumFontFamProc = 1
End Function

'//= END MODULE =\\

Now lets insert a combo box into our form and call the AddFonts sub to add it. The way you call it is:

AddFonts Combo1

where Combo1 is the name of the combobox you are tryign to load the Fonts into. Great! Thats about it:) Have fun and enjoy the speedier way of programming in VB with the Win32 API!

Work Work Work
Currently I'm working at Vividas Pty Ltd and studying at Swinburne University. My time on DeveloperFusion is limited due to workloads on both parts, I do however keep a Blog that gets updated fairly regularly with lots of Techno-babble...

I also have a software business called WebSoftware Systems in Australia, the primary product we have at the moment is HotHTML which started life as a simple VB6 based HTML editor and is now a full blown text/web development IDE. I'm currently also working on the v4.0 release in .NET 2.0.

Comments