加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 更改TStringGrid单元格的字体颜色

发布时间:2020-12-15 04:30:45 所属栏目:大数据 来源:网络整理
导读:我需要在Delphi中更改TStringGrid单元格中的文本颜色. 只是一个细胞.我怎样才能做到这一点? 解决方法 您可以使用DrawCell事件自行绘制单元格内容. procedure TForm1.GridDrawCell(Sender: TObject; ACol,ARow: Integer; Rect: TRect; State: TGridDrawState
我需要在Delphi中更改TStringGrid单元格中的文本颜色.

只是一个细胞.我怎样才能做到这一点?

解决方法

您可以使用DrawCell事件自行绘制单元格内容.
procedure TForm1.GridDrawCell(Sender: TObject; ACol,ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  S: string;
  RectForText: TRect;
begin
  // Check for your cell here (in this case the cell in column 4 and row 2 will be colored)
  if (ACol = 4) and (ARow = 2) then
  begin
    S := Grid.Cells[ACol,ARow];
    // Fill rectangle with colour
    Grid.Canvas.Brush.Color := clBlack;
    Grid.Canvas.FillRect(Rect);
    // Next,draw the text in the rectangle
    Grid.Canvas.Font.Color := clWhite;
    RectForText := Rect;
    // Make the rectangle where the text will be displayed a bit smaller than the cell
    // so the text is not "glued" to the grid lines
    InflateRect(RectForText,-2,-2);
    // Edit: using TextRect instead of TextOut to prevent overflowing of text
    Grid.Canvas.TextRect(RectForText,S);
  end;
end;

(灵感来自this.)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读