这是我的问题,我想知道文本的实际长度(以像素为单位)(请注意,各种字母在某些字体中具有不同的长度)。我将使用它来更好地调整 DBGrid 中的列宽。
问问题
18853 次
2 回答
29
您可以使用 Canvas.TextWidth
和Canvas.TextHeight
功能。
选项1,使用控件的画布
WidthInPixels := Label1.Canvas.TextWidth('My Text');
选项 2,创建临时画布(使用 Tbitmap)
Function GetWidthText(const Text:String; Font:TFont) : Integer;
var
LBmp: TBitmap;
begin
LBmp := TBitmap.Create;
try
LBmp.Canvas.Font := Font;
Result := LBmp.Canvas.TextWidth(Text);
finally
LBmp.Free;
end;
end;
于 2009-10-14T07:29:16.703 回答
6
如果你有一个 Delphi 组件有一个“Canvas”属性,那么你可以使用 Component.Canvas.TextWidth。例如:要获取 DBGrid 文本的宽度,您可以使用:
DBGrid1.Canvas.TextWidth('Stack');
在这里您可以找到有关此问题的完整参考: Delphi 字符串的长度(以像素为单位)
于 2009-10-14T07:32:17.183 回答