【问题标题】:problem with a code for string grid ( align center )字符串网格的代码问题(对齐中心)
【发布时间】:2011-07-14 15:43:37
【问题描述】:

Delphi: How to make cells' texts in TStringGrid center aligned?

当我使用顶部代码(OnDraw 部分)时,它不会删除第一个文本并在旧文本上写入新文本,并且一个 sel 会重复。

【问题讨论】:

    标签: delphi alignment center tstringgrid


    【解决方案1】:

    在写出新文本之前,您需要添加对TCanvas.FillRect 的调用:

    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    var
      S: String;
    begin
      S := StringGrid1.Cells[ACol, ARow];
      StringGrid1.Canvas.FillRect(Rect);
      SetTextAlign(StringGrid1.Canvas.Handle, TA_CENTER);
      StringGrid1.Canvas.TextRect(Rect,
        Rect.Left + (Rect.Right - Rect.Left) div 2, Rect.Top + 2, S);
    end;
    

    请注意,您还必须确保将 TStringGrid.DefaultDrawing 设置为 False 才能正常工作。

    【讨论】:

    • 谢谢,但您的代码中的 S 是什么?我更改了您的代码,但它不起作用:
    • procedure TF_avalie_salon.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); var S:字符串;开始 S := StringGrid1.Cells[ACol, ARow]; StringGrid1.Canvas.FillRect(Rect); SetTextAlign(StringGrid1.Canvas.Handle, TA_CENTER); StringGrid1.Canvas.TextRect(Rect,Rect.Left + (Rect.Right - Rect.Left) div 2, Rect.Top + 2, S);结束;
    • 你想要一个字符串网格的屏幕截图吗?
    • S 是我从您的链接中获取代码时错过的东西。对不起。
    • 请为您的原始问题添加屏幕截图,因为我现在不明白问题所在。 FillRect 应该可以解决问题(除非您忘记将 StringGrid.DefaultDrawing 设置为 False)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-29
    相关资源
    最近更新 更多