Delphi7中Listview的常用功能汇总
有些时候我们在使用Delphi7的Listview过程中总是要改一些默认的设置,现在把它们集中起来汇总如下。
MultiSelect:=True; 使Listview可以同时选择多行
GridLines:=True; 使Listview显示格线
ViewStyle:=vsReport;显示数据项的详细列表
HideSelection:=True; 使listview失去焦点时,选中行不高亮
//设置颜色 procedureTForm1.ListView1CustomDrawItem(Sender:TCustomListView; Item:TListItem;State:TCustomDrawState;varDefaultDraw:Boolean); var subRect,itemRect:TRect; i,SubItem:Integer; begin DefaultDraw:=False; ifItem.Selectedthenbegin Sender.Canvas.Font.Color:=clRed;//选中行字体颜色 Sender.Canvas.Brush.Color:=clgray;//clGreen;选中行高亮颜色 endelsebegin Sender.Canvas.Font.Color:=clNavy;//正常行字体颜色 Sender.Canvas.Brush.Color:=clWhite;//正常行高亮颜色 end; itemRect:=Item.DisplayRect(drLabel); subRect:=itemRect; forSubItem:=0to(SenderasTListView).Columns.Count-1do begin subRect.Left:=itemRect.Left; fori:=1toSubItemdo begin subRect.Left:=subRect.Left+(SenderasTListView).Column[i-1].Width; subRect.Right:=subRect.Right+SubRect.Left+(SenderasTListView).Column[i].Width; end; ifSubItem=0then begin subRect.Right:=subRect.Right+2; Sender.Canvas.TextRect(subRect,subRect.Left,subRect.Top,Item.Caption); endelse Sender.Canvas.TextRect(subRect,subRect.Left,subRect.Top,Item.SubItems[SubItem-1]); end; end;
//排序功能 private {Privatedeclarations} SortCol:Integer; SortWay:Integer; procedureTForm1.ListView1ColumnClick(Sender:TObject; Column:TListColumn); begin SortCol:=Column.Index; if(SortWay=1)thenSortWay:=-1elseSortWay:=1; (SenderasTCustomListView).AlphaSort; end; procedureTForm1.ListView1Compare(Sender:TObject;Item1,Item2:TListItem; Data:Integer;varCompare:Integer); var t:Integer; begin if(SortCol=0)then begin Compare:=SortWay*CompareText(Item1.Caption,Item2.Caption); endelse begin t:=SortCol-1; Compare:=SortWay*CompareText(Item1.SubItems[t],Item2.SubItems[t]); end; end;
这个功能存在一个问题:数字排序会按字符类似排,例如:1,10,102,3,34,356......感兴趣的读者可以加以完善