网站首页 全球最实用的IT互联网站!

人工智能P2P分享Wind搜索发布信息网站地图标签大全

当前位置:诺佳网 > 软件工程 > 后端开发 > Pascal/Delphi >

Delphi 中禁止 TStringGrid 单元格被选中

时间:2024-10-16 16:38

人气:

作者:admin

标签:

导读:Delphi 中禁止 TStringGrid 单元格被选中 环境 Windows 11 23H2 Delphi 12 Update 1 FXM 框架 使用 Delphi 中 FMX 框架的 TStringGrid 展示数据而不愿意某个单元格被选中时,OnSelectCell 事件提供了很简单的方法...

Delphi 中禁止 TStringGrid 单元格被选中

环境

  1. Windows 11 23H2
  2. Delphi 12 Update 1

FXM 框架

使用 Delphi 中 FMX 框架的 TStringGrid 展示数据而不愿意某个单元格被选中时,OnSelectCell 事件提供了很简单的方法实现了这一目的。

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

文档地址: 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.

VCL

在 VCL 程序中,CanSelect := False 的效果并不好。虽然确实无法选中单元格,但是 StringGrid 最初被渲染,依然有一个单元格看起来像是被选中了的样子,比如被渲染成浅蓝色。
VCL 程序,除了利用 OnSelectCell 事件外,需要使用 StringGrid 的 Selection 属性完善不被选中的效果:

procedure TForm.StrGrdDrawCell(Sender: TObject; ACol, ARow: LongInt; Rect: TRect; State: TGridDrawState);
var
  SelectRect: TGridRect;
begin
  with SelectRect do
  begin
    Left := -1;
    Top := -1;
    Right := -1;
    Bottom := -1;
  end;
  StrGrd.Selection := SelectRect;
  { ***
  other code ...
  }
end;

上述代码的意图是把被选中区域设置在表格可视区域之外,使得表格中任意位置都不被渲染成被选中的样子。

温馨提示:以上内容整理于网络,仅供参考,如果对您有帮助,留下您的阅读感言吧!
相关阅读
本类排行
相关标签
本类推荐

CPU | 内存 | 硬盘 | 显卡 | 显示器 | 主板 | 电源 | 键鼠 | 网站地图

Copyright © 2025-2035 诺佳网 版权所有 备案号:赣ICP备2025066733号
本站资料均来源互联网收集整理,作品版权归作者所有,如果侵犯了您的版权,请跟我们联系。

关注微信