【问题标题】:How to calculate the "Left" Property to center a Text in a DBGrid Cell?如何计算“左”属性以使 DBGrid 单元格中的文本居中?
【发布时间】:2012-11-08 18:52:41
【问题描述】:

继续开始的项目:

How to auto fit/scale DBGrid's (or other similar) columns widths according to its contents?

如何计算“Left”属性以使 DBGrid 单元格中的文本居中?

当我们调用 OnDrawColumnCell 并使用 Canvas 编写文本来代替网格的默认绘制时,我们要如何计算文本在单元格上居中的位置?

【问题讨论】:

    标签: delphi delphi-2006


    【解决方案1】:

    不要。使用DrawText/DrawTextEx 绘制文本并在格式参数中指定DT_CENTER。另见Draw text multiline in the centre of a rect

    或者如果您想或需要自己计算:

    procedure DrawCenteredText(Canvas: TCanvas; const S: String; R: TRect);
    var
      Left: Integer;
    begin
      Left := R.Left + (R.Right - R.Left - Canvas.TextWidth(S)) div 2;
    

    【讨论】:

      【解决方案2】:

      有更多可能性的更简单的方法是:

      Canvas.TextRect(Rect,s,[tfCenter,tfVerticalCenter,tfSingleLine]);
      

      【讨论】:

      • @NGLN,@bummi,There is no overloaded version of '%s' that can be called with these arguments (E2250)
      • 哦,抱歉,我以为我找到了它在 D2005 中的引用,但显然它还没有出现在 D2006 中!
      猜你喜欢
      • 2016-09-13
      • 2014-07-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-30
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多