Install font in firemonkey

2019-03-03 13:29发布

How Can I use embedded font or install new fonts in my firemonkey application?

I tried this solution but WM_FONTCHANGE is not defined in FMX!

I want to use custom font in my application, how I can do this?

2条回答
甜甜的少女心
2楼-- · 2019-03-03 14:03

You can surely use the Winapi.Messages unit in your FMX app which is clearly targeting Windows, and the message constant is defined there.

If you don't want to use Winapi.Messages, just define the constant in your own code:

const
  WM_FONTCHANGE = $001D;
查看更多
贼婆χ
3楼-- · 2019-03-03 14:06

I followed the instructions here, and they were of some help. I have a few extra hints that may help,

  • The two inclusions that you will need to make this work is WinAPI.Windows and WinAPI.Messages.
  • If you put these inclusions at the start of your "uses" clause, you're unlikely to have name conflicts with things like TBitmap.

Now, I've adding the following code,

procedure TMainForm.FormCreate(Sender: TObject);
begin
  AddFontResource('c:\fontpath\myfont.ttf');
  SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0) ;
end;

procedure TMainForm.FormDestroy(Sender: TObject);
begin
  RemoveFontResource('c:\fontpath\myfont.ttf');
  SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0) ;
end;

When I run this in a firemonkey form, i've observed the following.

  1. The code runs
  2. Other applications (eg. notepad, office) can suddenly see the new font
  3. Text objects in my application don't recognise the new font
  4. If I dynamically name my text object with the font name, it still won't change.
  5. If I dynamically name my text object with another obvious font, it changes.
  6. If I shut down my application, other applications stop seeing the new font.

This tells me that my code works (see bullets 1. and 6.) - but FMX won't recognise the font on my main form if you use the name that appears in notepad. I've quadruple checked the font name.

I created two identical projects. One was VCL and the other was FMX. The VCL project works perfectly - both for static text and dynamic text. The FMX code works for neither. If I had to hazard a guess, i'd say that FMX is building a list of fonts on startup, and checking the list of available fonts against that list (ie. like a cache). I'd hazard a guess this is being done to abstract FMX from the underlying operating system...

If anyone has made this work under firemonkey, I would appreciate any advice. Also, if anyone knows how to achieve the same goal under a Mac, i'd appreciate a pointer too.

Regards,

Mr Ed.

查看更多
登录 后发表回答