ListView点击列排序

很想实现像Windows资源管理器在使用详细资料方式查看文件时,点击列标题自动排序的功能。看了Delphi6的帮助,基本的排序功能是实现了,但不能显示表示升/降序的“小三角”。因为技术有限,找不出实现列标题显示的代码 ,也不会重写显示“小三角”的代码,所以只好使用两个特殊符号中的箭头来表示,哪位高手能实现“小三角”的请指教指教!代码如下:

 

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls;

const
  AscText = '↑';
  DescText = '↓';
  
type
  TForm1 = class(TForm)
    ListView1: TListView;
    procedure ListView1Compare(Sender: TObject; Item1, Item2: TListItem;
      Data: Integer; var Compare: Integer);
    procedure ListView1ColumnClick(Sender: TObject; Column: TListColumn);
    procedure ListView1CustomDraw(Sender: TCustomListView;
      const ARect: TRect; var DefaultDraw: Boolean);
  private
    { Private declarations }
    ColumnToSort: Integer;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function GetSortType(S: String): Integer;
begin
  Result:= 0;
  if pos(AscText, S) > 0 then
    Result:= 1
  else if pos(DescText, S) > 0 then
    Result:= -1;
end;

procedure TForm1.ListView1Compare(Sender: TObject; Item1, Item2: TListItem;
  Data: Integer; var Compare: Integer);
var
  ix: Integer;
begin
  if GetSortType(Item1.ListView.Column[ColumnToSort].Caption) >= 0 then
  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
  else begin
    if ColumnToSort = 0 then
      Compare := CompareText(Item2.Caption,Item1.Caption)
    else begin
     ix := ColumnToSort - 1;
     Compare := CompareText(Item2.SubItems[ix],Item1.SubItems[ix]);
    end;
  end;
end;

procedure TForm1.ListView1ColumnClick(Sender: TObject;
  Column: TListColumn);
var
  i: Integer;
begin
  case GetSortType(Column.Caption) of
    -1: Column.Caption:= StringReplace(column.Caption, DescText, AscText, []);
    0: Column.Caption:= Column.Caption + AscText;
    1: Column.Caption:= StringReplace(column.Caption, AscText, DescText, []);
  end;
 
  ColumnToSort := Column.Index;
  (Sender as TCustomListView).AlphaSort;
end;

procedure TForm1.ListView1CustomDraw(Sender: TCustomListView;
  const ARect: TRect; var DefaultDraw: Boolean);
var
  i: Integer;
  List: TListView;
begin
  if Sender is TListView then
  begin
    List:= (Sender as TListView);
    for i:= 0 to List.Columns.Count -1 do
    begin
      if i <> ColumnToSort then
      begin
        List.Columns[i].Caption:=
          StringReplace(List.Columns[i].Caption, AscText, '', []);
        List.Columns[i].Caption:=
          StringReplace(List.Columns[i].Caption, DescText, '', []); 
      end;
    end;
  end;
end;

end.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值