我以TGrid列中单元格的示例为例。组件选项中没有颜色属性。颜色只能通过代码访问。代码必须放在Draw Column Cell事件中,但是代码是什么?我试图使用与VCL组件中相同的过程,但FMX中的Tcanvas不包含画笔属性。网站上的其他类似问题并未提供关于如何处理颜色的猜测。如何更改Delphi TGrid Firemonkey组件中单元格的颜色?
是否有人成功更改了单元格(或其他组件)中的背景颜色?
我以TGrid列中单元格的示例为例。组件选项中没有颜色属性。颜色只能通过代码访问。代码必须放在Draw Column Cell事件中,但是代码是什么?我试图使用与VCL组件中相同的过程,但FMX中的Tcanvas不包含画笔属性。网站上的其他类似问题并未提供关于如何处理颜色的猜测。如何更改Delphi TGrid Firemonkey组件中单元格的颜色?
是否有人成功更改了单元格(或其他组件)中的背景颜色?
FMX
框架提供了一些手段来改变TGrid
的背景外观。下面介绍两种方法,每种单元交替排列颜色和颜色。
交替行的颜色,可选择使用样式
这存在在一个名为AlternateRowBackground
的TGrid.Options
属性可预设布尔项目。默认颜色是浅灰色($ FFEEEEEE)。要更改此颜色,您可以添加TStyleBook
或右键单击网格并选择Edit Custom Style ...
或Edit Default Style ...
,然后更改Color
属性gridstyle - alternatingrowbackground
。在这些地方颜色变为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);
的值和样本图像确定背景色的一个示例:
案文着色如this answer
乘坐看看http://stackoverflow.com/questions/39956166/fmx-delphi-berlin-how-to-change-font-color-in-rows-of-tgrid – RBA
VCL和FMX组件彼此不同,访问TGrid中的T列中的单元格只能通过代码访问。单元看起来不是一个组件(或一个类)。 – Ashlar