Results 1 to 6 of 6

Thread: extracting the icon assosiated with a particular file extension ...

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 1999
    Location
    Californ-I- A
    Posts
    207

    Post

    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
    [email protected]
    ICQ: 53480225


  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    You can use the ExtractAssociatedIcon API, here's a quick Example:

    Add a Picturebox, Command Button and CommonDialogbox to a Form..
    Code:
    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
    [email protected]
    [email protected]


  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 1999
    Location
    Californ-I- A
    Posts
    207

    Post

    Nice, thanks. That's two in a row you've answered for me ... a human desk reference. Thanks again.

  4. #4
    Lively Member
    Join Date
    Oct 2002
    Posts
    70
    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

  5. #5
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    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, it's old, so there are likely better ones I've posted since. (good excuse to use the excellent Search option.)

  6. #6
    Lively Member
    Join Date
    Oct 2002
    Posts
    70
    Perfect!

    Thanks a bunch.

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

    Thanks again,
    ImpShial

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width