Solved

Windows ImageList Question

Posted on 1999-01-04
4
185 Views
Last Modified: 2010-04-04
I am trying to use the Windows ImageList in a listview.  I can do everything just fine, if the files that are in the listview are real filenames.  But I want to be able to display file type images, without having to know the exact location of one of those types of files. (ie.  Mike.gif NOT c:\graphics\Mike.gif ).  I am using ShGetFileInfo to find out the information.  Basically, if I put a string into the Listview.Items list (ie. Mike.gif ), I want to be able to find the windows image...

Please help and give examples...
0
Comment
Question by:MichaelB3
  • 3
4 Comments
 
LVL 20

Accepted Solution

by:
Madshi earned 500 total points
ID: 1354427
Look at this text. I downloaded it from somewhere (can't remember where it was)...

>>

System Icon Index From Extensions      D2 D3  
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.
 
<<

Hope this helps...   :-)

Regards, Madshi.
0
 

Author Comment

by:MichaelB3
ID: 1354428
YOU DA MAN!
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1354429
:-)

I love 2000 points...   :-)
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1354430
I mean I got 2000 points for your 500 points together with an A grading...  :-)
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

685 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