如何在Delphi TGrid Firemonkey组件中更改单元格的颜色? [英] How to change color of a cell in a Delphi TGrid Firemonkey component?
问题描述
我以TGrid列中的单元格为例。组件选项中没有颜色属性。颜色只能通过代码访问。该代码必须位于绘制列单元格事件中,但是它是什么代码?我试图使用与VCL组件相同的过程,但是FMX中的Tcanvas不包含brush属性。该网站上的其他类似问题仅是对颜色处理方式的推测。
有人能成功更改单元格(或其他组件)中的背景颜色吗?
FMX
框架提供了几种更改 TGrid
背景外观的方法。下面介绍了两种交替显示行颜色和每个单元格颜色的方法。
交替显示行颜色,可以选择使用样式
它作为可预置的布尔项存在于名为 AlternateRowBackground
TGrid.Options 属性中>。默认颜色是浅灰色($ FFEEEEEE)。要更改此颜色,您可以添加 TStyleBook
或右键单击网格并选择 Edit Custom Style ...
或编辑默认样式...
,然后更改 gridstyle-交替行背景$的
颜色
属性c $ c>。在下面的示例中,颜色更改为 Bisque
:
文本的颜色与这个答案
I use the example of the cell in a TGrid column as an example. 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? I attempted to use the same process as in VCL components, but the Tcanvas in FMX does not include a brush property. 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
. 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
. The default color is a light gray color ($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
. Here an example where the color is changed to Bisque
:
Code in the OnDrawColumnCell
event
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:
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):
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
这篇关于如何在Delphi TGrid Firemonkey组件中更改单元格的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!