Solved

Windows ImageList Question

Posted on 1999-01-04
4
180 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
Comment Utility
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
Comment Utility
YOU DA MAN!
0
 
LVL 20

Expert Comment

by:Madshi
Comment Utility
:-)

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

Expert Comment

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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

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 my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

6 Experts available now in Live!

Get 1:1 Help Now