[英]How to change color of a cell in a Delphi TGrid Firemonkey component?
I use the example of the cell in a TGrid column as an example.我以 TGrid 列中的单元格示例为例。 There is no color property in the components option.组件选项中没有颜色属性。 THe color is only accessible by code.颜色只能通过代码访问。 The code must go in the Draw Column Cell event, but what code does it?代码必须放在 Draw Column Cell 事件中,但是它的代码是什么? I attempted to use the same process as in VCL components, but the Tcanvas in FMX does not include a brush property.我尝试使用与 VCL 组件中相同的过程,但 FMX 中的 Tcanvas 不包含画笔属性。 Other similar questions on the site do not provide more than speculations on how color is handled.该网站上的其他类似问题仅提供有关如何处理颜色的推测。
Has anyone successfully changed the background color in a cell (or other component)?有没有人成功地改变了单元格(或其他组件)的背景颜色?
The FMX
framework offers a couple of means to change the appearance of the background of a TGrid
. FMX
框架提供了几种方法来更改TGrid
背景的外观。 Two means, alternating row colors and color per cell are presented in the following.下面介绍了两种方法,交替行颜色和每个单元格的颜色。
Alternating row colors, optionally using styles交替行颜色,可选择使用样式
This exists as a presetable boolean item in the TGrid.Options
property named AlternateRowBackground
.这作为可预设的布尔项存在于名为AlternateRowBackground
的TGrid.Options
属性中。 The default color is a light gray color ($FFEEEEEE).默认颜色为浅灰色 ($FFEEEEEE)。 To change this color you can add a TStyleBook
or right click the grid and select Edit Custom Style...
or Edit Default Style...
and then change the Color
property of gridstyle - alternatingrowbackground
.要更改此颜色,您可以添加TStyleBook
或右键单击网格并选择Edit Custom Style...
或Edit Default Style...
,然后更改网格样式的Color
属性gridstyle - alternatingrowbackground
。 Here an example where the color is changed to Bisque
:这是一个颜色更改为Bisque
的示例:
Code in the OnDrawColumnCell
event OnDrawColumnCell
事件中的代码
This even is called for each cell of the grid and offers full control of painting the cell background.这甚至会为网格的每个单元格调用,并提供对绘制单元格背景的完全控制。 The header of the event handler looks like this:事件处理程序的标头如下所示:
procedure TForm11.Grid1DrawColumnCell(Sender: TObject; const Canvas: TCanvas;
const Column: TColumn; const Bounds: TRectF; const Row: Integer;
const Value: TValue; const State: TGridDrawStates);
For painting we will need a TBrush
, so we declare one as a local variable:为了绘画,我们需要一个TBrush
,所以我们将一个声明为局部变量:
var
bgBrush: TBrush;
We are now ready to apply some scenarios of special background drawing.我们现在准备应用特殊背景绘制的一些场景。 First is how to let the default drawing take place for certain cell states.首先是如何让默认绘图发生在某些单元格状态。
if (TGridDrawState.Selected in State) or
(TGridDrawState.Focused in State) then
begin
Grid1.DefaultDrawColumnCell(Canvas, Column, Bounds, Row, Value, State);
Exit;
end;
For the following we will need the TBrush
, so we create it and manage its lifetime (on Windows platform):对于以下内容,我们将需要TBrush
,因此我们创建它并管理其生命周期(在 Windows 平台上):
bgBrush:= TBrush.Create(TBrushKind.Solid, TAlphaColors.White); // default white color
try
//
// following code snippets go in here
//
finally
bgBrush.Free;
end;
Next, an example of painting alternating row backgrounds without using styles接下来,一个不使用样式绘制交替行背景的示例
if Odd(Row) then
bgBrush.Color := TAlphaColors.MoneyGreen+$202020; // a very light green color
Canvas.FillRect(Bounds, 0, 0, [], 1, bgBrush);
Then an example of background color for given columns然后是给定列的背景颜色示例
case Column.Index of
0: bgBrush.Color := TAlphaColors.lightBlue;
1: bgBrush.Color := TAlphaColors.MoneyGreen;
end;
Canvas.FillRect(Bounds, 0, 0, [], 1, bgBrush);
And finally an example of background color determined by value of data最后是一个由数据值决定的背景颜色的例子
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);
And a sample image:和示例图像:
The texts are colored as in this answer文本的颜色与此答案相同
Please see this example.请看这个例子。 Explanations in comments:评论中的解释:
procedure 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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.