listview排序支持

来源:互联网 发布:破解限时试用软件 编辑:程序博客网 时间:2024/05/09 19:32

//listview排序支持
function   CustomSortProc(Item1,Item2:TListItem;ParamSort:Integer):integer;stdcall;

var
  m_bSort:Boolean;//用于控制升序及降序的转换

implementation

function CustomSortProc(Item1, Item2: TListItem;
  ParamSort: Integer): integer;
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 TForm.lv1ColumnClick(Sender: TObject;
  Column: TListColumn);
begin
  Lv1.CustomSort(@CustomSortProc,Column.Index);
  m_bSort:=not m_bSort;
end;