• Get Name of Installed Font using API (Vb API)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Get Name of Installed Font using API (Vb API)

    Author
    Topic
    #361037

    Get Name of Installed Font using API

    I can temporarily install a font file using the AddFontResource lib, but how do I get the name of the font that I just installed? I need to know the name of the font so that I can call it when I automate Word. I think I need to use the GetTextFace lib, but I don’t fully understand how to use this function; Here is the Windows API declaration:

    Private Declare Function GetTextFace Lib “gdi32” Alias “GetTextFaceA” (ByVal hdc As Long, ByVal nCount As Long, ByVal lpFacename As String) As Long

    In Dan Appleman’s Win32 API book, it says the “hdc” is to be a handle to a device context- what does this mean!?! Can someone breakdown this function and explain its parts? I am guessing that the lpFacename holds a string that the font name is dumped to and nCount sets the size of this string- why?.

    A little guidance is much appreciated!

    Thank you!

    –Llyal

    PS – I have found that the AddFontResource only temporarily installs a font (a machine re-boot will remove them); this is with Windows 2000; are there no APIs that will allow me to permanently install and uninstall fonts?

    Viewing 0 reply threads
    Author
    Replies
    • #544942

      First, that function doesn’t do what you think it does, and I don’t recall offhand what the right one is — I may be able to find it tomorrow.

      Jeff Prosise explained it this way:
      “When a Windows program draws to a screen, a printer, or another output device, it doesn’t communicate directly with the device as an MS-DOS program is apt to do. Rather, the Windows program draws to a logical “display surface” represented by a device context (DC). Deep inside Windows, a device context is a data structure containing fields that describe everything the GDI needs to know about the display surface and the context in which it is being used…”

      The following is from a C++ viewpoint. I’m not sure you can do the same thing in VB…

      You can make copies of a DC and modify them before displaying them. If your function receives a copy of a DC and needs to add some text to it, you may need to find out what face is already selected there. That’s what GetTextFace is for: you pass in the DC (actually a handle to the DC, hence hDC) and a buffer that can hold a string. The nCount parameter says how big the buffer is. When the function returns, the buffer contains the name of the selected font in the DC.

      The MS documentation is at http://msdn.microsoft.com/library/default….ontext_3jtx.asp, but again, that’s based on C++ and may be hard to apply to VB.

      • #545029

        Jay,

        Thanks for the input- it looks like getting the font name is not easy; this probably explains why I have not seen much about this on the internet;

        I could be taking the wrong approach; what I could do is use Word- I could enumerate through the installed fonts in Word, remember them, install the font, then enumerate through the installed fonts again, find the “extra” font and get its name;

        You can see how this will be slow for 3000+ fonts, but it is better than nothing;

        Thanks for the help!

        –Llyal

        • #545085

          3000+ fonts????

          Anything much over 1000 installed fonts makes Windows crawl on its knees snail and then fall over. puke Mostly I see recommendations to keep it down around 250, using a font manager if necessary.

          • #545192

            Jay,

            When you “permanently” install fonts, you are absolutely correct; I couldn’t install more than several hundred fonts this way;

            But I have found that by using the APIs and installing the fonts temporarily, I could install 3800+ fonts and manipulate Word and other apps with no great problems; and the computer will revert itself back to the “permanently” installed fonts upons a re-boot (without having to unistall all the temp fonts);

            This is an interesting workaround to a windows limitation;

            Try it, you will see for yourself!

            –Llyal

    Viewing 0 reply threads
    Reply To: Get Name of Installed Font using API (Vb API)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: