0

My application is a VCL Win64 application built with Delphi 10.3.3.

For some reason, AddFontResource() is not working properly on Windows 11.

Here is what I have so far:

procedure TfrmMenuPrincipal.CriaFontesArialNarrow;
const
  cFonteArialNarrow: array [0..3] of string = (
         'ARIALN', 
         'ARIALNB', 
         'ARIALNBI', 
         'ARIALNI'
  );

var
  _FontFolder,
  _Resorce,
  _FontFile: string;
  _Res: TResourceStream;
begin
  _FontFolder := TSpecialFolders.GetAppDataFontFileFolder; // 'C:\Users\Leonardo\AppData\Roaming\MyApp\Fonts'

  for _Resorce in cFonteArialNarrow do
  begin
    _FontFile := _FontFolder + _Resorce + '.TTF';
    if not FileExists(_FontFile) then
    begin
      _Res := TResourceStream.Create(hInstance, _Resorce, 'FILE');
      try
        _Res.SaveToFile(_FontFile );
      finally
        _Res.Free;
      end;
    end;
    AddFontResource(PChar(_FontFile));
    //Screen.Fonts.Add(_FontFile );
    SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
  end;
//  Screen.Fonts.Add('Arial Narrow');
end;

No errors, app runs fine, but it can't use the fonts inside the application.

I read somewhere that font files should be on a folder that is contained in the PATH environment variable, but I have tested that and it didn't work.

Also, I have checked several Google hits, and other threads here at StackOverflow, but no luck.

Any tips?

1
  • Have you tried installing your font files in C:\Windows\Fonts instead? That said, rather than saving your fonts from your app's resources to disk, why not simply use AddFontMemResourceEx() instead to install the resource data as-is? Are you trying to allow other apps to use your fonts, or just your own app? Sep 22 at 16:28

1 Answer 1

0

Agreeing with Remy, to build the fonts into the application as a resource and using AddFontMemResourceEx() to load the font. For example...

{$R IonIconsFont.res}
begin
  Application.Initialize;
  Application.Title := APP_NAME;
  Application.MainFormOnTaskbar := True;
  Application.ShowMainForm := True;

  {Load embedded fonts *before* creating form so that positioning based on font is correct...}
  LoadResourceFont('IonIconsFont');
  
  Application.CreateForm(TMainForm, MainForm);
  Application.Run;
end.

Here is my LoadResourceFont function:

function LoadResourceFont(const ResourceName: string): Boolean;
var
  ResStream: TResourceStream;
  FontsCount: Integer;
  hFont: THandle;
begin
  ResStream := TResourceStream.Create(hInstance, ResourceName, RT_RCDATA);
  hFont := AddFontMemResourceEx(ResStream.Memory, ResStream.Size, nil, @FontsCount);
  Result := (hFont <> 0);
  ResStream.Free;
end;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.