为TListView实现项目单击/双击

Delphi's TListView control displays a list of items in columns with column headers and sub-items, or vertically or horizontally, with small or large icons.

Delphi的 TListView控件在具有列标题和子项目的列中显示项目列表,或者在垂直或水平方向上显示带有小图标或大图标的项目列表。

As do most Delphi controls, the TListView exposes the OnClick and OnDblClick (OnDoubleClick) events.

与大多数Delphi控件一样,TListView公开OnClickOnDblClick (OnDoubleClick)事件。

Unfortunately, if you need to know what item was clicked or double clicked you cannot simply handle the OnClick / OnDblClick events to get the clicked item.

不幸的是,如果您需要知道单击或双击了哪个项目,则不能简单地处理OnClick / OnDblClick事件来获取被单击的项目。

The OnClick (OnDblClick) event for the TListView is fired whenever the user clicks the control - that is whenever the "click" occurs somewhere inside the client area of the control.

每当用户单击控件时,即在控件的客户区域内某处发生“单击”时,都会触发TListView的OnClick(OnDblClick)事件。

The user can click inside the list view, BUT "miss" any of the items. What's more, since list view can change its display depending on the ViewStyle property, the user might have clicked on an item,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Delphi 2010 的 TListView 组件本身不支持虚拟加载和分组,但可以通过继承 TListView 实现这些功能。以下是实现虚拟加载和分组的示例代码: ``` unit VirtualListView; interface uses ComCtrls; type TVirtualListViewItem = class(TListItem) private FLoaded: Boolean; public property Loaded: Boolean read FLoaded write FLoaded; end; TVirtualListViewGroup = class(TListGroup) private FLoaded: Boolean; public property Loaded: Boolean read FLoaded write FLoaded; end; TVirtualListView = class(TListView) private FVirtualItemCount: Integer; FVirtualItemHeight: Integer; FVirtualGroupCount: Integer; FVirtualGroupHeaderHeight: Integer; protected function CreateItem(AOwner: TListItem): TListItem; override; function CreateGroup(AOwner: TListGroup): TListGroup; override; procedure DoUpdateItems(FirstIndex, LastIndex: Integer); override; procedure DoUpdateGroups(FirstIndex, LastIndex: Integer); override; function GetItemCount: Integer; override; function GetItemHeight(AItem: TListItem): Integer; override; function GetGroupHeaderHeight: Integer; override; procedure SetVirtualItemCount(Value: Integer); procedure SetVirtualItemHeight(Value: Integer); procedure SetVirtualGroupCount(Value: Integer); procedure SetVirtualGroupHeaderHeight(Value: Integer); public constructor Create(AOwner: TComponent); override; property VirtualItemCount: Integer read FVirtualItemCount write SetVirtualItemCount; property VirtualItemHeight: Integer read FVirtualItemHeight write SetVirtualItemHeight; property VirtualGroupCount: Integer read FVirtualGroupCount write SetVirtualGroupCount; property VirtualGroupHeaderHeight: Integer read FVirtualGroupHeaderHeight write SetVirtualGroupHeaderHeight; end; implementation function TVirtualListView.CreateItem(AOwner: TListItem): TListItem; begin Result := TVirtualListViewItem.Create(Items); end; function TVirtualListView.CreateGroup(AOwner: TListGroup): TListGroup; begin Result := TVirtualListViewGroup.Create(Groups); end; procedure TVirtualListView.DoUpdateItems(FirstIndex, LastIndex: Integer); var I: Integer; begin for I := FirstIndex to LastIndex do begin if not Items[I].Loaded then begin Items[I].Loaded := True; // 加载项目数据 end; end; end; procedure TVirtualListView.DoUpdateGroups(FirstIndex, LastIndex: Integer); var I: Integer; begin for I := FirstIndex to LastIndex do begin if not Groups[I].Loaded then begin Groups[I].Loaded := True; // 加载分组数据 end; end; end; function TVirtualListView.GetItemCount: Integer; begin Result := FVirtualItemCount; end; function TVirtualListView.GetItemHeight(AItem: TListItem): Integer; begin Result := FVirtualItemHeight; end; function TVirtualListView.GetGroupHeaderHeight: Integer; begin Result := FVirtualGroupHeaderHeight; end; procedure TVirtualListView.SetVirtualItemCount(Value: Integer); begin FVirtualItemCount := Value; Items.Count := Value; end; procedure TVirtualListView.SetVirtualItemHeight(Value: Integer); begin FVirtualItemHeight := Value; end; procedure TVirtualListView.SetVirtualGroupCount(Value: Integer); begin FVirtualGroupCount := Value; Groups.Count := Value; end; procedure TVirtualListView.SetVirtualGroupHeaderHeight(Value: Integer); begin FVirtualGroupHeaderHeight := Value; end; constructor TVirtualListView.Create(AOwner: TComponent); begin inherited; OwnerData := True; end; end. ``` 在这个示例中,我们创建了两个新类:TVirtualListViewItem 和 TVirtualListViewGroup,它们分别继承自 TListItem 和 TListGroup,并增加了 Loaded 属性。这个属性用来标记项目或分组是否已经加载了数据。当项目或分组需要显示时,我们可以根据该属性来判断是否需要加载数据。 TVirtualListView 继承自 TListView,并重写了一些方法和属性。CreateItem 和 CreateGroup 方法分别创建 TVirtualListViewItem 和 TVirtualListViewGroup 对象。DoUpdateItems 和 DoUpdateGroups 方法分别在项目或分组需要显示时调用,用来加载数据。GetItemCount、GetItemHeight 和 GetGroupHeaderHeight 方法分别返回项目数、项目高度和分组标题高度。SetVirtualItemCount、SetVirtualItemHeight、SetVirtualGroupCount 和 SetVirtualGroupHeaderHeight 方法分别设置虚拟项目数、项目高度、虚拟分组数和分组标题高度。在 TVirtualListView 的构造函数中,我们将 OwnerData 属性设置为 True,这是启用虚拟模式的必要条件。 使用 TVirtualListView 时,可以像使用普通的 TListView 一样设置分组和项目,但需要设置虚拟属性。例如: ``` procedure TForm1.FormCreate(Sender: TObject); begin VirtualListView1.VirtualGroupCount := 100; VirtualListView1.Groups[0].Header := 'Group 1'; VirtualListView1.Groups[1].Header := 'Group 2'; VirtualListView1.VirtualItemCount := 1000; end; ``` 在上面的示例中,我们设置了虚拟分组数和虚拟项目数。我们还设置了前两个分组的标题。当需要显示分组或项目时,TVirtualListView 会自动调用 DoUpdateGroups 和 DoUpdateItems 方法,来加载数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值