How to get TextWidth of string (without Canvas)?

后端 未结 2 1882
野的像风
野的像风 2020-12-31 03:51

I would like to get text width of a string before an application starts. Everything works fine until Application.MainForm canvas present. The problem is, when I try dynamica

相关标签:
2条回答
  • 2020-12-31 04:24

    This works:

    procedure TForm1.FormCreate(Sender: TObject);
    var
      c: TBitmap;
    begin
      c := TBitmap.Create;
      try
        c.Canvas.Font.Assign(self.Font);
        Caption := IntToStr(c.Canvas.TextWidth('My String'));
      finally
        c.Free;
      end;
    end;
    
    0 讨论(0)
  • 2020-12-31 04:30

    I'm not sure if this can be done, but if by "before the app starts" you mean "before the main form is displayed", you could always put your canvas-related code in the main form's OnCreate event. You'll have a valid canvas by that point.

    0 讨论(0)
提交回复
热议问题