Solved

Get icons in ZIP-files

Posted on 1998-08-08
1
175 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
1 Comment
 
LVL 20

Accepted Solution

by:
Madshi earned 100 total points
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now