⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unsort.pas

📁 本产品是开发LISTVIEW相关功能的可定制产品
💻 PAS
字号:
{**********************************************************************
                 LISTVIEW排序相关方法
代码名称:   LISTVIEW排序
编译工具:  Delphi 6.0
作者:      邓普德
版权:      成都四方信息技术有限公司
定义时间:  2006-08-02
修改时间:  2006-08-06
**********************************************************************}

unit unSort;

interface

uses
  comctrls, Sysutils,ExtCtrls;
  
type
  TSortStyle = (ssAlpha,ssNumeric,ssDateTime);
  {
    ssAlpha: 按照字符进行排序
    ssNumeric: 按照数字进行排序
    ssDateTime: 按照时间进行排序
  }
  
  PSortInfo = ^TSortInfo;
  TSortInfo = record
      Col     : Integer;
      Style   : TSortStyle;
      Asc     : Boolean;
  end;

procedure SortListView(ListView:TListView; ColumnIndex:Integer;
                       Style: TSortStyle; Ascending: Boolean);

//设置ListView中的CheckBox()选择框的通用方法
procedure ExecSetLVItems(
  var vListView: TListView;
  rgSelect: TRadioGroup;
  strSource:String;
  cmpType:Integer);

  //根据条件设置ListView中的CheckBox()选择框的通用方法
procedure SetLVItemsByCondition(
  var vListView:TListView;
  strTarget,strSource:String;
  cmpType:Integer;indexListView:Integer);

implementation

function  ListViewCompare(I1, I2: TListItem; Data: Integer): Integer; stdcall;
var
    V1, V2: string;

    function    Sign(Val: Extended): Integer;//判断是大于/小于/等于0
    begin
        if Val < 0 then
            Result := -1
        else if Val > 0 then
            Result := 1
        else
            Result := 0;
    end;

    function    ExtractNum(const S: string): string;//获取字符串中前面的数字部分
    var
        i, j: Integer;
    begin
        j := 0;
        for i := 1 to Length(S) do
            if S[i] in ['0'..'9'] then
                Inc(j)
            else
                Break;
        if j = 0 then
            Result := '0'
        else
            Result := Copy(S,1,j);
    end;
begin
    with PSortInfo(Data)^ do
    begin
        if Col = 0 then
        begin
            V1 := I1.Caption;
            V2 := I2.Caption;
        end else
        begin
            V1 := I1.SubItems[Col-1];
            V2 := I2.SubItems[Col-1];
        end;
        case Style of
            ssAlpha     : Result := AnsiCompareText(V1,V2);
            ssNumeric   : Result := Sign(StrToFloat(ExtractNum(V1))-StrToFloat(ExtractNum(V2)));
            ssDateTime  : Result := Sign(StrToDateTime(V1) - StrToDateTime(V2));
        else
            Result := 0;
        end;
        if not Asc then
            Result := -Result;
    end;
end;

procedure SortListView(ListView:TListView; ColumnIndex:Integer;
                       Style: TSortStyle; Ascending: Boolean);
{排序ListView,ColumnIndex:排序列索引号,
 Style 排序方式:按字符,按数值,按日期(日期格式为
 SysUtils.ShortDataTimeFmt,缺省为YY-MM-DD);
 Ascending:=True按升序,否则按降序}
var
  FSortInfo:TSortInfo;
begin
    FSortInfo.Col   := ColumnIndex;
    FSortInfo.Style := Style;
    FSortInfo.Asc   := Ascending;
    try
      ListView.CustomSort(@ListViewCompare,LongInt(@FSortInfo));
    except

    end;
end;

//根据条件设置ListView中的CheckBox()选择框的通用方法
procedure SetLVItemsByCondition(
  var vListView:TListView;
  strTarget,strSource:String;
  cmpType:Integer;indexListView:Integer);
var
  ps:Integer;
begin
  Ps := pos(strTarget,strSource);
  if Ps =0 then
  begin
    vListView.Items[indexListView].Checked := false;
    Exit;
  end;
  Case cmpType of
    0:
    begin
      vListView.Items[indexListView].Checked := (ps = 1);
    end;
    1:
    begin
      vListView.Items[indexListView].Checked :=
        (strTarget = Copy(strSource,
                          Length(strSource) - Length(strTarget) + 1,
                          Length(strTarget)));
    end;
    2:
    begin
      vListView.Items[indexListView].Checked := true;
    end;
  end;
end;

procedure ExecSetLVItems(
  var vListView: TListView;
  rgSelect: TRadioGroup;
  strSource:String;
  cmpType:Integer);
var
  I:Integer;
begin
  if rgSelect.ItemIndex = 0 then
  begin
    For I := 0 to vListView.Items.Count -1 do
    begin
      SetLVItemsByCondition(vListView,
        strSource,vListView.Items[I].Caption,cmpType,I);
    end;
  end
  else if rgSelect.ItemIndex = 1 then
  begin
    For I := 0 to vListView.Items.Count -1 do
    begin
      SetLVItemsByCondition(vListView,
        strSource,vListView.Items[I].SubItems[0],cmpType,I);
    end;
  end
  else if rgSelect.ItemIndex = 2 then
  begin
    For I := 0 to vListView.Items.Count -1 do
    begin
      SetLVItemsByCondition(vListView,
        strSource,vListView.Items[I].SubItems[1],cmpType,I);
    end;
  end;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -