How to use external fonts?
Asked Answered
B

2

15

Is it possible to use a font directly from resources in Delphi and how?

I have a problem with the very first steps.Example I cannot include Segoe UI Light font in resources of a project,an exception occurs.And that is only if the file's extension is 'ttf'.

If the written above is not possible then how do I use an external font without deploying the font separately(from executable)?

Thanks in advance!

Bitterweed answered 21/2, 2010 at 8:41 Comment(0)
S
15

On Windows 2000 and later, you can use AddFontMemResourceEx to install fonts for your process from memory.

Shavers answered 22/2, 2010 at 9:57 Comment(1)
Just what I was searching for,Thank you!Bitterweed
S
15

If you want to use a font the font must be installed. But you can fake this, by using AddFontResource.

procedure TForm1.FormCreate(Sender: TObject) ;
begin
  AddFontResource('c:\FONTS\MyFont.TTF') ;
  SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0) ;
end;

//Before application terminates we must remove our font:
procedure TForm1.FormDestroy(Sender: TObject) ;
begin
  RemoveFontResource('C:\FONTS\MyFont.TTF') ;
  SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0) ;
end;

As you see the AddFontResource needs a file name. The same stands for AddFontResourceEx.

So you need a font file. But we can also fake that.

Use JVCL's TjvDataEmbedded to include your TTF file in your executable. To embed the font file is straightforard. (Right-Click, 'Load from File'...).

At runtime, extract your file in user's temporary directory (see TjvDataEmbedded methods - I don't know now, but it should be something like SaveToFile or similar). Btw you can extract it in any other directory you like. Call AddFontResource on it.

Also, according to your requirements, you can extract the file in a memory mapped one and/or in a RAM drive.

HTH

Snipes answered 21/2, 2010 at 9:7 Comment(5)
Great answer! Thank you! Only one thing lasts: How do I assign TForm.Font with the 'installed' font?Bitterweed
@John: This is good as far as the technical side is concerned - however you need to make sure that you have redistribution rights for the font.Homemade
@John: You must know the name of the font. This is easy - install the font, open the WordPad (or similar) and see what the font name appears. Eg. for timesb.ttf the name is 'Times New Roman (Bold)'. So, in your application you must assign the font name (not the file name) to the TForm.Font. Eg. after installing (by using the above procedure) the mytimes.ttf you will have the line: myMainForm.Font.Name:='My Times New Roman'; myMainForm.Font.Size:=10; //etc. As an aside, be sure that all the controls of your form have ParentFont:=TrueSnipes
@John: If you are not familiar with changing fonts, perhaps is better to try the above procedure on 'regular' fonts to be sure that you know how it works. (This is just to make your 'debugging life' easier :-) )Snipes
My firemonkey app does not recognized WM_FONTCHANGE. How I can use it?Simp
S
15

On Windows 2000 and later, you can use AddFontMemResourceEx to install fonts for your process from memory.

Shavers answered 22/2, 2010 at 9:57 Comment(1)
Just what I was searching for,Thank you!Bitterweed

© 2022 - 2024 — McMap. All rights reserved.