By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,994 Members | 2,073 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,994 IT Pros & Developers. It's quick & easy.

image listview

P: n/a
hi all,

is there a way to show the icons of a file, used by the system in a
listview.

now i have something like:

Private Sub AddFiles(ByVal nod As TreeNode)

Dim strPath As String = nod.FullPath

Dim strFile As String

With nod

For Each strFile In Directory.GetFiles(strPath)

With nod.Nodes.Add(Path.GetFileName(strFile))

..Tag = ItemType.File

..ImageIndex = 3

End With

Next

End With

End Sub

kind regards,

Maarten

Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi,

Use shgetfileinfo to get them. Here a listview control to the form
and name it lv. Here is an example.

Private Function getLocalIcons(ByVal szFolderPath As String)

Dim dirInfo As New System.IO.DirectoryInfo(szFolderPath)

Dim di As System.IO.DirectoryInfo

Dim fi As System.IO.FileInfo

Dim lvitem As ListViewItem

Dim hIcon As Icon

Dim cIcon As New clsGetIcon

Dim htIcons As New Hashtable

Dim intIndex As Integer

imlIcon.Images.Clear()

lv.Items.Clear()

lv.SmallImageList = imlIcon

For Each di In dirInfo.GetDirectories()

lvitem = lv.Items.Add(di.Name)

hIcon = cIcon.getIcon(di.FullName)

If htIcons.ContainsKey(hIcon) Then

intIndex = htIcons(hIcon)

Else

imlIcon.Images.Add(hIcon.ToBitmap)

intIndex = imlIcon.Images.Count - 1

End If

lvitem.ImageIndex = intIndex

Next

For Each fi In dirInfo.GetFiles()

lvitem = lv.Items.Add(fi.Name)

hIcon = cIcon.getIcon(fi.FullName)

If htIcons.ContainsKey(hIcon) Then

intIndex = htIcons(hIcon)

Else

imlIcon.Images.Add(hIcon.ToBitmap)

intIndex = imlIcon.Images.Count - 1

End If

lvitem.ImageIndex = intIndex

Next

End Function

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

lv.SuspendLayout()

