We help IT Professionals succeed at work.

Question about default icon

chengjian
chengjian asked
on
hi

   There's a TListView in my program, it should display some files with different extension(HTM, EXE, ICO, BMP, PAS ECT.), I want users can see the file's icon in the TListView just like in explorer, how can I do this?

   I do not want to use 3d party component and TFileListView stuff.
Comment
Watch Question

DMN

Commented:
Take a look at Virtual ListView example shipped with Delphi...
Here is an example of how this could be done. The example requires a TEdit, TListView, TButton, and a TImageList to be on a form. Once all the components are on the form, just make your code like below.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ComCtrls, ShellAPI, StdCtrls, ImgList, FileCtrl;

type
  TForm1 = class(TForm)
    ListView1: TListView;
    Button1: TButton;
    ImageList1: TImageList;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}


procedure TForm1.Button1Click(Sender: TObject);var
  I : Integer;
  Icn : TIcon;
  SearchRec : TSearchRec;
  ListItem : TListItem;
  FileInfo : SHFileInfo;
begin
  with ListView1 do
  begin
    if DirectoryExists(Edit1.Text) then
    begin
      if (Edit1.Text[Length(Edit1.Text)] <> '\') and
         (Edit1.Text[Length(Edit1.Text)] <> '/') then
        Edit1.Text := Edit1.Text + '\';
      Columns.Clear;
      Items.Clear;
      //--Could be set in Object Inspector---
      SmallImages := ImageList1;
      ViewStyle := vsReport;
      SortType := stText;
      Columns.Add;
      //------------------------
      Icn := TIcon.Create;
      try
        Items.BeginUpdate;
        I := FindFirst(Edit1.Text + '*.*', faAnyFile, SearchRec); //Change file extension here if wished
        while I = 0 do
        begin
          Application.ProcessMessages;
          if ((SearchRec.Attr and FaDirectory <> FaDirectory) and
             (SearchRec.Attr and FaVolumeId <> FaVolumeID)) then
          begin
            ListItem := Items.Add;
            SHGetFileInfo(PChar(Edit1.Text + SearchRec.Name), 0, FileInfo,
                          SizeOf(FileInfo), SHGFI_DISPLAYNAME);
            ListItem.Caption := FileInfo.szDisplayName;
            SHGetFileInfo(PChar(Edit1.Text + SearchRec.Name), 0, FileInfo,
                          SizeOf(FileInfo), SHGFI_ICON);
            Icn.handle := FileInfo.hIcon;
            ListItem.ImageIndex := ImageList1.AddIcon(Icn);
          end;
          I := FindNext(SearchRec);
        end;
      finally
        Items.EndUpdate;
        Icn.Free;
        if Items.Count = 0 then
          ShowMessage('No Files with the extension "*.exe" found.');
      end;
    end
    else
      ShowMessage('Directory does not exist.');
  end;
end;

end.

Cheers,

Alan

Author

Commented:
thanx and i'll try, wait me please

Commented:
I go with DMN, please look at the virtual listView example, Alan's code will most probably work fine, but it's terribly slow, if you explore a big folder. The virtual listView is as fast as Windows' explorer.

Regards, Madshi.

Commented:
Whay don't use GXExplore pack ? Is freeware and include a TListView with this feature ( http://www.gexperts.org ).

Commented:
Sorry , I read to late .....
 'I do not want to use 3d party component and TFileListView stuff'............

Mea culpa!

Author

Commented:
alanwhincup's answer works, though it works not so good in performance(so i do not give him/her A), but it is acceptable in my program. thanx all of you!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.