我很难理解我试图在 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 。但是我分配了事件处理程序。
在某个阶段,我获得了一些在网格单元格中呈现的文本,但它非常模糊且颜色错误。看起来好像控件在文本呈现后被涂上了一些半透明的背景颜色,从而将文本颜色从指定的黑色字体颜色更改为粉红色。
仅当我删除了OnGetCell
value 事件处理程序时才会发生这种情况。当将此处理程序分配给网格时,控件会自动呈现文本,但不是我希望它出现的那样,这就是我想用自定义OnDrawColumnCell
事件处理程序覆盖自动单元格绘制的原因。
在我最近的尝试中,我设置Grid1.DefaultDrawing
为 False。我发现或多或少相同的代码不会产生任何可见的文本。但是,当单击单元格时,如果OnGetValue
已将事件处理程序分配给网格,则正确的文本会以褪色的颜色短暂显示。
任何人都可以建议什么可能会阻止OnDrawColumnCell
事件处理程序呈现文本?