delphi - Fmx TStringGrid 行颜色

标签 delphi background-color firemonkey delphi-10.1-berlin stringgrid

我在 Delphi 10.1 的多设备应用程序(在 Windows 上)中遇到问题。 我有一个 StringGrid (连接到数据库),我可以更改行的背景颜色,但问题是单元格之间有“填充”(灰色/银色)。

onformCreate中我定义:

stringgrid1.DefaultDrawing := False;

这是我的代码:

procedure Tlist_form.StringGrid1DrawColumnCell(Sender: TObject;
  const Canvas: TCanvas; const Column: TColumn; const Bounds: TRectF;
  const Row: Integer; const Value: TValue; const State: TGridDrawStates);
   var aRowColor: TBrush;
begin
  aRowColor := Tbrush.Create(TBrushKind.Solid,TAlphaColors.Alpha);


  if (stringgrid1.Cells[7,row]='1') then 
        aRowColor.Color := TAlphaColors.Green
    else
      aRowColor.Color := TAlphaColors.Red;

    Canvas.FillRect(Bounds, 0, 0, [], 1, aRowColor);
    Column.DefaultDrawCell(Canvas, Bounds, Row, Value, State);

  aRowColor.free;

end; 

在 Delphi 6 中我从未遇到过这个问题,而且我不知道如何修复它。 谢谢。

最佳答案

解决方案 1(设计时):

对于每个 StringColumn 找到 Padding 属性并将所有值从 3 更改为 0。

解决方案 2(在运行时):

TRectF 添加到本地变量。为其分配 BoundsInlfate() 的值。修改后的 OnDrawColumnCell() 如下所示:

procedure TForm30.StringGrid1DrawColumnCell(Sender: TObject;
  const Canvas: TCanvas; const Column: TColumn; const Bounds: TRectF;
  const Row: Integer; const Value: TValue; const State: TGridDrawStates);
var
  aRowColor: TBrush;
  aNewRectF: TRectF;
begin
  aRowColor := TBrush.Create(TBrushKind.Solid, TAlphaColors.Alpha);

  if (StringGrid1.Cells[7, Row] = '1') then
    aRowColor.Color := TAlphaColors.Green
  else
    aRowColor.Color := TAlphaColors.Red;

  aNewRectF := Bounds;
  aNewRectF.Inflate(3, 3);
  Canvas.FillRect(aNewRectF, 0, 0, [], 1, aRowColor);
  Column.DefaultDrawCell(Canvas, Bounds, Row, Value, State);

  aRowColor.free;
end;

两种解决方案的网格看起来都是这样的:

enter image description here

要同时删除单元格之间的线条,请取消选中网格选项中的ColLinesRowLines

关于delphi - Fmx TStringGrid 行颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41854676/

相关文章:

Delphi FMX 验证 EditText 是否仅包含数字?

delphi - 为什么我的单选按钮不透明?

Delphi ReadProcessMemory问题

delphi - 使 TChromium 渲染抗锯齿

delphi - DataSnap XE2 服务器 - 使用 GZIP 压缩?

css - 如何修复背景颜色?

delphi - 用于 MacOS 的 Delphi 的 OutputDebugString()

ios - 更改背景中阴影的颜色

delphi - Firemonkey相当于ActionList?