我需要在Delphi的TStringGrid
中更改单元格的文本颜色。
只是一个单元格。 我该怎么做?
我需要在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;
(受这里的启发)
ACol
和ARow
参数仅更改一个单元格(并可能解释对InflateRect
的调用-作为建议,尽管您不需要一个单独的变量;您可以直接将Rect
传递给InflateRect
,因为它没有声明为const
)。 - Ken WhiteclWindow
和clWindowText
系统颜色;主题颜色的更改应该已经为您处理了,而默认绘图在不更改任何内容的情况下应该可以处理其余部分。我有一个完整的示例,用于绘制带有货币值右对齐列的TStringGrid
,我在工作中使用它,它可以很好地响应主题,只需要大约十几行代码。并不是一直建议自定义绘制自己的网格;如果需要,最好使用自定义组件。 - Ken White