gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 14:57:39 26 4
gpt4 key购买 nike

我需要更改 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. )

关于delphi - 更改 TStringGrid 单元格的字体颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8043582/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com