唯棉坜 发表于 2025-6-5 08:27:24

Delphi 中禁止 TStringGrid 单元格被选中

Delphi 中禁止 TStringGrid 单元格被选中

环境

[*]Windows 11 23H2
[*]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. TheCol 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;上述代码的意图是把被选中区域设置在表格可视区域之外,使得表格中任意位置都不被渲染成被选中的样子。

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

宇文之 发表于 2025-10-23 05:35:25

懂技术并乐意极积无私分享的人越来越少。珍惜

捡嫌 发表于 2025-12-9 15:39:02

感谢分享,下载保存了,貌似很强大

觐有 发表于 2025-12-29 21:47:20

喜欢鼓捣这些软件,现在用得少,谢谢分享!

米榜饴 发表于 2025-12-30 05:26:05

不错,里面软件多更新就更好了

圣罩 发表于 2026-1-4 09:03:40

分享、互助 让互联网精神温暖你我

骆贵 发表于 2026-1-11 06:26:50

谢谢分享,辛苦了

搜娲瘠 发表于 2026-1-13 18:24:15

感谢分享,学习下。

村亢 发表于 2026-1-14 11:37:16

鼓励转贴优秀软件安全工具和文档!

乐敬 发表于 2026-1-16 19:02:06

谢谢楼主提供!

哈梨尔 发表于 2026-1-19 11:03:33

很好很强大我过来先占个楼 待编辑

埤兆 发表于 2026-1-19 20:22:45

收藏一下   不知道什么时候能用到

刃减胸 发表于 2026-1-20 01:09:27

感谢,下载保存了

锑砖 发表于 2026-1-21 14:55:54

不错,里面软件多更新就更好了

嗣伐 发表于 2026-1-21 21:03:47

过来提前占个楼

昆拗干 发表于 2026-1-25 08:59:16

这个好,看起来很实用

谧怏弦 发表于 2026-2-1 07:46:04

感谢,下载保存了

聱嘹 发表于 2026-2-4 04:00:43

这个有用。

翁谌缜 发表于 2026-2-5 18:39:58

过来提前占个楼

栓汨渎 发表于 2026-2-6 09:35:56

感谢,下载保存了
页: [1] 2
查看完整版本: Delphi 中禁止 TStringGrid 单元格被选中