我们都知道,Windows资源管理器可以对文件名按时间排序,但是在BCB里的“Win3.1”面板里的FileListBox控件中,如何实现对文件名按时间排序呢?例如我的文件本来应该排列为: LN1.txt
                            LN2.txt
                            LN3.txt
                            LN10.txt
                            LN20.txt
                            LN30.txt但在FileListBox控件中却排列为: LN1.txt
                               LN10.txt
                               LN2.txt
                               LN20.txt
                               LN3.txt
                               LN30.txt如何才能满足我的要求呢?请高手指点,谢谢!

解决方案 »

  1.   

    在TFileListBox的子类里重写ReadFileNames那个虚方法(见filectrl.pas),根据TSearchRec.Time进行排序。unit Unit1;interfaceuses
      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;
          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.
      

  2.   

    我昨天采用了变通的方法,定义一个结构数组,通过FindFrist和FindNext把找到的文件放在这个结构里面,然后通过文件时间排序,最后把排序好的数组反送到FileListBox,就很好的实现了愿望.多谢liangqingzhi(老之)!我学了点新知识,只是Delphi语言我看的比较吃力,呵呵