Solved

Get icons in ZIP-files

Posted on 1998-08-08
1
187 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

Technology Partners: 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!

Question has a verified solution.

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

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

751 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