【问题标题】:Width in pixels of a text/caption in Delphi 7Delphi 7中文本/标题的像素宽度
【发布时间】:2010-12-06 14:07:55
【问题描述】:

这是我的问题,我想知道文本的实际长度(以像素为单位)(请注意,在某些字体中,各种字母的长度不同)。我将使用它在 DBGrid 中更好地调整列宽。

【问题讨论】:

    标签: delphi font-size column-width caption


    【解决方案1】:

    您可以使用Canvas.TextWidthCanvas.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;
    

    【讨论】:

      【解决方案2】:

      如果你有一个 Delphi 组件有一个“Canvas”属性,那么你可以使用 Component.Canvas.TextWidth。例如:要获取 DBGrid 文本的宽度,您可以使用:

      DBGrid1.Canvas.TextWidth('Stack'); 
      

      您可以在此处找到有关此问题的完整参考: Length of Delphi string in pixels

      【讨论】:

        猜你喜欢
        • 2010-11-23
        • 2017-07-27
        • 1970-01-01
        • 1970-01-01
        • 2011-02-19
        • 2012-03-15
        • 2010-10-28
        • 2019-03-22
        • 1970-01-01
        相关资源
        最近更新 更多