Canvas.FillText not working in TGrid.OnDrawColumnCell event handler


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);
Grid: TGrid;
ColIndex, RowIndex: integer;
CellStr: string;
CellStringSize: TSizeF;
CellStrPosn: TPointF;
CellStrRect: TRectF;
{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
{Calculate size of cell string:} Canvas.TextWidth(CellStr); Canvas.TextHeight(CellStr);
{Calculate posn of cell string:}
if ColIndex=0 then
{Align to centre}
CellStrPosn.x:= (Bounds.Left + Bounds.Right – / 2;
case (ColIndex-1) mod CFolderFieldCount of
0: CellStrPosn.x:= Bounds.Left + CCellMargin; {Align to left}
1..4: CellStrPosn.x:= (Bounds.Left +
Bounds.Right – / 2 ; {Align to centre}
5: CellStrPosn.x:= Bounds.Right – CCellMargin –;
{Align to right}
CellStrPosn.y:= (Bounds.Top + Bounds.Bottom – / 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 +;
Canvas.FillText(CellStrRect, CellStr, True, 1.0, [], TTextAlign.Leading);

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?

Comments are closed.