我在 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
添加到本地变量。为其分配 Bounds
和 Inlfate()
的值。修改后的 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;
两种解决方案的网格看起来都是这样的:
要同时删除单元格之间的线条,请取消选中网格选项
中的ColLines
和RowLines
。
关于delphi - Fmx TStringGrid 行颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41854676/