Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 166
  • Last Modified:

ShellGetIconIndexFromExt/Listview

I'm attempting to write an FTP client but I need some help with a procedure.

I have a listview for the remote file view and I need a procedure I can stick (for example only) in the change event, that will use "ShellGetIconIndexFromExt" or SOMETHING to assign an icon to the item being added. MEANING the file won't exist, so the icon needs to be retrieved using an unconventional method. Any help with this would be GREATLY appreciated and full credit will be given in my client when it is finished.

Thanks in advance!
0
Psylord
Asked:
Psylord
  • 6
  • 2
1 Solution
 
LischkeCommented:
Hi Psylord,

I have not fully understood what you want. Is it that you need to set the correct (system) icon for a certain file or do you want to directly retrive the icon of the application (for remote files this will need some extra work)?

If you want to show the normal icons for all files in your listview (normal means here: the same icons as Explorer shows) then use the system's internal image list for the listview and pick the correct icon indices for each file:

procedure CreateImages;

var
  SysIL: HImageList;
  SFI: TSHFileInfo;
 
begin
  // Create an image list component
  MyListView.Images := TImageList.Create(Self);
  // This gets the small image list.  Replace SHGFI_SMALLICON with
  // SHGFI_LARGEICON for large images.
  SysIL := SHGetFileInfo('', 0, SFI, SizeOf(SFI), SHGFI_SYSICONINDEX or SHGFI_SMALLICON);
  if SysIL <> 0 then
  begin
    // Assign the system list to the component
    Images.Handle := SysIL;
    // The following prevents the image list handle from being
    // destroyed when the component is.
    Images.ShareImages := True;
  end;
end;

In order to get the correct icon index into the image list use:

function GetIconIndex(const AFile: string; Attrs: DWORD): integer;
 var
  SFI: TSHFileInfo;
 begin
  SHGetFileInfo(PChar(AFile), Attrs, SFI, SizeOf(TSHFileInfo),
              SHGFI_SYSICONINDEX or SHGFI_USEFILEATTRIBUTES);
   Result := SFI.iIcon;
end;

If the file physically exists and is accessible by the system then Attrs can be set to 0, because the true icon will be used (depending on the kind of file). If the file is not accessible then you need to set Attrs accordingly. Then the returned index is based on the file extension.

Ciao, Mike
0
 
PsylordAuthor Commented:
I think We're on the right track here.
To answer your question: "Is it that you need to set the correct (system) icon for a certain file or do you want to directly retrive the icon of the application (for remote files this will need some extra work)?" -- I want to "set the correct (system) icon for a certain file"

I tried using your code (which I appreciate!) but I'm guessing some things are missing from the uses clause? What do I need to add?

Best Regards,
Paul Swonger
0
 
PsylordAuthor Commented:
I've found the uses clauses (I apologize for my ignorance). For anyone reading they are:

ShellAPI, commctrl,
  ComCtrls

I think. :)

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
PsylordAuthor Commented:
I've found the uses clauses (I apologize for my ignorance). For anyone reading they are:

ShellAPI, commctrl,
  ComCtrls

I think. :)

0
 
PsylordAuthor Commented:
Ok... I think I've got it... But since the file does not exist... I would set Attrs to what?
0
 
PsylordAuthor Commented:
I've done it! Disregard all previous comments. I appreciate the help!

-Paul Swonger
0
 
PsylordAuthor Commented:
Excellent job.
0
 
LischkeCommented:
Hey Psylord, very frequent posts here... I was sleeping a while, so I could not answer here :-). But I see you managed the missing parts yourself, so thank you for the A grading!

Have fun and

Ciao, Mike
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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