FMX TStringGrid行颜色 [英] Fmx TStringGrid row color
问题描述
在Delphi 10.1中,在多设备应用程序中(在Windows上)出现问题.
我有一个StringGrid
(连接到数据库),可以更改行的背景颜色,但是问题是单元格之间存在填充"(灰色/银色).
I have a problem in Delphi 10.1 in a multi-device application (on Windows).
I have a StringGrid
(connected to a db) and I can change background color of row, but the problem is there is a "padding" (in grey/silver) between cells.
在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中,我从来没有遇到过这个问题,而且我也不知道如何解决它. 谢谢.
In Delphi 6 I've never had this problem, and I don't know how to fix-it. Thank you.
推荐答案
解决方案1(在设计时):
对于每个StringColumn
,找到Padding
属性,并将所有值从3更改为0.
For each StringColumn
locate the Padding
property and change all values from 3 to 0.
解决方案2(在运行时):
将TRectF
添加到本地变量.为它分配Bounds
和Inlfate()
的值.修改后的OnDrawColumnCell()
看起来像这样:
Add a TRectF
to local vars. Assign it the value of Bounds
and Inlfate()
it. The modified OnDrawColumnCell()
looks like this:
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;
两种解决方案的网格看起来都是这样的:
The grid looks like this with both solutions:
要同时删除单元格之间的线,请取消选中网格Options
中的ColLines
和RowLines
.
To also remove the lines between the cells, untick ColLines
and RowLines
in the grids Options
.
这篇关于FMX TStringGrid行颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!