我以TGrid列中的单元格为例。组件选项中没有颜色属性。颜色只能通过代码访问。代码必须在“绘制列单元格”事件中使用,但是需要什么样的代码呢?我尝试使用与VCL组件相同的过程,但是FMX中的Tcanvas不包括刷子属性。该网站上的其他类似问题只是猜测如何处理颜色。
是否有人成功更改了单元格(或其他组件)的背景色?
我以TGrid列中的单元格为例。组件选项中没有颜色属性。颜色只能通过代码访问。代码必须在“绘制列单元格”事件中使用,但是需要什么样的代码呢?我尝试使用与VCL组件相同的过程,但是FMX中的Tcanvas不包括刷子属性。该网站上的其他类似问题只是猜测如何处理颜色。
是否有人成功更改了单元格(或其他组件)的背景色?
FMX
框架提供了几种改变TGrid
背景外观的方法。以下介绍两种方法,即交替行颜色和单元格颜色。
交替行颜色,可选择使用样式
这是在TGrid.Options
属性中存在的可预设布尔项,名为AlternateRowBackground
。默认颜色为浅灰色($FFEEEEEE)。要更改此颜色,可以添加TStyleBook
或右键单击网格并选择编辑自定义样式...
或编辑默认样式...
,然后更改gridstyle-alternatingrowbackground
的Color
属性。这里是一个将颜色更改为Bisque
的示例:
OnDrawColumnCell
事件中的代码
这个事件会为网格的每个单元格调用一次,并提供完全控制绘制单元格背景的功能。事件处理程序的标题如下:
procedure TForm11.Grid1DrawColumnCell(Sender: TObject; const Canvas: TCanvas;
const Column: TColumn; const Bounds: TRectF; const Row: Integer;
const Value: TValue; const State: TGridDrawStates);
TBrush
,因此我们将其声明为本地变量:var
bgBrush: TBrush;
现在我们准备应用一些特殊背景绘制的场景。首先是如何让默认绘制出现在某些单元格状态下。
if (TGridDrawState.Selected in State) or
(TGridDrawState.Focused in State) then
begin
Grid1.DefaultDrawColumnCell(Canvas, Column, Bounds, Row, Value, State);
Exit;
end;
接下来我们需要用到TBrush
,因此我们创建它并管理其生命周期(在Windows平台上):
bgBrush:= TBrush.Create(TBrushKind.Solid, TAlphaColors.White); // default white color
try
//
// following code snippets go in here
//
finally
bgBrush.Free;
end;
接下来是一个无需使用样式绘制交替行背景的示例
if Odd(Row) then
bgBrush.Color := TAlphaColors.MoneyGreen+$202020; // a very light green color
Canvas.FillRect(Bounds, 0, 0, [], 1, bgBrush);
case Column.Index of
0: bgBrush.Color := TAlphaColors.lightBlue;
1: bgBrush.Color := TAlphaColors.MoneyGreen;
end;
Canvas.FillRect(Bounds, 0, 0, [], 1, bgBrush);
最后,这是一个由数据值决定背景颜色的示例。
if Column.Index = 1 then
if Value.AsOrdinal < 0 then // negative
bgBrush.Color := TAlphaColors.Lightpink
else
bgBrush.Color := TAlphaColors.MoneyGreen;
Canvas.FillRect(Bounds, 0, 0, [], 1, bgBrush);
并提供一个样例图片:
文本颜色如此答案所示。
OnDrawColumnCell()
事件时,需要首先安排背景颜色的绘制,然后再进行文本绘制。即先使用FillRect()
函数进行绘制,然后使用FillText()
函数。 - Tom Brunbergprocedure TForm11.Grid1DrawColumnCell()
,不是吗?当我说OnDrawColumnCell
时,我是在谈论属性,IDE会将过程名称Grid1DrawColumnCell
分配给该属性。如果这不清楚,您需要针对此问题提出一个新问题,但首先请阅读文档。在我上面的答案结尾处,有一个关于文本颜色的问题的链接,其中显示了FillText()
方法的用法。 - Tom Brunbergprocedure TfrmOperationTab1.strgridHeartbeatsDrawColumnCell(Sender: TObject;
const Canvas: TCanvas; const Column: TColumn; const Bounds: TRectF;
const Row: Integer; const Value: TValue; const State: TGridDrawStates);
var
bgBrush: TBrush;
begin
try
// if a cell contains word 'WARNING' then we want to paint its background in Pink color
if Value.AsString.Contains('WARNING') then
begin
// Create pink brush
bgBrush:= TBrush.Create(TBrushKind.Solid, TAlphaColors.Lightpink);
// Paint the whole area
Canvas.FillRect(Bounds, 0, 0, [], 1, bgBrush);
bgBrush.Free;
end;
finally
end;
// IMPORTANT: let system draw all the other staff. If not called, then you wont see the content of your cell
Column.DefaultDrawCell(Canvas, Bounds, Row, Value, State);
end;