Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Share:
Notifications
Clear all

Lazarus Pascal - How to use a custom font file in Windows

1 Posts
1 Users
0 Reactions
10 Views
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2870
Topic starter  

This is the easiest way to use an external font file, not registered in Windows, in your Lazarus Applications under Windows.

I've written a how to for Delphi and a how to use a custom font for Lazarus under macOS, and here finally the trick for Lazarus Pascal under Windows.

In some of my projects I use a custom TTF font, generated using IcoMoon (generates super compact and very nicely scaling TTF fonts and I can highly recommend this for especially for icons/glyphs/pictograms!).

Note: The TTF file has to be stored in the same directory as the application executable, in this example I use "mycustomfont.ttf".

Note: At Design time you can assign the font name(s) to the Font.Name properties for control you want to use this font name with (here: myscustomfont).

 

Step 1: Define Windows specific GDI calls.

Add this to the type definition of the main form (on top somewhere before implemenation):

{$IFDEF Windows}
const
  FR_PRIVATE  = $00000010;

  function AddFontResourceExW(name: LPCWSTR; fl: DWORD; res: PVOID): LongInt; stdcall external 'gdi32.dll';
  function RemoveFontResourceExW(name: LPCWSTR; fl: DWORD; pdv: PVOID): BOOL; stdcall external 'gdi32.dll';
{$ENDIF}  

 

Step 2: Load the font file(s) when your application starts.

Next in the OnCreate event of the main form, we call AddFountResourceExw to load the font:

procedure TForm1.FormCreate(Sender: TObject);
begin
  {$IFDEF Windows}
  AddFontResourceExW(PWideChar('mycustomfont.ttf'),FR_PRIVATE,nil);
  {$ENDIF}                  
end;

Now, this will load the font and the Lazarus IDE, after the first run of your application, seems to be able to show them at design time.

 

Step 3: Unload the font(s) when closing your application (optional).

For good measure though, it is not a bad idea to unload your custom font when the application is being closed in the OnDestroy event of the main form, like so (this makes it that the Lazarus IDE may not see the font either after the application was closed!):

procedure TForm1.FormDestroy(Sender: TObject);
begin
  {$IFDEF WINDOWS}
  RemoveFontResourceExW(PWideChar('mycustomfont.ttf'),FR_PRIVATE,nil);
  {$ENDIF}
end; 

  

Hope this is useful for someone 😊 

This topic was modified 5 hours ago by Hans

   
ReplyQuote
Share: