• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 449
  • Last Modified:

Add Associated File Icon into Listview

hi, I would like to add an Associated Icon into Listview from the selected file string.

could anyone provide an example.

Sal.
0
SaLz
Asked:
SaLz
3 Solutions
 
wolsenCommented:
http://www.swissdelphicenter.ch/torry/showcode.php?id=218

That will give you the icon for a given file name.
0
 
yehiaegCommented:
ok, something like this will do  it (pseudo code not tested):

procedure TForm1.cmdGetIconClick(Sender: TObject);
var
  Icon : TIcon;
  ListItem : TListItem;
  shInfo : TSHFileInfo;
  FileLocation : string;
begin
  ListView.SmallImages := imgList;
  Icon := TIcon.Create;
   ListItem := listview.items.add;
   SHGetFileInfo(PChar(FileLocation), 0, shInfo, SizeOf(shInfo), SHGFI_ICON or SHGFI_SMALLICON or
        SHGFI_SYSICONINDEX); //Get File Icon, which is now stored in shInfo.Icon

    Icon.Handle := shInfo.hIcon;    
    ListItem.Caption := ExtractFileName(FileLocation);    //...add filename
    ListItem.ImageIndex := imgList.AddIcon(Icon); //...and icon.

  end;
end;
0
 
Slick812Commented:
hello   SaLz, here is some code you might find helpful to get a TListView to show asscialted Icons, I have code for a Button Click event in -
TForm1.but_DoListviewClick(Sender: TObject);
below, This sets the list view Image Lists for icons and then uses the  GetIconIndex( ) function to get the system index


function GetIconIndex(const Path: String): Cardinal;
var
Attribute, SHResult: Cardinal;
ShInfo1: TSHFILEINFO;
begin
Attribute := FILE_ATTRIBUTE_NORMAL;
if DirectoryExists(Path) then
  Attribute := FILE_ATTRIBUTE_DIRECTORY;
{test to see if it is a Directory and set Attibute to FILE_ATTRIBUTE_DIRECTORY.}

SHResult := SHGetFileInfo(PChar(Path), Attribute, ShInfo1, SizeOf(ShInfo1),
                 SHGFI_SYSICONINDEX);
{this get's the Image Index in the ShInfo1.iIcon }
if SHResult = 0 then
  Result := 0 else // you may want a different one for does not exist
  Result := ShInfo1.iIcon;
end;

procedure TForm1.but_DoListviewClick(Sender: TObject);
var
i, hSysIList: Integer;
ListItem1: TListItem;
NewColumn1: TListColumn;
ShInfo1: TSHFILEINFO;
begin
with ListView1 do
  begin
  {First get the Handle of the System Large Icon Image List using the
  SHGFI_SYSICONINDEX or SHGFI_LARGEICON flags in the SHGetFileInfo function}
  hSysIList := SHGetFileInfo('', 0, ShInfo1, SizeOf(ShInfo1),
    SHGFI_SYSICONINDEX or SHGFI_LARGEICON);
  if hSysIList <> 0 then
    begin
    {you do NOT have ANY TImageList components on your Form
  you will create them here at run time
  First Create the LargeImages TImageList for the large Icons}
    LargeImages := TImageList.Create(Self);
    // Assign the system large Icon imageList to the LargeImages TImageList
    LargeImages.Handle := hSysIList;
    //Now the LargeImages IS the system large Icon imageList

    // The following prevents the image list handle from being
    // destroyed when the LargeImages component is destroyed.
    LargeImages.ShareImages := TRUE;
  {This LargeImages IS the system Image List, so do not
  try and change it, like adding images or changing it's properties}
    end;


  hSysIList := SHGetFileInfo('', 0, ShInfo1, SizeOf(ShInfo1),
    SHGFI_SYSICONINDEX or SHGFI_SMALLICON);
  {get the Handle for the system Small Icon Image List with the
  SHGFI_SYSICONINDEX or SHGFI_SMALLICON flags}
  if hSysIList <> 0 then
    begin
    SmallImages := TImageList.Create(Self);
    {create a SmallImages Image List}
    // Assign the system list handle to the ListView1.SmallImages
    SmallImages.Handle := hSysIList;
    {Make Sure that you do NOT try to destroy the system Image List}
    SmallImages.ShareImages := TRUE;
    end;

    for i := 0 to 9 do
      begin
    {I will craete 10 List Items, just to test this List View}
      ListItem1 := Items.Add;
      ListItem1.ImageIndex := 0;
      end;

 {add file path to Item Captions, it will look better if you just display the file name}
    Items[0].Caption := 'E:\Bmp1.bmp';
    Items[1].Caption := 'E:\8bit.gif';
    Items[2].Caption := 'E:\ColorText.txt';
    Items[3].Caption := 'E:\Dialog2.htm';
    Items[4].Caption := 'E:\test2.file';
    Items[5].Caption := 'E:\Dialogs.exe';
    Items[6].Caption := 'E:\test.ico';
    Items[7].Caption := 'E:\No File.ghik';
    Items[8].Caption := 'E:\Demos';
    Items[9].Caption := 'C:\boot.ini';

    for i := 0 to Items.Count - 1 do
      begin
    {the ListView1.Items[i].Caption will have a File Path, if you are
    using the List View as a Container for a Folder's Files with out Path, then you will
    need to ADD the Folder's Drive and full Path
    to the Items[i].Caption, , maybe PChar(DirPath + '\' + Items[i].Caption)  }
      Items[i].ImageIndex := GetIconIndex(Items[i].Caption);
      end;
  // Create two columns to show during viewing as vsReport
  NewColumn1 := Columns.Add;
  NewColumn1.Caption := 'Column 1';
  NewColumn1.Width := 130;
  NewColumn1 := Columns.Add;
  NewColumn1.Caption := 'Column 2';
  NewColumn1.Width := 100;
  Refresh;
  end;

end;

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Ask questions if you need more information
0
 
SaLzAuthor Commented:
sorry about that, had 2 go into hospital.
0
 
Slick812Commented:
I hope you are getting Better health !
  good luck
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now