LISTVIEW点击标题排序:
function CustomSortProc( Item1, Item2 : TListItem; lParam : LongInt ) : Integer; stdcall;
begin
if lParam >= 0 then //lParam中保存的是SubItem的Index
begin
result := -CompareText(Item1.SubItems.Strings[lParam],
Item1.SubItems.Strings[lParam] );
end else
result := 0;
end;在ListView的ColumnClick事件响应方法中输入
CustomSort(@CustomSortProc, Column.Index );
function CustomSortProc( Item1, Item2 : TListItem; lParam : LongInt ) : Integer; stdcall;
begin
if lParam >= 0 then //lParam中保存的是SubItem的Index
begin
result := -CompareText(Item1.SubItems.Strings[lParam],
Item1.SubItems.Strings[lParam] );
end else
result := 0;
end;在ListView的ColumnClick事件响应方法中输入
CustomSort(@CustomSortProc, Column.Index );
var ColumnToSort: Integer;The OnColumnClick event handler sets the global variable to indicate the column to sort and calls AlphaSort:procedure TForm1.ListView1ColumnClick(Sender: TObject; Column: TListColumn);begin
ColumnToSort := Column.Index;
(Sender as TCustomListView).AlphaSort;
end;
The OnCompare event handler causes the list view to sort on the selected column:procedure TForm1.ListView1Compare(Sender: TObject; Item1, Item2: TListItem; Data: Integer;
var Compare: Integer);
var
ix: Integer;
begin
if ColumnToSort = 0 then
Compare := CompareText(Item1.Caption,Item2.Caption)
else begin
ix := ColumnToSort - 1;
Compare := CompareText(Item1.SubItems[ix],Item2.SubItems[ix]);
end;end;