PDA

Click to See Complete Forum and Search --> : extracting the icon assosiated with a particular file extension ...


MicahCarrick
Jan 10th, 2000, 03:03 PM
I've seen tons of code on extracting icons from dll's and exe's etc. But what I'd like to be able to do is determine and extract an icon based on it's assosiation with a file extension so that I can ... say load a listView control with a directory of files and have the appropriate icon.

I'm guessing the registry would be where to determine the assosiations .... any ideas?



------------------
Micah Carrick
http://micah.carrick.com
micah@carrick.com
ICQ: 53480225

Aaron Young
Jan 11th, 2000, 01:28 AM
You can use the ExtractAssociatedIcon API, here's a quick Example:

Add a Picturebox, Command Button and CommonDialogbox to a Form..

Private Declare Function ExtractAssociatedIcon Lib "shell32.dll" Alias "ExtractAssociatedIconA" (ByVal hInst As Long, ByVal lpIconPath As String, lpiIcon As Long) As Long
Private Declare Function DrawIconEx Lib "user32" (ByVal hdc As Long, ByVal xLeft As Long, ByVal yTop As Long, ByVal hIcon As Long, ByVal cxWidth As Long, ByVal cyWidth As Long, ByVal istepIfAniCur As Long, ByVal hbrFlickerFreeDraw As Long, ByVal diFlags As Long) As Long

Private Sub Command1_Click()
Dim lIcon As Long
On Error GoTo User_Cancelled
With CommonDialog1
.Flags = cdlOFNNoValidate
.CancelError = True
.DialogTitle = "Select a File.."
.Filter = "All Files (*.*)|*.*"
.ShowOpen
Picture1.Cls
lIcon = ExtractAssociatedIcon(App.hInstance, .FileName, -1)
Call DrawIconEx(Picture1.hdc, 0, 0, lIcon, 32, 32, 0, 0, 3)
End With
User_Cancelled:
End Sub



------------------
Aaron Young
Analyst Programmer
aarony@redwingsoftware.com
ajyoung@pressenter.com

MicahCarrick
Jan 11th, 2000, 02:17 AM
Nice, thanks. That's two in a row you've answered for me ... a human desk reference. Thanks again. :)

ImpShial
Mar 23rd, 2003, 12:17 PM
What if I need to view the icon of an associated file without having the physical file.

Say I have a database loaded with a list of files. I want to load a listview with those files, but they do not exist physically on a disk.

I still want to be able to show the icon next to each item in the list.

Can anyone help me here?

Thanks,
ImpShial

Aaron Young
Mar 23rd, 2003, 12:27 PM
You could create an empty file with the extension,
extract the icon, then destroy the empty file.

Otherwise you could enumerate the extensions in the HKEY_CLASSES_ROOT Hive of the registry and extract the icons using the icon information stored there.

I've posted many examples in this forum relating to associated file icons,
here's one (http://www.vbforums.com/showthread.php?s=&threadid=3038), it's old, so there are likely better ones I've posted since. (good excuse to use the excellent Search option.)

ImpShial
Mar 23rd, 2003, 01:31 PM
Perfect!

Thanks a bunch.

Now I just need to attach this code to a system image list.

Thanks again,
ImpShial