简体   繁体   中英

How to use external fonts?

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!

在Windows 2000及更高版本中,您可以使用AddFontMemResourceEx从内存中为进程安装字体。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM