2 Replies - 1146 Views - Last Post: 29 January 2016 - 04:00 AM Rate Topic: -----

#1 skyscraper11   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 23-July 14

How to display icon of folders in a ListView

Posted 29 January 2016 - 12:42 AM

I'm having problems with showing the icon of folders(not files) into a listview, I spent hours to search and code just for this one problem, can you please help me?

this is the way I show the folders' icon and it doesn't work, it doesn't show any icon.

For Each fldr As String In Directory.GetDirectories(Form2.TextBox1.Text)
            Dim mShellIconManager As New Form1

            lvi = New ListViewItem
            lvi.Text = Path.GetFileName(fldr)

            lvi.SubItems.Add(((fldr.Length / 1024)).ToString("0.00"))


and I'm using shell32 extraction with it, but I think it's just for files only, this is my shell extraction

' declare the Win32 API function SHGetFileInfo
    Public Declare Auto Function SHGetFileInfo Lib "shell32.dll" (ByVal pszPath As String, ByVal dwFileAttributes As Integer, ByRef psfi As SHFILEINFO, ByVal cbFileInfo As Integer, ByVal uFlags As Integer) As IntPtr
    ' declare some constants that SHGetFileInfo requires
    Public Const SHGFI_ICON As Integer = &H100
    Public Const SHGFI_SMALLICON As Integer = &H1
    ' define the SHFILEINFO structure
    Structure SHFILEINFO
        Public hIcon As IntPtr
        Public iIcon As Integer
        Public dwAttributes As Integer
        <Runtime.InteropServices.MarshalAs(Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=260)> _
        Public szDisplayName As String
        <Runtime.InteropServices.MarshalAs(Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=80)> _
        Public szTypeName As String
    End Structure
    Function RetrieveShellIcon(ByVal argPath As String) As Image
        Dim mShellFileInfo As SHFILEINFO
        Dim mSmallImage As IntPtr
        Dim mIcon As System.Drawing.Icon
        Dim mCompositeImage As Image
        mShellFileInfo = New SHFILEINFO
        mShellFileInfo.szDisplayName = New String(Chr(0), 260)
        mShellFileInfo.szTypeName = New String(Chr(0), 80)
        mSmallImage = SHGetFileInfo(argPath, 0, mShellFileInfo, System.Runtime.InteropServices.Marshal.SizeOf(mShellFileInfo), SHGFI_ICON Or SHGFI_SMALLICON)
        ' create the icon from the icon handle
            mIcon = System.Drawing.Icon.FromHandle(mShellFileInfo.hIcon)
            mCompositeImage = mIcon.ToBitmap
        Catch ex As Exception
            ' create a blank black bitmap to return
            mCompositeImage = New Bitmap(16, 16)
        End Try
        ' return the composited image
        Return mCompositeImage
    End Function
    Function GetIcon(ByVal argFilePath As String) As Image
        Dim mFileExtension As String = System.IO.Path.GetExtension(argFilePath)
        ' add the image if it doesn't exist
        If cIcons.ContainsKey(mFileExtension) = False Then
            cIcons.Add(mFileExtension, RetrieveShellIcon(argFilePath))
        End If
        ' return the image
        Return cIcons(mFileExtension)
    End Function

Is This A Good Question/Topic? 0
  • +

Replies To: How to display icon of folders in a ListView

#2 Minimalist   User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 240
  • Joined: 13-November 13

Re: How to display icon of folders in a ListView

Posted 29 January 2016 - 03:27 AM

IronRazer has a good example here:
which should you get started.
Was This Post Helpful? 2
  • +
  • -

#3 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1505
  • View blog
  • Posts: 3,810
  • Joined: 01-February 13

Re: How to display icon of folders in a ListView

Posted 29 January 2016 - 04:00 AM

I would recommend that you use the example that i showed in the link that Minimalist has given you. Not only does it give you the option of getting the large (32x32) or small (16x16) icon image, it also destroys the icon that is created in unmanaged memory by the SHGetFileInfo api function. That is important to do to avoid memory leaks.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1