【问题标题】:Fmx TStringGrid row colorFmx TStringGrid 行颜色
【发布时间】:2017-06-10 19:34:15
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

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

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

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

    【讨论】:

    • 它在“InflateRect(aNewRectF, 3, 3);”上显示“E2033 实际和正式 var 参数的类型必须相同”
    • 嗯...它对我来说编译得很好。仔细检查您将 aNewRectF 声明为 TRectF(以“F”结尾)并尝试将常量专门写为浮点数,如 3.0
    • 是的 aNewRectF 和 TRectF,尝试改变值,使用浮点数,使用 const 但我总是有同样的错误(光标就在 InflateRect(aNewRectF,3,3) 的第一个参数之后)
    • 当您将鼠标光标放在InflateRect 上时,提示窗口显示的完全限定名称和参数是什么
    • 参数为TRect@,integer,integer
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-12
    • 2022-01-10
    • 2012-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-22
    相关资源
    最近更新 更多