1

我很难理解我试图在 Delphi 10.3 中开发的 FireMonkey 所有者绘制的网格中发生了什么。

我已将该Grid1.DefaultDrawing属性设置为 False 并将以下事件处理程序分配给Grid1.OnColumnCellDraw

procedure TFormFolderCptPairArray.Grid1DrawColumnCell1(Sender: TObject;
  const Canvas: TCanvas; const Column: TColumn; const Bounds: TRectF;
  const Row: Integer; const Value: TValue; const State: TGridDrawStates);
var
  Grid: TGrid;
  ColIndex, RowIndex: integer;
  CellStr: string;
  CellStringSize: TSizeF;
  CellStrPosn: TPointF;
  CellStrRect: TRectF;
begin
  {Retrieve Grid reference:}
  Grid:= Sender as TGrid;
  {Retrieve column and row indices:}
  ColIndex:= Column.Index;
  RowIndex:= Row;
  {Determine text to be drawn in cell:}
  GetGridCellText(ColIndex, RowIndex, CellStr);
  {Determine cell text position and bounds:}
  if CellStr<>'' then
    begin
      {Calculate size of cell string:}
      CellStringSize.cx:= Canvas.TextWidth(CellStr);
      CellStringSize.cy:= Canvas.TextHeight(CellStr);
      {Calculate posn of cell string:}
      if ColIndex=0 then
        begin
          {Align to centre}
          CellStrPosn.x:= (Bounds.Left + Bounds.Right - CellStringSize.cx) / 2;
        end
      else
        case (ColIndex-1) mod CFolderFieldCount of
          0:  CellStrPosn.x:= Bounds.Left + CCellMargin;    {Align to left}
          1..4: CellStrPosn.x:= (Bounds.Left +
                Bounds.Right - CellStringSize.cx) / 2 ;  {Align to centre}
          5:  CellStrPosn.x:= Bounds.Right - CCellMargin - CellStringSize.cx;
                           {Align to right}
        end;
      CellStrPosn.y:= (Bounds.Top + Bounds.Bottom - CellStringSize.cy) / 2;
      {Draw cell text:}
      {Calculate cell strings bounding rect:}
      CellStrRect.Left:= CellStrPosn.x;
      CellStrRect.Top:= CellStrPosn.y;
      CellStrRect.Right:= Bounds.Right - CCellMargin;
      CellStrRect.Bottom:= CellStrRect.Top +  CellStringSize.cy;
      Canvas.FillText(CellStrRect,  CellStr, True, 1.0, [], TTextAlign.Leading);
    end;
end;

在我第一次尝试时,我没有明确设置该Grid1.DefaultDrawing属性,因此默认情况下它是 True 。但是我分配了事件处理程序。

在某个阶段,我获得了一些在网格单元格中呈现的文本,但它非常模糊且颜色错误。看起来好像控件在文本呈现后被涂上了一些半透明的背景颜色,从而将文本颜色从指定的黑色字体颜色更改为粉红色。

仅当我删除了OnGetCellvalue 事件处理程序时才会发生这种情况。当将此处理程序分配给网格时,控件会自动呈现文本,但不是我希望它出现的那样,这就是我想用自定义OnDrawColumnCell事件处理程序覆盖自动单元格绘制的原因。

在我最近的尝试中,我设置Grid1.DefaultDrawing为 False。我发现或多或少相同的代码不会产生任何可见的文本。但是,当单击单元格时,如果OnGetValue已将事件处理程序分配给网格,则正确的文本会以褪色的颜色短暂显示。

任何人都可以建议什么可能会阻止OnDrawColumnCell事件处理程序呈现文本?

4

1 回答 1

3

您没有设置文本的颜色。

例如(就在之前Canvas.FillText()

  Canvas.Fill.Color := TAlphaColors.Black;

从文档中Canvas.FillText()

FillText 由 TCanvas 后代实现,以显示具有指定对齐方式、当前字体和由Fill和 Font 属性指定的画笔的文本字符串。

于 2019-08-19T12:24:55.603 回答