How to get TextWidth of string (without Canvas)?

后端 未结 2 1893
野的像风
野的像风 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;
    

提交回复
热议问题