Author: Jonas Bilinkevicius
How to get the text width in pixels when a component doesn't have a canvas
Answer:
If a component doesn't have a Canvas property you can use the following function to
get the text width based on the font passed.
1 2 function GetTextWidth(CanvasOwner: TForm; Text: string; TextFont: TFont): Integer;
3 var4 OldFont: TFont;
5 begin6 OldFont := TFont.Create;
7 try8 OldFont.Assign(CanvasOwner.Font);
9 CanvasOwner.Font.Assign(TextFont);
10 Result := CanvasOwner.Canvas.TextWidth(Text);
11 CanvasOwner.Font.Assign(OldFont);
12 finally13 OldFont.Free;
14 end;
15 end;