繁体   English   中英

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

[英]How to change color of a cell in a Delphi TGrid Firemonkey component?

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

有没有人成功地改变了单元格(或其他组件)的背景颜色?

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

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

这作为可预设的布尔项存在于名为AlternateRowBackgroundTGrid.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);

和示例图像:

在此处输入图像描述

文本的颜色与此答案相同

请看这个例子。 评论中的解释:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM