ListView排序的时候上面怎么样出现一个小三角的图标
在listview排序的时候,怎样能够在排序标题上出现一个表示排序的
三角图标?(明天结贴)
我看看下面的代码有什么问题:
首先声明两个全局变量来保存被单据的列的索引号和当前索引状态(ASC或DESC)
Image1,Image2分别放两个图标表示当前索引状态(ASC或DESC)
在你的单元接口段的private下声明:
columntosort:integer;
isascsort:boolean;
在ListView的ColumnClick事件里给columntosort赋值(被单击列的索引号),调用AlphaSort:
procedure TForm1.ListView1ColumnClick(Sender: TObject;
Column: TListColumn);
begin
isascsort:=not isascsort;
columntosort:=column.Index;
(sender as tcustomlistview).AlphaSort;
SetColumnBitmap(ListView1,columntosort);
end;
然后在ListView的Compare事件里写排序代码:
procedure TForm1.ListView1Compare(Sender: TObject; Item1, Item2: TListItem;
Data: Integer; var Compare: Integer);
var
xx:integer;
begin
if columntosort=0 then//按标题列排序;
if isascsort then
compare:=comparetext(item1.Caption,item2.Caption)
else
compare:=comparetext(item2.Caption,item1.Caption)
else//按SubItems排序
begin
xx:=columntosort-1;
if isascsort then
compare:=comparetext(item1.SubItems[xx],item2.SubItems[xx])
else
compare:=comparetext(item2.SubItems[xx],item1.SubItems[xx]);
end;
end;
procedure TForm1.SetColumnBitmap(ListView: TListView;
SortIndex: integer);
const
HDF_SORTUP = $0400;
HDF_SORTDOWN = $0200;
var
OwnerDrawMode : boolean;
i : Integer; HdItem : THdItem;
AscendingBitmap ,DescendingBitmap : TBitmap;
begin
AscendingBitmap :=Image1.Picture.Bitmap;
DescendingBitmap:=Image2.Picture.Bitmap;
with ListView do if HandleAllocated and ShowColumnHeaders then
begin
OwnerDrawMode := not ((AscendingBitmap=nil) or (DescendingBitmap=nil));
if OwnerDrawMode then
if isascsort then
HdItem.hbm := AscendingBitmap.Handle
else
HdItem.hbm := DescendingBitmap.Handle;
for i := 0 to Columns.Count-1 do
begin
HdItem.Mask := HDI_FORMAT;
Header_GetItem(GetDlgItem(Handle, 0), i, HdItem);
if OwnerDrawMode then
begin
HdItem.Mask := HDI_BITMAP or HDI_FORMAT;
if SortIndex = i then
begin
HdItem.fmt := HdItem.fmt or HDF_BITMAP or HDF_BITMAP_ON_RIGHT;
end
else HdItem.fmt := HdItem.fmt and (not HDF_BITMAP);
end
else
begin
HdItem.fmt := ((HdItem.fmt and HDF_JUSTIFYMASK) and not HDF_OWNERDRAW) or HDF_STRING;
if SortIndex = i then
begin
if isascsort then
HdItem.fmt := HdItem.fmt or HDF_SORTUP
else
HdItem.fmt := HdItem.fmt or HDF_SORTDOWN;
end
else
HdItem.fmt := HdItem.fmt and (not HDF_SORTUP) and (not HDF_SORTDOWN);
end;
Header_SetItem(GetDlgItem(Handle, 0), i, HDItem); // set column data
end;
end;
end;