1

I am having difficulty understanding what is going on in a FireMonkey owner-drawn grid I am trying to develop in Delphi 10.3.

I have set the Grid1.DefaultDrawing property to False and assigned the following event handler to 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;

On my first attempt, I hadn't explicitly set the Grid1.DefaultDrawing property, so it was True by default. However I assigned the event handler.

At some stage, I obtained some text being rendered in the grid cells, but it was very faint and in the wrong color. It looked as if the control had been painted with some semi-transparent background color after the text had been rendered, thereby changing the text color from the specified font color of black to pink.

This only happened if I removed the OnGetCell value event handler. When this handler was assigned to the grid, text was rendered automatically by the control, but not as I wanted it to appear, which was the reason why I want to override the automatic cell drawing with the custom OnDrawColumnCell event handler.

In my latest attempt, I set Grid1.DefaultDrawing set to False. I am finding that more or less the same code is producing no visible text whatsoever. However, when a cell is clicked, the correct text briefly appears in washed out colors, if an OnGetValue event handler has been assigned to the grid.

Can anyone suggest what might be preventing the text from being rendered by the OnDrawColumnCell event handler?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
user11935527
  • 101
  • 1
  • 7

1 Answers1

3

You are not setting the color of the text.

For example (just before Canvas.FillText():

  Canvas.Fill.Color := TAlphaColors.Black;

From the documentation for Canvas.FillText():

FillText is implemented by the TCanvas descendants to display a text string with the specified alignment, current font, and brush specified by the Fill and Font properties.

Tom Brunberg
  • 20,312
  • 8
  • 37
  • 54