devxlogo

GetFileIcon – Retrieve the icon associated to a file

GetFileIcon – Retrieve the icon associated to a file

Private Const MAX_PATH = 260Private Type SHFILEINFO    hIcon As Long    iIcon As Long    dwAttributes As Long    szDisplayName As String * MAX_PATH    szTypeName As String * 80End TypePrivate Declare Function SHGetFileInfo Lib "Shell32" Alias "SHGetFileInfoA" _    (ByVal pszPath As Any, ByVal dwFileAttributes As Long, psfi As SHFILEINFO, _    ByVal cbFileInfo As Long, ByVal uFlags As Long) As LongPrivate Const SHGFI_ICON = &H100Private Const SHGFI_OPENICON = &H2Private Const SHGFI_SELECTED = &H10000Private Const SHGFI_SHELLICONSIZE = &H4Private Const SHGFI_SMALLICON = &H1Private Const SHGFI_LARGEICON = &H0Public Enum mbIconSizeConstants    mbLargeIcon = SHGFI_LARGEICON           '32x32 icon    mbSmallIcon = SHGFI_SMALLICON           '16x16 icon    mbShellSizeIcon = SHGFI_SHELLICONSIZE   'size used by the shell to displayEnd Enum                                    'the icons (for example 32x32 or                                             ' 48x48)Public Enum mbIconTypeConstants    mbNormalIcon = SHGFI_ICON               'normal icon    mbSelectedIcon = SHGFI_SELECTED         'the icon when it is selected    mbOpenIcon = SHGFI_OPENICON             'the icon used for open foldersEnd Enum' Returns the description of the specified file/folder (for example "Folder",' "Executable file", "Bmp Image" and so on)' Get the file/folder's associated icon'' NOTE: uses the IconToPicture function'       (you can find it elsewhere in the Code Bank)Function GetFileIcon(ByVal sPath As String, Optional ByVal mbIconSize As _    mbIconSizeConstants = mbLargeIcon, Optional ByVal mbIconType As _    mbIconTypeConstants = mbNormalIcon) As StdPicture    Dim FInfo As SHFILEINFO    Dim lIconType As Long        lIconType = mbIconSize Or mbIconType    ' be sure that there is the mbNormalIcon too    If mbIconType <> mbNormalIcon Then lIconType = lIconType Or mbNormalIcon    ' retrieve the item's icon    SHGetFileInfo sPath, 0, FInfo, Len(FInfo), lIconType    ' convert the handle to a StdPicture    Set GetFileIcon = IconToPicture(FInfo.hIcon)End Function

See also  How College Students Can Shape the Future of Tech Responsibility
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist