gpt4 book ai didi

delphi - 如何更改 Delphi TGrid Firemonkey 组件中单元格的颜色?

转载 作者:行者123 更新时间:2023-12-03 15:51:31 25 4
gpt4 key购买 nike

我使用 TGrid 列中的单元格示例作为示例。组件选项中没有颜色属性。颜色只能通过代码访问。该代码必须放在“绘制列单元格”事件中,但是什么代码是做什么的呢?我尝试使用与 VCL 组件中相同的过程,但 FMX 中的 Tcanvas 不包含画笔属性。该网站上的其他类似问题仅提供了有关如何处理颜色的猜测。

有人成功更改了单元格(或其他组件)中的背景颜色吗?

最佳答案

FMX 框架提供了多种方法来更改 TGrid 背景的外观。下面介绍了两种方法,即交替行颜色和每个单元格的颜色。

交替行颜色,可以选择使用样式

这作为名为 AlternateRowBackgroundTGrid.Options 属性中的可预设 bool 项存在。默认颜色是浅灰色 ($FFEEEEEE)。要更改此颜色,您可以添加 TStyleBook 或右键单击网格并选择编辑自定义样式...编辑默认样式...然后更改gridstyle-alternatingrowbackgroundColor属性。以下是颜色更改为 Bisque 的示例:

enter image description here

OnDrawColumnCell 事件中的代码

这甚至会为网格的每个单元格调用,并提供对绘制单元格背景的完全控制。事件处理程序的 header 如下所示:

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);

还有示例图像:

enter image description here

文本的颜色如this answer所示

关于delphi - 如何更改 Delphi TGrid Firemonkey 组件中单元格的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43624260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com