2010-01-07 23 views

回答

15

有多個部分你的問題。我將在這裏提供概述。如果您需要任何特定步驟的幫助,請發佈更具體的後續問題。

  1. 確定「編譯的程序在哪裏」指的是什麼。

    要獲取EXE文件的完整路徑,請撥打ParamStr(0)。要從該字符串中刪除EXE文件名,所以您只需要目錄部分,請撥打ExtractFilePath。確保它以反斜槓(IncludeTrailingPathDelimiter)結尾,然後附加您的「文件」目錄。

  2. 獲取文件列表。

    使用FindFirstFindNext來製作一個查看所有文件的循環。名字將包括「。」和「..」相對目錄名稱,因此您可能希望排除它們。請注意,這些文件不是以任何特定順序枚舉的。例如,如果你需要按字母順序排序,你需要自己做。 (他們可能出現是在測試中的字母順序,但不會永遠是正確的。)

    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; 
    
  3. 添加節點控制來表示的文件。

    您可能希望在我上面提到的假設的DoSomethingWithFile函數中執行此操作。使用列表視圖的Items屬性對項目進行操作,例如添加新項目。

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

    TListItem具有附加屬性;檢查文檔的細節。如果您以「報告」模式顯示列表視圖,則可以使用SubItems屬性,其中可以有多個列用於單個節點。

  4. 將圖像與項目關聯。

    列表視圖中節點的圖像來自相關圖像列表LargeImagesSmallImages。它們是指您的表單上的一個或多個TImageList組件。將圖標圖像放在那裏,然後將項目'ImageIndex屬性分配給相應的數字。

根據您希望如何精心設計你的程序是,你不妨用Delphi的TShellListView控制,而不是做自己所有上述工作。

+1

+1非常好的答案,羅布! – jpfollenius 2010-01-08 08:08:37

+1

堆棧溢出行爲的一個很好的例子。真的很好回答Rob。 – robsoft 2010-01-08 09:30:30

2

如果您在包含兩個圖像(一個前置文件和一個目錄)的表單上放置一個TImagelist,然後將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; 
相關問題