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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Font as resource?

Status
Not open for further replies.

VincentP

Programmer
Apr 1, 2001
116
I would like to use a specific font (True Type) in my program, but I can't be sure if it is installed on my clients' systems. I would like to know how to use a font which is not installed (i.e. load a .TTF file and get an HFONT or CFont out of it). Or, preferably, have the .TTF file as a resource inside my .EXE

If no one knows how to do that, can someone point to me how to install a font on a computer programmatically?

Thanks,

Vincent
 
Hi Vincent, yes it is possible: you add the font file to your project resources by 'importing' it and giving the resource type a specific name (eg. "FONT"). The resource type is the name of the little directory/folder that appears in the list of resorce types.

Make a note of the resource ID - eg. IDR_MYFONT.

Download my CFileExtractor library and follow the instructions in the included WordPad document. The library is a static library and can be downloaded from my web site at:


You will be able to extract the font to the user's hard disk with one single line of code, eg:

Code:
CFileExtractor(IDR_MYFONT,"FONT","c:\\windows\\fonts\\myFont.ttf");

The constructor does everything for you in this one line of code. All you have to do is specify the resource ID, the resource type (as a string) and the path/filename where you want the file to be saved.

There's also an alternative constructor which will display the common 'Save-as' dialog to the user to prompt for file name and save location.

Hope this helps!! :)
 
Thanks, but what I would really want to do is use the font without installing it on the client's system. Besides, I don't think that simply copying the files to the "font" directory is enough to "install" it. The registry has to be updated (at reboot time? Or automatically? I'm not sure...)

Vincent
 
I'm not too sure Vince, I think you should be able to load the font without restarting. However, the font would have to be installed before the app accesses the fonts list (or whatever) for it to show up.
Anyhow, unlike on the Macintosh, I really don't think there's anyway to do it on Windows without loading the font onto the user's hard disk first.

If you really get stuck, I guess you could 'install' the font from your programs installer file!! ;-)
 
You can use AddFontResource() and RemoveFontResource().
 
Cool beans - can't believe I've never seen those function calls before!! :) Learn something new every day!
 
Thanks tchouch, I look forward to try these!

Vincent
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top