Delphi 中禁止 FMX 框架的 TStringGrid 单元格被选中

Delphi 中禁止 FMX 框架的 TStringGrid 单元格被选中

环境

  1. Windows 11 23H2
  2. Delphi 12 Update 1
  3. Multi-Device Application

使用 Delphi 中 FMX 框架的 TStringGrid 展示数据而不愿意某个单元格被选中时,曾经的 VCL 手段是把选中位置调整到无效位置从而实际上使得单元格无法被选中。阅读文档偶然发现 OnSelectCell 事件提供了很简单也更规范的方法实现了这一目的。

procedure TFrom.StrGrdSelectCell(Sender: TObject; const ACol, ARow: Integer; var CanSelect: Boolean);
begin
  CanSelect := False;
end;

在 FMX 框架中测试此方法有效。在 VCL 代码中观察到使用此方法的效果不理想。设置CanSelect := False后在程序启动时依然有某个单元格处于被选中的渲染状态。

文档地址: Vcl.Grids.TCustomDrawGrid.OnSelectCell

Occurs before a cell in the grid is selected.
Write an OnSelectCell event handler to specify whether any particular cell in the grid can be selected. The Col and Row parameters indicate the column and row indexes of the cell that is about to be selected. Set the CanSelect parameter to False to prevent the cell being selected.