Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

delphi - 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:FONTSMyFont.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:FONTSMyFont.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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...