在TFileListBox的子类里重写ReadFileNames那个虚方法(见filectrl.pas),根据文件创建时间进行排序。

原文出自:http://topic.csdn.net/t/20061003/15/5061795.html

在TFileListBox的子类里重写ReadFileNames那个虚方法(见filectrl.pas),根据TSearchRec.Time进行排序。

unit   Unit1;

interface

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

type
       TForm1   =   class(TForm)
             Button1:   TButton;
             procedure   Button1Click(Sender:   TObject);
       private
             {   Private   declarations   }
       public
             {   Public   declarations   }
       end;

       TMyFileListBox   =   class(TFileListBox)
          public
                constructor   Create(AOwner:   TComponent);   override;
          protected
                procedure   ReadFileNames;override;
          end;

var
       Form1:   TForm1;

implementation

{$R   *.dfm}


{   TMyFileListBox   }

constructor   TMyFileListBox.Create(AOwner:   TComponent);
begin
       inherited;
       Sorted   :=   False;   //禁止它自动排序
end;

procedure   TMyFileListBox.ReadFileNames;
var
       AttrIndex:   TFileAttr;
       I:   Integer;
       FileExt:   string;
       MaskPtr:   PChar;
       Ptr:   PChar;
       AttrWord:   Word;
       FileInfo:   TSearchRec;
       SaveCursor:   TCursor;
       Glyph:   TBitmap;
       DateList   :TStringList;   //时间列表
const
Attributes: array[TFileAttr] of Word = (faReadOnly, faHidden, faSysFile,
          faVolumeID,   faDirectory,   faArchive,   0);

       procedure   QuickSort(L,   R:   Integer);   //快速排序
     var
             I,   J:   Integer;
             P:   Integer;
       begin
             I   :=   L;
             J   :=   R;
             P   :=   StrToInt(DateList[(L   +   R)   shr   1]);
             repeat
                   while   StrToInt(DateList[I])    <   P      do   Inc(I);
                   while   StrToInt(DateList[J])   >    P      do   Dec(J);
                   if   I    <=   J   then
                         begin
                               Items.Exchange(I,   J);
                               DateList.Exchange(I,   J);
                               Inc(I);
                               Dec(J);
                         end;
             until   I   >    J;
                if   L    <   J   then
                         QuickSort(L,   J);
                if   I    <   R   then
                         QuickSort(I,   R);
       end;

begin
       DateList   :=   TStringList.Create;
       AttrWord   :=   DDL_READWRITE;
       if   HandleAllocated   then
       begin
             for   AttrIndex   :=   ftReadOnly   to   ftArchive   do
                   if   AttrIndex   in   FileType   then
                         AttrWord   :=   AttrWord   or   Attributes[AttrIndex];
             ChDir(FDirectory);
             Clear;
             I   :=   0;
             SaveCursor   :=   Screen.Cursor;
             try
                   MaskPtr   :=   PChar(FMask);
                   while   MaskPtr    <>    nil   do
                   begin
                         Ptr   :=   StrScan   (MaskPtr,    '; ');
                         if   Ptr    <>    nil   then
                               Ptr^   :=   #0;
                         if   FindFirst(MaskPtr,   AttrWord,   FileInfo)   =   0   then
                         begin
                               repeat
                                     if   (ftNormal   in   FileType)   or   (FileInfo.Attr   and   AttrWord    <>    0)   then
                                           if   FileInfo.Attr   and   faDirectory    <>    0   then
                                           begin
                                                 I   :=   Items.Add(Format( '[%s] ',[FileInfo.Name]));
                                                 if   ShowGlyphs   then
                                                       Items.Objects[I]   :=   DirBMP;
                                           end
                                           else
                                           begin
                                                 FileExt   :=   AnsiLowerCase(ExtractFileExt(FileInfo.Name));
                                                 Glyph   :=   UnknownBMP;
                                                 if   (FileExt   =    '.exe ')   or   (FileExt   =    '.com ')   or
                                                       (FileExt   =    '.bat ')   or   (FileExt   =    '.pif ')   then
                                                       Glyph   :=   ExeBMP;
                                              I   :=   Items.AddObject(FileInfo.Name,   Glyph);
                                              DateList.Append(IntToStr(FileInfo.Time));
                                           end;
                                     if   I   =   100   then
                                           Screen.Cursor   :=   crHourGlass;
                               until   FindNext(FileInfo)    <>    0;
                               FindClose(FileInfo);
                         end;
                         if   Ptr    <>    nil   then
                         begin
                               Ptr^   :=    '; ';
                               Inc   (Ptr);
                         end;
                         MaskPtr   :=   Ptr;
                   end; 
                  if Items.Count>0 then//原文中没有这句,当filelistbox的条目为0时,count-1会溢出

                    QuickSort(0,Items.Count   -   1   );
             finally
                   Screen.Cursor   :=   SaveCursor;
                   DateList.Free;
             end;
             Change;
       end;
end;

procedure   TForm1.Button1Click(Sender:   TObject);
var   a:   TMyFileListBox;
begin
       a   :=   TMyFileListBox.Create(self);
       a.FileType   :=[ftReadOnly,ftHidden,ftSystem,ftNormal];
       a.Mask   :=    'c:\Windows\System32\*.* ';
       a.Parent   :=   Form1;
       a.Width   :=   200;
       a.Height   :=   200;
end;

end.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值