Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to store fonts in a resource file Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
12-Oct-02
Category
Graphic
Language
Delphi 2.x
Views
125
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jonas Bilinkevicius

Is there a way to store a particular font in an *.ini type of file so that it can 
be recalled when an application starts?

Answer:

There may be copyright issues with Fonts. With that said, you can include the font 
directly in you program with a resource file.

Using your favorite text editor, create a *.rc file that describes the font:

MY_FONT ANYOL1 "Bauhs93.ttf"

The first two parameters can be whatever you want. They get used in your program 
later. Then, use the BRCC32.EXE command line compiler that ships with Delphi to 
create a *.res file. If your file in step 1 was MyFont.rc, the command from the DOS 
prompt would be:

BRCC32 MyFont

The program will append the .rc to the input, and create a file with the same name 
except it appends .res: MyFont.res . In your program, add a compiler directive to 
include your newly created file:
1   
2   {$R MyFont.res}


This can go right after the default {$R *.DFM} in the implementation section. Add a 
procedure to create a file from the resource, then make the Font available for use. 
Example:

3   procedure TForm1.FormCreate(Sender: TObject);
4   var
5     Res: TResourceStream;
6   begin
7     Res := TResourceStream.Create(hInstance, 'MY_FONT', Pchar('ANYOL1'));
8     Res.SavetoFile('Bauhs93.ttf');
9     Res.Free;
10    AddFontResource(PChar('Bauhs93.ttf'));
11    SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
12  end;
13  
14  //You can now assign the font to whatever you wish:
15  
16  procedure TForm1.Button1Click(Sender: TObject);
17  begin
18    Button1.Font.Name := 'Bauhaus 93';
19  end;



Caveats:

The above example provides for no error checking whatsoever. The user may already 
have that font installed.

Notice that the File name is NOT the same as the Font name. It's assumed that you 
know the font name associated with the file name. You can determine this by double 
clicking on the file name in the explorer window.

I would recommend placing your font file in the C:\WINDOWS\FONTS folder. It's 
easier to find them later.

Your newly installed font can be removed programatically, assuming the font is not 
in use anywhere:

20  procedure TForm1.FormDestroy(Sender: TObject);
21  begin
22    RemoveFontResource(PChar('Bauhs93.ttf'));
23    SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
24  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC