要点:
1.代码备注的超级详细,如下:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls; type TForm1 = class(TForm) lv1: TListView; Button1: TButton; Button2: TButton; Button3: TButton; Button4: TButton; Button5: TButton; Button6: TButton; Button7: TButton; Button8: TButton; Button9: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); procedure Button5Click(Sender: TObject); procedure Button6Click(Sender: TObject); procedure Button7Click(Sender: TObject); procedure lv1CustomDrawItem(Sender: TCustomListView; Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean); procedure lv1ColumnClick(Sender: TObject; Column: TListColumn); procedure FormCreate(Sender: TObject); procedure Button8Click(Sender: TObject); procedure Button9Click(Sender: TObject); private public procedure 分组(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} {风格} procedure TForm1.Button1Click(Sender: TObject); begin lv1.Clear; //清空 lv1.Columns.Clear; //lv1.ViewStyle := lv1 {增加3项列表头} lv1.Columns.Add; lv1.Columns.Add; lv1.Columns.Add; lv1.Columns.Items[0].Caption:='时间'; lv1.Columns.Items[1].Caption:='地点'; lv1.Columns.Items[2].Caption:='人物'; lv1.Columns.Items[0].Width:=100; lv1.Columns.Items[1].Width:=100; lv1.Columns.Items[2].Width:=300; {设置属性} lv1.ViewStyle:=vsreport; //报表模式 lv1.GridLines:=true; //表格线 //lv1.RowSelect := True; //选中一整行 end; {} {插入数据方法1} procedure TForm1.Button2Click(Sender: TObject); begin with lv1.items.add do begin caption:='2013'; subitems.add('上海'); subitems.add('田攀'); end; end; {插入数据方法2} procedure TForm1.Button3Click(Sender: TObject); var Titem:Tlistitem;//存储临时记录. begin Titem := lv1.Items.add; Titem.Caption := '201314'; Titem.SubItems.Add('广州'); Titem.SubItems.Add('God'); end; {删除行} procedure TForm1.Button4Click(Sender: TObject); begin lv1.DeleteSelected; //删除选中行 lv1.Items.Item[2].Delete;//删除某一行 lv1.Items.Clear;//清除所有数据 end; {按标题内容删除} procedure TForm1.Button6Click(Sender: TObject); var I: Integer; begin for I := 0 to lv1.Items.Count - 1 do begin if lv1.Items[I].Caption = '田攀2013' then //如果标题为 '田攀2013' lv1.Items.Item[I].Delete();//删掉 end; end; {选择某行} procedure TForm1.Button7Click(Sender: TObject); begin lv1.SetFocus;//获取焦点 lv1.Items[lv1.Items.Count - 1].Selected := True;//选中 end; procedure TForm1.Button8Click(Sender: TObject); begin 分组(Sender); end; {分组效果} procedure TForm1.分组(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 := '2013'; 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; {初始化,TList分组} procedure TForm1.FormCreate(Sender: TObject); begin end; {显示和隐藏列表头} procedure TForm1.Button9Click(Sender: TObject); var IsShow : Boolean; begin IsShow := lv1.ShowColumnHeaders;//是否隐藏列表头 lv1.ShowColumnHeaders := not IsShow;//取反 end; {每2行交叉换颜色} procedure TForm1.lv1CustomDrawItem(Sender: TCustomListView; Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean); begin if Item.Index mod 2 = 0 then//如果序号是2的倍数 Sender.Canvas.Brush.Color :=clSkyBlue //蓝色 else Sender.Canvas.Brush.Color :=clWhite; //白色 end; {读取数据} procedure TForm1.Button5Click(Sender: TObject); begin ShowMessage(lv1.Items[2].Caption) ; //读取某条数据标题 ShowMessage(lv1.Items[2].SubItems.strings[1]) ;//读取第二行的第二子列(也就是第三列) ShowMessage(lv1.Items[2].SubItems.CommaText); //显示全部内容 end; {排列函数} function CustomSortProc(Item1, Item2: TListItem; ColumnIndex: integer): integer; stdcall; begin if ColumnIndex = 0 then //第0行 Result := CompareText(Item1.Caption,Item2.Caption) //返回 else Result := CompareText(Item1.SubItems[ColumnIndex-1],Item2.SubItems[ColumnIndex-1]); end; {点击每列头重新排列列表} procedure TForm1.lv1ColumnClick(Sender: TObject; Column: TListColumn); begin lv1.CustomSort(@CustomSortProc,Column.Index); //点击每行第一个排列 end; end.