Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleGet the icons associated with a file type
Keywordsicon, file type icon
CategoriesGraphics, Software Engineering, Files and Directories
 
Use the SHGetFileInfo API function to get the icons' file handles. Use OleCreatePictureIndirect to create a picture from the handle.
 
Private Function GetTypeIcon(filename As String, icon_size _
    As Long) As IPictureDisp
Dim index As Integer
Dim hIcon As Long
Dim item_num As Long
Dim icon_pic As IPictureDisp
Dim sh_info As SHFILEINFO

    SHGetFileInfo filename, FILE_ATTRIBUTE_NORMAL, sh_info, _
        Len(sh_info), SHGFI_USEFILEATTRIBUTES Or _
        (SHGFI_ICON + icon_size)

    hIcon = sh_info.hIcon
    Set icon_pic = IconToPicture(hIcon)
    Set GetTypeIcon = icon_pic
End Function

' Convert an icon handle into an IPictureDisp.
Private Function IconToPicture(hIcon As Long) As _
    IPictureDisp
Dim cls_id As CLSID
Dim hRes As Long
Dim new_icon As TypeIcon
Dim lpUnk As IUnknown

    With new_icon
        .cbSize = Len(new_icon)
        .picType = vbPicTypeIcon
        .hIcon = hIcon
    End With
    With cls_id
        .id(8) = &HC0
        .id(15) = &H46
    End With
    hRes = OleCreatePictureIndirect(new_icon, _
        cls_id, 1, lpUnk)
    If hRes = 0 Then Set IconToPicture = lpUnk
End Function

Private Sub cboFileTypes_Click()
    On Error GoTo LoadPictureError

    SmallIconPicture.Picture = _
        GetTypeIcon(cboFileTypes.Text, SHGFI_SMALLICON)
    SmallIconLabel.Caption = _
        Format$(SmallIconPicture.ScaleWidth) & _
        "x" & _
        Format$(SmallIconPicture.ScaleHeight)

    LargeIconPicture.Picture = _
        GetTypeIcon(cboFileTypes.Text, SHGFI_LARGEICON)
    LargeIconLabel.Caption = _
        Format$(LargeIconPicture.ScaleWidth) & _
        "x" & _
        Format$(LargeIconPicture.ScaleHeight)
    
    Exit Sub

LoadPictureError:
    Beep
    Caption = "TypeIcons [Invalid picture]"
    Exit Sub
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated