Delphi 2010 TListView一些使用

        Delphi 2010的TListView扩展了一些功能,其中就有项分组功能,在XP和Vista以上系统有效。但是扩展的更多一些功能只对Vista系统有效。下面在XP SP3下实现TListView的分组效果:
1.新建一个应用程序,拖动一个 TListView到窗体上;
2.在窗体创建函数,写入以下代码:
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
procedure TForm1.FormCreate(Sender: TObject); 
begin 
  with lv1 do 
  begin 
    AllocBy := 0;                                //设置总共的项数量,省去每次添加开辟内存空间 
    Checkboxes := False;                         //项左边出现复选框,vsList or vsReport有效 
    Color := clWindow;                           //背景颜色 
    ColumnClick := True;                         //列头能否点击 
    with Columns.Add do                          //增加列 
    begin 
      Alignment := taLeftJustify;                //左对齐 
      Caption := '列一'
      ImageIndex := -1
      Width := 100
    end
    with Columns.Add do                          //增加列 
    begin 
      Caption := '列二'
      ImageIndex := -1
      Width := 50
    end
    Ctl3D := True; 
    DoubleBuffered := False;                     //双缓冲 
    Enabled := True; 
    FlatScrollBars := False;                     //平滑滚动条 
    FullDrag := False;                           //允许拖动列头 
    GridLines := False;                          //表格线 
    GroupHeaderImages := nil;                    //分组头关联图像列表 
    with Groups.Add do                           //增加分组 
    begin 
      BottomDescription := '底部的说明文字'
      ExtendedImage := -1;                       //关联 GroupHeaderImages图像列表,only on Windows Vista 
      Footer := '页脚文本'
      FooterAlign := taLeftJustify;              //页脚文本对齐 
      GroupID := 0;                              //组ID号 
      Header := '页首文本'
      HeaderAlign := taLeftJustify; 
      State := [                                 //分组状态,一些状态只应用于VISTA系统 
                lgsNormal,                       //所有分组展开 
                lgsHidden,                       //分组隐藏 
                lgsCollapsed,                    //分组折叠 Windows Vista only. 
                lgsNoHeader,                     //页首不可见 Windows Vista only. 
                lgsCollapsible,                  //分组可折叠 Windows Vista only. 
                lgsFocused,                      //分组有键盘焦点 Windows Vista only. 
                lgsSelected,                     //分组被选择 Windows Vista only. 
                lgsSubseted,                     //只有分组的一个子集显示出来 Windows Vista only. 
                lgsSubSetLinkFocused             //分组的子集有键盘焦点  Windows Vista only. 
               ]; 
       SubsetTitle := '子集标题'
       Subtitle := '子标题'
       TitleImage := -1;                         //关联 GroupHeaderImages图像列表,only on Windows Vista 
       TopDescription := '顶部的说明文字'
    end
    with Groups.Add do 
    begin 
      GroupID := 1
      Header := '分组标题'
    end
    GroupView := True;                            //打开或关闭分组视图 
    HideSelection := True;                        //失去焦点时,项不再保持被选择状态 
    HotTrack := False;                            //指定是否鼠标移过项进行高亮 
    HotTrackStyles := 
                   //  htHandPoint,               //手势 
                   //  htUnderlineCold,           //非热点下划线 
                   //  htUnderlineHot             //下划线热点 
                      ]; 
    HoverTime := -1;                              //鼠标在项上暂停时间,除非HotTrack为True 
    with IconOptions do                           //确定如何排列图标,vsIcon or vsSmallIcons 有效 
    begin 
      Arrangement := iaTop;                       //项在顶部从左到右对齐,iaLeft在左部从上到下对齐 
      AutoArrange := False;                       //图标自动重新排列 
      WrapText := True;                           //图标标题是否折行 
    end
    with Items.Add do                             //增加项 
    begin 
      Caption := '行一列一'
      ImageIndex := -1;                           //关联 LargeImages or SmallImages图像列表 
      StateIndex := -1;                           //关联StateImages图像列表 
      GroupID := 0;                               //关联分组ID号 
      SubItems.Add('行一列二');                   //添加第二列 
    end
    with Items.Add do 
    begin 
      Caption := '行二列一'
      GroupID := 1
      SubItems.Add('行二列二'); 
    end
    LargeImages := nil;                            //大图标图像列表 
    MultiSelect := False;                          //多选 
    OwnerData := False;                            //指定列表视图控件是否是虚拟的 
    OwnerDraw := False;                            //自绘项 
    ParentColor := False;                          //继承父控件颜色 
    ReadOnly := False;                             //只读 
    RowSelect := False;                            //整行选择 
    ShowColumnHeaders := True;                     //显示列头,vsReport有效 
    ShowWorkAreas := False;                        //显示工作区,vsIcon or vsSmallIcon有效,不支持 OwnerData 
    SmallImages := nil;                            //小图标图像列表 
    SortType := stNone;                            //确定列表项如何自动排序 
    StateImages := nil;                            //状态图像列表 
    ViewStyle := vsReport;                         //视图风格,vsIcon、vsSmallIcon、vsList、vsReport 
  end
end
3.运行结果如下所示:

4.拖动一个TImageList到窗体上,添加一些图标到TImageList上,使TListView所有可以关联图像列表的都关联到此TImageList上,然后分别设置图像索引的不同,运行结果如下图所示:

Windows 7下效果分别如下:

设定隐藏的分组在Windows 7可以成功隐藏起来。


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值