Include
Font as a Resource in *.EXE
From:
"Steve Harman" <harmdog@ne.uswest.net>
Include a
font in your EXE:
1. 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.
2. 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
3. In your program, add a compiler
directive to include your newly created file:
{$R
MyFont.res}
This can go
right after the default {$R *.DFM} in the implementation section.
4. Add a procedure to create a file from
the resource, then make the Font available for use. Example:
procedure
TForm1.FormCreate(Sender: TObject);
var
Res : TResourceStream;
begin
Res := TResourceStream.Create(hInstance,
'MY_FONT', Pchar('ANYOL1'));
Res.SavetoFile('Bauhs93.ttf');
Res.Free;
AddFontResource(PChar('Bauhs93.ttf'));
SendMessage(HWND_BROADCAST,WM_FONTCHANGE,0,0);
end;
5. You can now assign the font to whatever
you wish:
procedure
TForm1.Button1Click(Sender: TObject);
begin
Button1.Font.Name := 'Bauhaus 93';
end;
Caveats:
The above
example provides for no error checking whatsoever. :-)
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:
procedure
TForm1.FormDestroy(Sender: TObject);
begin
RemoveFontResource(PChar("Bauhs93.ttf"))
SendMessage(HWND_BROADCAST,WM_FONTCHANGE,0,0);
end;
Check the
Win32 help for further details on the AddFontResource and RemoveFontResource.