2005. július 24., vasárnap
How to store fonts in a resource file
Problem/Question/Abstract:
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:
{$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:
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;
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. 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:
procedure TForm1.FormDestroy(Sender: TObject);
begin
RemoveFontResource(PChar('Bauhs93.ttf'));
SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
end;
Feliratkozás:
Megjegyzések küldése (Atom)
Nincsenek megjegyzések:
Megjegyzés küldése