List of files in a directory on TListView

I am creating a program that should be on Form_Create, fill in TListViewunder the name FileList, the directory I want to fill in is where the compiled program + is \Files, since I never used it TListViewI want to know how to do this?

+3
source share
4 answers

There are several parts to your question. I will give a review here. If you need help at any given stage, send a more specific question about the next steps.

  • Determine what “where the program is compiled” means.

    EXE, ParamStr(0). EXE , , ExtractFilePath. , (IncludeTrailingPathDelimiter), "Files".

  • .

    FindFirst FindNext, , . "." ".." , . , - . , , . ( , .)

    var
      Rec: TSearchRec;
    begin
      if FindFirst(path + '\*', faAnyFile, Rec) = 0 then try
        repeat
          if (Rec.Name = '.') or (Rec.Name = '..') then
            continue;
          if (Rec.Attr and faVolumeID) = faVolumeID then
            continue; // nothing useful to do with volume IDs
          if (Rec.Attr and faHidden) = faHidden then
            continue; // honor the OS "hidden" setting
          if (Rec.Attr and faDirectory) = faDirectory then
            ; // This is a directory. Might want to do something special.
          DoSomethingWithFile(Rec.Name);
        until FindNext(Rec) <> 0;
      finally
        SysUtils.FindClose(Rec);
      end;
    end;
    
  • .

    , DoSomethingWithFile, . list Items, - , .

    var
      Item: TListItem;
    begin
      Item := ListView.Items.Add;
      Item.Caption := FileName;
    end;
    

    TListItem ; . SubItems , "", node.

  • .

    , LargeImages SmallImages. TImageList . , ImageIndex .

, , , Delphi TShellListView, .

+15

TImagelist ( fore ), TImagelist TListviews LargeImages, .

procedure TForm2.Button1Click(Sender: TObject);
    var li:TListItem;
    SR: TSearchRec;
begin
    FileList.Items.BeginUpdate;
    try
        FileList.Items.Clear;

        FindFirst(ExtractFilePath(Application.ExeName) +'*.*', faAnyFile, SR);
        try
            repeat
                li :=  FileList.Items.Add;
                li.Caption := SR.Name;

                if ((SR.Attr and faDirectory) <> 0)  then li.ImageIndex := 1
                else li.ImageIndex := 0;

            until (FindNext(SR) <> 0);
        finally
            FindClose(SR);
        end;
    finally
        FileList.Items.EndUpdate;
    end;
end;

, TImageList ExtractFileExt (SR.Name),

if ((SR.Attr and faDirectory) <> 0)  then li.ImageIndex := 1
else if lowercase(ExtractFileExt(SR.Name)) = '.png' then li.ImageIndex := 2
else if lowercase(ExtractFileExt(SR.Name)) = '.pdf' then li.ImageIndex := 3
else li.ImageIndex := 0;
+2

.

: http://www.delphidabbler.com/articles?article=16 http://delphi.about.com/od/delphitips2008/qt/lv_checkbox_bmp.htm

The only difference is that you will draw an icon / image. Here you will learn how to do this in the grid: http://delphi.about.com/library/weekly/aa032205a.htm So, from both of these you can get this idea.

+1
source

I need you to show program icons how to be code

"C: \ Program Files (x86) \ Google \ Chrome \ Application \ chrome.exe"

0
source

Source: https://habr.com/ru/post/1727636/


All Articles