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;
|