Install font in firemonkey

青春壹個敷衍的年華 提交于 2019-12-31 07:04:10

问题


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?


回答1:


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.




回答2:


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;


来源:https://stackoverflow.com/questions/15985465/install-font-in-firemonkey

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!