was ist neu ¦  programmier tips ¦  indy artikel ¦  intraweb artikel ¦  informationen ¦  links ¦  interviews
 sonstiges ¦  tutorials ¦  Add&Win Gewinnspiel

Tips (1541)

Dateien (137)
Datenbanken (90)
Drucken (35)
Grafik (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Mathematik (76)
Multimedia (45)
Oberfläche (107)
Objekte/
ActiveX (51)

OpenTools API (3)
Sonstiges (126)
Strings (83)
System (266)
VCL (242)

Tips sortiert nach
Komponente


Tip suchen

Tip hinzufügen

Add&Win Gewinnspiel

Werbung

40 Visitors Online


 
...Dateien mit ihren zugehörigen Icons in einer Listview anzeigen?
Autor: Thomas Stutz
[ Tip ausdrucken ]  

Tip Bewertung (56):  
     




{
  The following example shows how to show all files and their
  associated icons of a folder in a TListView.
  To test the code, you need a ListView1 and a ImageList1 where the icons are stored.

  Im folgenden Beispiel werden alle Dateien & zugehörigen
  Icons eines Verzeichnisses in einer TListView angezeigt.
  Um das Beispiel zu testen, braucht man eine ListView1 und eine ImageList1
  Komponente, wo die Icons gespeichert werden.
}


uses
  
ShellApi;

procedure LV_InsertFiles(strPath: string; ListView: TListView; ImageList: TImageList);
var
  
i: Integer;
  Icon: TIcon;
  SearchRec: TSearchRec;
  ListItem: TListItem;
  FileInfo: SHFILEINFO;
begin
  
// Create a temporary TIcon
  
Icon := TIcon.Create;
  ListView.Items.BeginUpdate;
  try
    
// search for the first file
    
i := FindFirst(strPath + '*.*', faAnyFile, SearchRec);
    while i = 0 do
    begin
      with 
ListView do
      begin
        
// On directories and volumes
        
if ((SearchRec.Attr and FaDirectory <> FaDirectory) and
          
(SearchRec.Attr and FaVolumeId <> FaVolumeID)) then
        begin
          
ListItem := ListView.Items.Add;
          //Get The DisplayName
          
SHGetFileInfo(PChar(strPath + SearchRec.Name), 0, FileInfo,
            SizeOf(FileInfo), SHGFI_DISPLAYNAME);
          Listitem.Caption := FileInfo.szDisplayName;
          // Get The TypeName
          
SHGetFileInfo(PChar(strPath + SearchRec.Name), 0, FileInfo,
            SizeOf(FileInfo), SHGFI_TYPENAME);
          ListItem.SubItems.Add(FileInfo.szTypeName);
          //Get The Icon That Represents The File
          
SHGetFileInfo(PChar(strPath + SearchRec.Name), 0, FileInfo,
            SizeOf(FileInfo), SHGFI_ICON or SHGFI_SMALLICON);
          icon.Handle := FileInfo.hIcon;
          ListItem.ImageIndex := ImageList.AddIcon(Icon);
          // Destroy the Icon
          
DestroyIcon(FileInfo.hIcon);
        end;
      end;
      i := FindNext(SearchRec);
    end;
  finally
    
Icon.Free;
    ListView.Items.EndUpdate;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  
// Assign a Imagelist to the ListView
  
ListView1.SmallImages := ImageList1;
  // Show Listview in Report Style and add 2 Columns
  
ListView1.ViewStyle := vsReport;
  ListView1.Columns.Add;
  ListView1.Columns.Add;
  LV_InsertFiles('C:\Windows\', ListView1, ImageList1);
end;


 

Bewerten Sie diesen Tipp:

dürftig
ausgezeichnet


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners