Hello everyone
we use a listview box and we add some path's of the files in list
and now we want to show the file icon with in file path
example:
listview1.item.add " D:\test.txt"
listview1.item.add " D:\picture.jpg"
now we want to show these files icon in my listview
please tell me what i do ?
thanks

Dani AI

Generated

You can show the real Explorer icon for each file (txt, jpg, etc.) without adding hundreds of .ico files yourself. The trick is to attach the shell’s system image list to your VB6 ListView, then ask the shell for the icon index of each path you add. This way new file types just work, and the icons always match Windows. See the flags for SHGetFileInfo and the LVM_SETIMAGELIST message. SHGetFileInfo docs, LVM_SETIMAGELIST. (learn.microsoft.com)

Code sketch (drop the VB6 ListView on a form, name it ListView1). It uses Report view with two columns and shows each file’s small icon at the left of the first column.

' Declarations (Form or .bas module)
Private Declare Function SHGetFileInfo Lib "shell32" Alias "SHGetFileInfoA" _
  (ByVal pszPath As String, ByVal dwFileAttributes As Long, _
   ByRef psfi As SHFILEINFO, ByVal cbFileInfo As Long, ByVal uFlags As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
  (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Type SHFILEINFO: hIcon As Long: iIcon As Long: dwAttributes As Long: _
  szDisplayName As String * 260: szTypeName As String * 80: End Type
Private Const LVM_FIRST = &H1000, LVM_SETIMAGELIST = LVM_FIRST + 3
Private Const LVSIL_SMALL = 1, SHGFI_SYSICONINDEX = &H4000, SHGFI_SMALLICON = &H1
Private Const SHGFI_USEFILEATTRIBUTES = &H10, FILE_ATTRIBUTE_NORMAL = &H80

Private Sub Form_Load()
  Dim sfi As SHFILEINFO, hSysIL As Long
  hSysIL = SHGetFileInfo("C:\", 0&, sfi, Len(sfi), SHGFI_SYSICONINDEX Or SHGFI_SMALLICON)
  If hSysIL <> 0 Then SendMessage ListView1.hWnd, LVM_SETIMAGELIST, LVSIL_SMALL, hSysIL
  ListView1.View = lvwReport
  ListView1.ColumnHeaders.Add , , "Path", 3500
  ' Example rows:
  AddPath "D:\test.txt": AddPath "D:\picture.jpg"
End Sub

Private Sub AddPath(ByVal p As String)
  Dim sfi As SHFILEINFO, flags As Long, exists As Boolean
  exists = (Dir$(p, vbNormal Or vbHidden Or vbSystem Or vbReadOnly) <> "")
  flags = SHGFI_SYSICONINDEX Or SHGFI_SMALLICON Or IIf(exists, 0, SHGFI_USEFILEATTRIBUTES)
  Call SHGetFileInfo(IIf(exists, p, p), IIf(exists, 0, FILE_ATTRIBUTE_NORMAL), sfi, Len(sfi), flags)
  With ListView1.ListItems.Add(, , p): .SmallIcon = sfi.iIcon: End With
End Sub

Notes:

This approach extends ’s idea so each path shows its own correct icon automatically.

Recommended Answers

All 7 Replies

Add icon to imagelist.
use icon picture (.ico) and change the "view" to "0-lvwIcon" on listview properties
assign icon image from imagelist to listview.

Jx Man thanks for attaching this project
please dont mind but i want to do this
example:
we use a listview and make two column tabs
listview1.ListItems.Add , , "D:\test.txt"
Now we want to show this file icon in left or right side in the listview
when add any path of file in listview then it will show his file icon
please help

you mean every added any path then there are icon set in left side of that items?

yes brother

Add one icon with dimension 16x16 to Imagelist.
Then add this code :

Private Sub Command1_Click()
On Error Resume Next

    With ListView1
        Set .Icons = ImageList
        Set .SmallIcons = ImageList
        Set View = .ListItems.Add(, , , 1, 1) ' Icon Added
        View.SubItems(1) = "This/Where/Path/Added/" & .ListItems.Count 'Path Added
        View.SubItems(2) = "Size " & .ListItems.Count 'Size Added
    End With
End Sub

Thanks
but you mean every file which we add the list box first we added all icons them

Yes. It means every you added new path the same icon will shown.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.