以下是我程序中的一段代码,实现按下Column时按指定的列升序和降序排列将将ListView的SortType属性改为stData var m_bSort : boolean;//用于控制升序及降序的转换 //.................function CustomSortProc(Item1, Item2: TListItem; ParamSort: integer): integer; stdcall; var txt1,txt2 : string; begin if ParamSort <> 0 then begin txt1 := Item1.SubItems.Strings[ParamSort - 1]; txt2 := Item2.SubItems.Strings[ParamSort - 1]; if m_bSort then begin Result := CompareText(txt1,txt2); end else begin Result := -CompareText(txt1,txt2); end; end else begin if m_bSort then begin Result := CompareText(Item1.Caption,Item2.Caption); end else begin Result := -CompareText(Item1.Caption,Item2.Caption); end; end; end;procedure TForm1.ListView1ColumnClick(Sender: TObject; Column: TListColumn); begin ListView1.CustomSort(@CustomSortProc, Column.Index); m_bSort := not m_bSort; end;
Item的参数通过 function CustomSortProc(Item1, Item2: TListItem; ParamSort: integer): integer; stdcall;的ParamSort参数传递。
var m_bSort : boolean;//用于控制升序及降序的转换
//.................function CustomSortProc(Item1, Item2: TListItem; ParamSort: integer): integer; stdcall;
var txt1,txt2 : string;
begin
if ParamSort <> 0 then begin
txt1 := Item1.SubItems.Strings[ParamSort - 1];
txt2 := Item2.SubItems.Strings[ParamSort - 1];
if m_bSort then begin
Result := CompareText(txt1,txt2);
end else begin
Result := -CompareText(txt1,txt2);
end;
end else begin
if m_bSort then begin
Result := CompareText(Item1.Caption,Item2.Caption);
end else begin
Result := -CompareText(Item1.Caption,Item2.Caption);
end;
end;
end;procedure TForm1.ListView1ColumnClick(Sender: TObject;
Column: TListColumn);
begin
ListView1.CustomSort(@CustomSortProc, Column.Index);
m_bSort := not m_bSort;
end;
function CustomSortProc(Item1, Item2: TListItem; ParamSort: integer): integer; stdcall;的ParamSort参数传递。