this slowpoke moves

ListView AlphaSort

Wenn man den Windows Explorer öffnet, um sich Dateien anzusehen und dabei die Ansicht oben auf "Details" stellt, kann man den obigen Column dafür benutzen, um die Ansicht mit einem Klick zu sortieren. Da der Explorer von Windows nichts anderes ist als eine ListView, die einem auch zur Verfügung steht, kann das in seinem eigenen Programm genutzt werden.

Legt auf die Form eine ListView und erstellt zwei Columns, dann muss der ViewStyle auf "vsReport" gestellt werden. Mit dem folgenden Code kann man seine ListView genauso sortieren wie in dem Explorer.

uses ExtCtrls, ComCtrls

private
    { Private declarations }
    procedure FindAllFiles(RootFolder: string; Mask: string = '*.*'; Recurse:
      Boolean = True);
      
var
  Form1: TForm1;
  ColumnToSort : Integer;
  Accending    : Boolean = False;
  
//

function GetFileSize(szFile: PChar): Int64;
var
  fFile        : THandle;
  wfd          : TWIN32FINDDATA;
begin
  result := 0;
  if not FileExists(szFile) then
    exit;
  fFile := FindFirstfile(pchar(szFile), wfd);
  if fFile = INVALID_HANDLE_VALUE then
    exit;
  result := (wfd.nFileSizeHigh * (Int64(MAXDWORD) + 1)) + wfd.nFileSizeLow;
  windows.FindClose(fFile);
end;

procedure TForm1.FindAllFiles(RootFolder: string; Mask: string = '*.*'; Recurse:
  Boolean = True);
var
  SR           : TSearchRec;
  ListItem     : TListItem;
begin
  if RootFolder = '' then
    Exit;
  if AnsiLastChar(RootFolder)^ <> '\' then
    RootFolder := RootFolder + '\';
  RootFolder := IncludeTrailingPathDelimiter(RootFolder);
  if Recurse then
    if FindFirst(RootFolder + '*.*', faAnyFile, SR) = 0 then
    try
      repeat
        if SR.Attr and faDirectory = faDirectory then
          if (SR.Name <> '.') and (SR.Name <> '..') then
            FindAllFiles(RootFolder + SR.Name, Mask, Recurse);
      until FindNext(SR) <> 0;
    finally
      FindClose(SR);
    end;
  if FindFirst(RootFolder + Mask, faAnyFile, SR) = 0 then
  try
    repeat
      if SR.Attr and faDirectory <> faDirectory then
      begin
        ListItem := Listview1.Items.Add;
        ListItem.Caption := SR.Name;
        ListItem.SubItems.Add(IntToStr(GetFileSize(PChar(RootFolder +
          SR.Name))));
      end;
    until FindNext(SR) <> 0;
  finally
    FindClose(SR);
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Listview1.SortType := stData;
  FindAllFiles('C:\Windows\Temp', '*.*', False);
end;

procedure TForm1.ListView1ColumnClick(Sender: TObject;
  Column: TListColumn);
begin
  ColumnToSort := Column.Index;
  Listview1.AlphaSort;
  Accending := not Accending;
end;

procedure TForm1.ListView1Compare(Sender: TObject; Item1, Item2: TListItem;
  Data: Integer; var Compare: Integer);
begin
  case ColumnToSort of
    0:
      begin
        if Accending then
          Compare := CompareText(Item1.Caption, Item2.Caption)
        else
          Compare := CompareText(Item2.Caption, Item1.Caption);
      end;
    1:
      begin
        if Accending then
          Compare := StrToInt(Item1.SubItems[0]) - StrToInt(Item2.SubItems[0])
        else
          Compare := StrToInt(Item2.SubItems[0]) - StrToInt(Item1.SubItems[0]);
      end;
  end;
end;

Keine Kommentare:

Kommentar veröffentlichen

Beliebte Posts

Translate