What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleDisplay the big and small icons associated with a file
DescriptionThis example shows how to display the big and small icons associated with a file in Visual Basic 6. It uses the SHGetFileInfo API function to get an icon handle and then uses OleCreatePictureIndirect to convert the handle into a picture. You could modify the program to save the icons if you wanted to use them in other programs.
Keywordsicon, large icon, small icon
CategoriesGraphics, Software Engineering
The program uses DriveListBox, DirListBox, and FileListBox controls to let the user easily select files. When the user clicks on a file, the program calls the GetIcon function to get the file's icons.

Function GetIcon uses the SHGetFileInfo API function to retrieve a handle to one of the file's icons. It then calls function IconToPicture to convert the handle into a picture.

Function OleCreatePictureIndirect uses thue OleCreatePictureIndirect API function to convert the icon handle into a picture.

' Return a file's icon.
Private Function GetIcon(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, 0, sh_info, _
        Len(sh_info), SHGFI_ICON + icon_size
    hIcon = sh_info.hIcon
    Set icon_pic = IconToPicture(hIcon)
    Set GetIcon = icon_pic
End Function

' Convert an icon handle into an IPictureDisp.
Private Function IconToPicture(hIcon As Long) As _
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
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.