2

我在多设备应用程序(在 Windows 上)中的 Delphi 10.1 中遇到问题。我有一个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 中我从来没有遇到过这个问题,而且我不知道如何修复它。谢谢你。

4

1 回答 1

4

解决方案 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;

两种解决方案的网格如下所示:

在此处输入图像描述

要同时删除单元格之间的线条,请取消勾选ColLinesRowLinesin the grids Options

于 2017-01-25T16:01:45.223 回答