Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Install a Font

Status
Not open for further replies.

agoeddeke

Programmer
Jul 23, 2002
201
0
0
US
Hello All,

Does anyone know how to install a font programmatically from VFP using the Win API? I can check for the existence of a font using AFONT(), but I'd like to intall it automatically from a TTF file if it is not there.

Any ideas?

Andy
 
Hi Andy,

Have you tried copying the font to %systemroot%\fonts folder?
Code:
GETENV("SYSTEMROOT")+"\FONTS"
Regards,

Mike
 
I think I found the answer. I found this example on the Foxite forum posted by Brian Walsh:

Code:
llSucess = InstallSystemFont("path\filename.TTF")

PROCEDURE InstallSystemFont()
   LPARAMETERS lcFontLocFile
   LOCAL lnNumFontsAdded

   DECLARE INTEGER AddFontResource IN GDI32.DLL ;
      STRING @ lpszFileName
   DECLARE INTEGER SendMessage IN USER32.DLL ;
      INTEGER hWnd, ;
      INTEGER Msg, ;
      INTEGER wParameter, ;
      INTEGER lParameter
   #DEFINE HWND_BROADCAST 0xFFFF
   #DEFINE WM_FONTCHANGE  0x001D

   lnNumFontsAdded=AddFontResource(lcFontLocFile)
   IF lnNumFontsAdded > 0
      *\\Font added sucessfully, send notification to Windows so apps get updated
      =SendMessage(HWND_BROADCAST,WM_FONTCHANGE,0,0)
      RETURN .T.
      ELSE
      *\\Unable to add font
      RETURN .F.
   ENDIF   
ENDPROC
 
Thanks Mike and Mike.

I guess I always thought "installing" a font did something more than just put it in the FONTS folder. Is that really all it does?

Andy
 
Andy,

Is that really all it does?

That's certainly my understanding. It was different back in Win 3.x days. Maybe that API call you mentioned is one of those backward compatibility things.

Mike


Mike Lewis
Edinburgh, Scotland

My Visual Foxpro web site: My Crystal Reports web site:
 
Mike,

I believe the GDI32.DLL & USER32.DLL files did not exist during the 16-bit Win 3.x era. I can tell the approach is made at least for Win 95 and above.
 
Hi Andy,

One caveat, Windows can only register about 1000 fonts (expect it's 1024 but I remember reading "about 1000"). Not sure if this applied to all versions of Windows. I do know about the weird font substitution problems that start happening when you hit the limit.

Regards,

Mike
 
There are some things to note on installing fonts given what has been said in this thread. Copying the font is just as good if the machine is rebooted after the font is copied into the fonts folder. However, prior to rebooting, AddFontResource or AddFontResourceEx needs to be called in order for the font to be available to all applications. The reason for this is Windows holds an internal font table/array (or whatever you want to call it... array is probably closer to the way it is held in memory given the way you can enumerate it) and the font you copied won't be available in that table until you call AddFontResource or AddFontResourceEx.

You can make a font available temporarily. Just call the AddFontResource (as detailed in that post above in this thread) without copying the font to the Windows Fonts directory...the result: A font that is available until the machine is rebooted and then is no longer available.

So the difference is that calling the AddFontResource as above makes the font available UNTIL Windows is rebooted, copying the Font to the Fonts folder makes it available AFTER windows is rebooted, and copying it and then calling AddFontResource makes the Font available immediately and also after Windows is rebooted (obviously AddFontResource doesn't need to be called again after the first reboot as Windows will have already searched it's Fonts directory and added the new font to the internal fonts table it holds).

Microsoft probably does a better job of explaining this than I am, so here's a link I just found out on their site:


boyd.gif

 
craigsboyd said:
So the difference is that calling the AddFontResource as above makes the font available UNTIL Windows is rebooted, copying the Font to the Fonts folder makes it available AFTER windows is rebooted, and copying it and then calling AddFontResource makes the Font available immediately and also after Windows is rebooted (obviously AddFontResource doesn't need to be called again after the first reboot as Windows will have already searched it's Fonts directory and added the new font to the internal fonts table it holds).

Craig,

I played with all the various options late last night and these were EXACTLY my findings. In order to make the font immediately useful and have it around after a reboot, I had to copy the TTF to the GETENV("SYSTEMROOT")+"\FONTS" folder and then call AddFontResource in order to use it right away.

I was going to post my findings, but you beat me to it. I'll probably can all this in a procedure and post it later today or tomorrow.

Thanks everyone for the input.

Andy
 
Mike,

Windows can only register about 1000 fonts

That seems reasonable. I can't imagine anyone wanting to install anywhere near as many fonts as that -- not even a graphics or DTP professional. But who knows ...

Mike


Mike Lewis
Edinburgh, Scotland

My Visual Foxpro web site: My Crystal Reports web site:
 
MikeLewis said:
I can't imagine anyone wanting to install anywhere near as many fonts as that -- not even a graphics or DTP professional.
When Mike posted that I checked and found that I had 188 fonts installed. I'm not a desktop publisher or anything, but I've installed a few things on this old Win2000 box. I can't imagine having 1000 fonts... especially after learning that Windows loads all of them in memory upon startup.

Andy
 
Hi Andy and Mike,

When installing Corel's Wordperfect I told it to install all the fonts. Did I need them, no, but I did it anyway and went straight to "font hell".

You have no idea how odd it looks when the example code in VFP help has been substituted with the box drawing font. Something that has to do with Cascading Style Sheets. I never did find out where the substitution is stored, but it persists even after the correct font becomes available.

Regards,

Mike
 
Hola a todos, yo ya habia probado el api "AddFontResource" y funciona bien con 95,98 y ME, pero con XP o NT envia el mensaje "Declare DLL Call caused an exception" en el momento de declarar el api.

Algun comentario?

Rocco Di Chiara
Guatemala
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top