Solved

Get icons in ZIP-files

Posted on 1998-08-08
1
189 Views
Last Modified: 2010-04-04
How do I get the icon of a file, compressed by ZIP?
My problem is that I need to get the image index of a file, using only the extension (*.exe, par example). The functions I've found requires the whole path + filename, and therefore needs uncompressed files.
0
Comment
Question by:jonassondaniel
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 20

Accepted Solution

by:
Madshi earned 100 total points
ID: 1361725
There's a tip I found last week on a homepage (I don't know anymore from what homepage...):

"System Icon Index From Extensions
I've seen this question a lot, and several asked me directly: How can I get the index of a system icon based only on the extension? It's pretty simple once you see it done, but I guess it is one of those things that's not readily apparent. I know it wasn't to me.

Assuming you are using some control that has an image list property that you assign, you first set that up so that it uses the system's image list. There is one thing abuot this that you must be very careful of; the system image list belongs to the system, and it (the list, not the component that "wraps" it) must not be freed. Following is a code snippet that properly creates the system image list, setting its ShareImages property which prevents the image list handle from being destroyed when the component is.

// This gets the system image list.
var
  SysIL: HImageList;
  SFI: TSHFileInfo;
begin
  // Create an image list component
  MyTreeView.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;

Now, given that we have the system image list to work with, we can get the image index to a file, whether it exists or not, by using SHGetFileInfo function with the SHGFI_USEATTRIBUTES flag. This looks like:

  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;

{...and to use this function...}

  // This will get the index to the folder image even though the
  //  directory doesn't exist.
  NewTreeItem.ImageIndex := GetIconIndex('c:\bogus\directory\',
     FILE_ATTRIBUTE_NORMAL or FILE_ATTRIBUTE_DIRECTORY);

  // This will get the index for .htm files image
  NewTreeItem.ImageIndex := GetIconIndex('notarealfile.htm',
     FILE_ATTRIBUTE_NORMAL);
That's it."
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Suggested Courses
Course of the Month8 days, 8 hours left to enroll

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question