Recent

Author Topic: Import local font and set size via code [SOLVED]  (Read 752 times)

AlphaInc.

  • Jr. Member
  • **
  • Posts: 93
Import local font and set size via code [SOLVED]
« on: April 19, 2021, 12:09:29 pm »
Hello everybody,

I have a small tool which uses the external component EasySize to automaticly adjust the tool's windows to the Screen Size of my monitor. Now I wanted to add a local Font (saved as a .ttf-file) to my tool without installing it to Windos. I have found a thread online, where someone wanted to do the same and I've been trying to implement this code-line without succes. Beyond you can see my FormCreate and FormResize procedure as well as the LoadFonts procedure which i manually created to load the local font. 

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.    //Loading Images to the Paintboxes
  4.    img[1] := TImage.create(form1);
  5.    img[1].Picture.LoadFromFile('../Binaries/background.jpg');
  6.    img[2] := TImage.create(form1);
  7.    img[2].Picture.LoadFromFile('../Binaries/background.jpg');
  8.    img[3] := TImage.create(form1);
  9.    img[3].Picture.LoadFromFile('../Binaries/logo.jpg');
  10.    img[4] := TImage.create(form1);
  11.    img[4].Picture.LoadFromFile('../Binaries/logo.jpg');
  12.  
  13.    PaintBox1.Tag := 1;
  14.    PaintBox2.Tag := 2;
  15.    PaintBox3.Tag := 3;
  16.    PaintBox4.Tag := 4;
  17.  
  18.    //Implemetnation of Resizer
  19.    Sizer := TFormResizer.Create(self);
  20.    Sizer.EnforceMinSize := false;
  21.    Sizer.ResizeFonts := true;
  22.    Sizer.MinFontSize := 10;
  23.    Sizer.MaxFontSize := 30;
  24.    Sizer.InitializeForm;
  25.    
  26.    Button1.Font.Size := 20;  //Doesn't affect the font!
  27.  
  28.    Width := round(Screen.Width/2.4);
  29.    Height := round(Screen.Height/2.4);
  30.    Position := poScreenCenter;
  31. end;
  32.  
  33. procedure TForm1.FormResize(Sender: TObject);
  34. begin
  35.    Sizer.ResizeAll;
  36. end;
  37.  
  38. procedure LoadFonts(); // There I want to import a local font.
  39. begin
  40.   if FileExists(ExtractFilePath(Application.ExeName)+'Auricom_Regular.ttf') then
  41.     if AddFontResourceEx(PAnsiChar(ExtractFilePath(Application.ExeName)+'Auricom_Regular.ttf'), FR_Private, Nil) <> 0 then
  42.     begin
  43.       SendMessage(Form1.Handle, WM_FONTCHANGE, 0, 0);
  44.       ShowMessage('Font Installed!');
  45.     end else RaiseLastOSError;
  46. end;  

Also, I tried to set a custom font-size for my button in the FormCreate procedure but this won't work. First I was susspecting the EasySize component to block this (because it handel's the size of the tool) but setting the size manually with Lazarus IDE the font size changes. Therefore, did i do something wrong?
« Last Edit: April 20, 2021, 09:30:44 pm by AlphaInc. »

wp

  • Hero Member
  • *****
  • Posts: 11853
Re: Import local font and set size via code
« Reply #1 on: April 19, 2021, 01:33:27 pm »
I tried to set a custom font-size for my button in the FormCreate procedure but this won't work. First I was susspecting the EasySize component to block this (because it handel's the size of the tool) but setting the size manually with Lazarus IDE the font size changes. Therefore, did i do something wrong?
When you add a control to a form its Font.Size in Lazarus (unlike Delphi) become 0 - this is later replaced by the default size of the widgetset. When a font size is rescaled by some tool, its size is multiplied by some number. But when the Font.Size is zero it is still zero after multiplication. The EasySize component, an ancient control of Delphi 4 times, does not know about the Lazarus font size handling...

What you can do to fix this is to initialize the font size in the object inspector with some non-zero value, e.g. 9 or 10.

AlphaInc.

  • Jr. Member
  • **
  • Posts: 93
Re: Import local font and set size via code
« Reply #2 on: April 19, 2021, 01:41:55 pm »
I tried to set a custom font-size for my button in the FormCreate procedure but this won't work. First I was susspecting the EasySize component to block this (because it handel's the size of the tool) but setting the size manually with Lazarus IDE the font size changes. Therefore, did i do something wrong?
When you add a control to a form its Font.Size in Lazarus (unlike Delphi) become 0 - this is later replaced by the default size of the widgetset. When a font size is rescaled by some tool, its size is multiplied by some number. But when the Font.Size is zero it is still zero after multiplication. The EasySize component, an ancient control of Delphi 4 times, does not know about the Lazarus font size handling...

What you can do to fix this is to initialize the font size in the object inspector with some non-zero value, e.g. 9 or 10.

For the said Button the value of Size TFont in the object inspector is set to 10. There was never a 0-value since it was set by default.
Or are we talking about something else ?

 

TinyPortal © 2005-2018