getLocalIcons("C:\")

lv.ResumeLayout()

End Sub

The helper class.

Imports System.Runtime.InteropServices

Public Class clsGetIcon

Public Structure SHFILEINFO

Dim hIcon As IntPtr

Dim iIcon As Integer

Dim dwAttributes As Integer

<VBFixedString(260),
System.Runtime.InteropServices.MarshalAs(System.Ru ntime.InteropServices.UnmanagedType.ByValTStr,
SizeConst:=260)> Public szDisplayName As String

'String that contains the name of the file as it appears in the Microsoft®
Windows® Shell, or the path and file name of the file that contains the icon
representing the file.

<VBFixedString(80),
System.Runtime.InteropServices.MarshalAs(System.Ru ntime.InteropServices.UnmanagedType.ByValTStr,
SizeConst:=80)> Public szTypeName As String

End Structure
Private Declare Auto Function SHGetFileInfo Lib "shell32" (ByVal pszPath As
String, _

ByVal dwFileAttributes As Integer, ByRef psfi As SHFILEINFO, _

ByVal cbFileInfo As Integer, ByVal uFlags As Integer) As Integer

Private Const SHGFI_ICON As Integer = &H100

Private Const SHGFI_SMALLICON As Integer = &H1 'Small icon

Private Const SHGFI_TYPENAME As Integer = &H400 ' get type name

Public Function getIcon(ByVal szFilename As String) As Icon

Try

Dim aSHFileInfo As New SHFILEINFO

Dim cbFileInfo As Integer = _

Marshal.SizeOf(aSHFileInfo)

Dim uflags As Integer = SHGFI_ICON Or SHGFI_SMALLICON

SHGetFileInfo(szFilename, 0, aSHFileInfo, cbFileInfo, uflags)

Dim myIcon As Icon

myIcon = Icon.FromHandle(aSHFileInfo.hIcon)

aSHFileInfo.szTypeName = Space(255)

SHGetFileInfo(szFilename, 0, aSHFileInfo, cbFileInfo, SHGFI_TYPENAME)

Trace.WriteLine(aSHFileInfo.szTypeName)

Return myIcon

Catch ex As Exception

Debug.WriteLine(ex.ToString)

Return Nothing

End Try

End Function

End Class

Ken

---------------------------

"Maarten" <gu******@hotmail.com> wrote in message
news:41**********************@news.skynet.be...
hi all,

is there a way to show the icons of a file, used by the system in a
listview.

now i have something like:

Private Sub AddFiles(ByVal nod As TreeNode)

Dim strPath As String = nod.FullPath

Dim strFile As String

With nod

For Each strFile In Directory.GetFiles(strPath)

With nod.Nodes.Add(Path.GetFileName(strFile))

..Tag = ItemType.File

..ImageIndex = 3

End With

Next

End With

End Sub

kind regards,

Maarten


Nov 21 '05 #2

P: n/a
"Maarten" <gu******@hotmail.com> schrieb:
is there a way to show the icons of a file, used by the system in a
listview.


System Image List
<URL:http://vbaccelerator.com/article.asp?id=4303>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #3

P: n/a
thank you it works fine,

regards,
Maarten
"Ken Tucker [MVP]" <vb***@bellsouth.net> schreef in bericht
news:O#**************@TK2MSFTNGP11.phx.gbl...
Hi,

Use shgetfileinfo to get them. Here a listview control to the form and name it lv. Here is an example.

Private Function getLocalIcons(ByVal szFolderPath As String)

Dim dirInfo As New System.IO.DirectoryInfo(szFolderPath)

Dim di As System.IO.DirectoryInfo

Dim fi As System.IO.FileInfo

Dim lvitem As ListViewItem

Dim hIcon As Icon

Dim cIcon As New clsGetIcon

Dim htIcons As New Hashtable

Dim intIndex As Integer

imlIcon.Images.Clear()

lv.Items.Clear()

lv.SmallImageList = imlIcon

For Each di In dirInfo.GetDirectories()

lvitem = lv.Items.Add(di.Name)

hIcon = cIcon.getIcon(di.FullName)

If htIcons.ContainsKey(hIcon) Then

intIndex = htIcons(hIcon)

Else

imlIcon.Images.Add(hIcon.ToBitmap)

intIndex = imlIcon.Images.Count - 1

End If

lvitem.ImageIndex = intIndex

Next

For Each fi In dirInfo.GetFiles()

lvitem = lv.Items.Add(fi.Name)

hIcon = cIcon.getIcon(fi.FullName)

If htIcons.ContainsKey(hIcon) Then

intIndex = htIcons(hIcon)

Else

imlIcon.Images.Add(hIcon.ToBitmap)

intIndex = imlIcon.Images.Count - 1

End If

lvitem.ImageIndex = intIndex

Next

End Function

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

lv.SuspendLayout()

getLocalIcons("C:\")

lv.ResumeLayout()

End Sub

The helper class.

Imports System.Runtime.InteropServices

Public Class clsGetIcon

Public Structure SHFILEINFO

Dim hIcon As IntPtr

Dim iIcon As Integer

Dim dwAttributes As Integer

<VBFixedString(260),
System.Runtime.InteropServices.MarshalAs(System.Ru ntime.InteropServices.Unma
nagedType.ByValTStr, SizeConst:=260)> Public szDisplayName As String

'String that contains the name of the file as it appears in the Microsoft®
Windows® Shell, or the path and file name of the file that contains the icon representing the file.

<VBFixedString(80),
System.Runtime.InteropServices.MarshalAs(System.Ru ntime.InteropServices.Unma
nagedType.ByValTStr, SizeConst:=80)> Public szTypeName As String

End Structure
Private Declare Auto Function SHGetFileInfo Lib "shell32" (ByVal pszPath As String, _

ByVal dwFileAttributes As Integer, ByRef psfi As SHFILEINFO, _

ByVal cbFileInfo As Integer, ByVal uFlags As Integer) As Integer

Private Const SHGFI_ICON As Integer = &H100

Private Const SHGFI_SMALLICON As Integer = &H1 'Small icon

Private Const SHGFI_TYPENAME As Integer = &H400 ' get type name

Public Function getIcon(ByVal szFilename As String) As Icon

Try

Dim aSHFileInfo As New SHFILEINFO

Dim cbFileInfo As Integer = _

Marshal.SizeOf(aSHFileInfo)

Dim uflags As Integer = SHGFI_ICON Or SHGFI_SMALLICON

SHGetFileInfo(szFilename, 0, aSHFileInfo, cbFileInfo, uflags)

Dim myIcon As Icon

myIcon = Icon.FromHandle(aSHFileInfo.hIcon)

aSHFileInfo.szTypeName = Space(255)

SHGetFileInfo(szFilename, 0, aSHFileInfo, cbFileInfo, SHGFI_TYPENAME)

Trace.WriteLine(aSHFileInfo.szTypeName)

Return myIcon

Catch ex As Exception

Debug.WriteLine(ex.ToString)

Return Nothing

End Try

End Function

End Class

Ken

---------------------------

"Maarten" <gu******@hotmail.com> wrote in message
news:41**********************@news.skynet.be...
hi all,

is there a way to show the icons of a file, used by the system in a
listview.

now i have something like:

Private Sub AddFiles(ByVal nod As TreeNode)

Dim strPath As String = nod.FullPath

Dim strFile As String

With nod

For Each strFile In Directory.GetFiles(strPath)

With nod.Nodes.Add(Path.GetFileName(strFile))

.Tag = ItemType.File

.ImageIndex = 3

End With

Next

End With

End Sub

kind regards,

Maarten

Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.