News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Getting the ICON of a file

Started by Robert Collins, October 14, 2005, 04:43:02 AM

Previous topic - Next topic

Robert Collins

How do you obtain the current icon for any given file type?

I am making a Windows Explorer like application so when I display the files contained within a directory I would like to also display the icon that is currently associated with the different files.

I need to do this on the fly since icons for files could change from time to time.

Tedd

No snowflake in an avalanche feels responsible.

P1

Robert,

Please remember the icon of registered file types too.

Regards,  P1  :8)

Robert Collins

OK, the SHGetFileInfo API works fine.

Now, I use the API to get the "Small Icon" of the file and load it into an ImageList. Then I use the ImageList to place the icon on the ListView report.

It appears that I cannot use the following statements however:

ImageList.listImages.Add(returned handle of the icon image from the API).
ListView.ListItems(index).SmallIcon = returned handle of the icon image from the API.

Neither one of the above two seem to work so I have to load the returned icon from the API into a PictureBox Control then I have to use the following:

ImageList.ListImages.Add(,"myicon", handle of the image in the PictureBox).   

Then I have to do this:

ListView.ListItems(index).SmallIcon = "myicon".

OK, kind of messy but it works.

This is OK for the the first pass through a listing display in the ListView. It displays the correct icons but I have a problem when switching to a new list of files. As mentioned above I have to get the icon of each file using the API, put the handle of the image into a PictureBox, then load the ImageList from the PictureBox.

In order to make this work I need to clear out the ImageList of all previous loaded images so as to start with a new set of images for the new listing report but I cannot get either of the below two to work:

ImageList.ListImages.Clear
ImageList.ListImages.Remove Index

Acccording to what I have read from books and other sources either one of the above two is how you remove images from a ImageList Control. But they don't. I cannot get either to work so what happens is that new images for the new list report are simply added onto the end of the previous images. This is a problem because as I go from one listing to another the icon images are always being added to the end of the ImageList. Well, it doesn't take long before the ImageList has thousands of images in it and they are duplicated all the time.

Does anyone know what I may be doing wrong about clearing or removing the images from the ImageList or is there another way to do this?     



Robert Collins

OK, never mind on above post. I finally go it to work. However, there must be a better way to do this than using the SHGetFileInfo API. The problem with this API is that it requires the full path to the file in order to retrieve it's icon but I have another application, called CuteFTP, which always places the correct icon for the file in it's List View and these files are on a remote host so here it cannot use the path because the path doesn't exist on the local machine.

Tedd

To get the icon for a file association, it looks like you'll have to either:
- hunt through the registry, and lookup the file extension and then check its subkeys to get the location of the icon;
- or, it's likely to be done through one of the COM interfaces, have a look at the IShell.... interfaces.
No snowflake in an avalanche feels responsible.

Phoenix

Robert,

perhaps this link is useful for you: http://www.developerfusion.co.uk/show/2982/1/ - File Extensions: Finding the default Icon. This is for Visual Basic, but should be easy to convert...

Robert Collins

Quote from: Phoenix on October 17, 2005, 07:06:08 PM
Robert,

perhaps this link is useful for you: http://www.developerfusion.co.uk/show/2982/1/ - File Extensions: Finding the default Icon. This is for Visual Basic, but should be easy to convert...

Thanks, Phoenix, that is exactly what I needed to get the icons.

The only problem I have left is how to place them in the List View Report window. As I mentioned above I have to get the icon and place it in a PictureBox Control then add the Image property of the PictureBox to the ImageList Control and then use the ImageList Control as the source for the icons to place in the List View. This is an extermely crude approach (not to mention time consuming) and then it only works for small directories. If the directory of files is large, like the System directory, it won't work because an ImageList Control cannot hold as many icons as there are files in the System directory and pretty soon about half way into the files I get an Out Of Memory error. I tried my approach on the Windows directory and although it worked it took forever to load the ListView with the file information and their associated icons. I sure wish I knew how Windows Explorer does it. It's like instantaneous. Almost as if the Windows Explorer Tree View and List View Controls are preloaded with all this info because as soon as I click on any directory (including System) it's all there at a blink of an eye. I came accross several example of Window Explorer sample applications off the Web and they worked perfectly but unfortunately whatever methods they used were wrapped up in a DLL or an OCX by the authors and as such gave no clue as to how it's done :(

Phoenix

Robert,

there is an ExtractAssociatedIconEx-Function in shell32.dll version 5.0 or later that returns an icon handle: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/functions/openregstream.asp

Tedd

#9
An example, using SHGetFileInfo (without a full path - just the file name!)

Certain icons aren't cached in the system's image list (I think it's internet explorer specific ones eg. html, xml) so you need to get these separately (somehow.)
And it's probably a good idea to pre-fetch a number of the common ones you'll expect to find.
No snowflake in an avalanche feels responsible.

Tedd

Okay, here's a better version - you don't need to create your own image list, you can just index into the system's image list :bg

The icon for htm/html files still doesn't seem to work (does on Win98, but not WinXP.)
(And the xml icon works on some machines, but not others.)


[attachment deleted by admin]
No snowflake in an avalanche feels responsible.