Hi,
Try something like this.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim arIcons As New ArrayList
Dim clsIco As New clsGetIcon
Dim diRoot As New IO.DirectoryInfo("C:\")
For Each fiCur As IO.FileInfo In diRoot.GetFiles
Dim c As New FileHelper
c.FileName = fiCur.Name
c.FileIcon = clsIco.getIcon(fiCur.FullName)
arIcons.Add(c)
Next
ListBox1.DrawMode = DrawMode.OwnerDrawFixed
ListBox1.DataSource = arIcons
ListBox1.DisplayMember = "FileName"
End Sub
Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As
System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
Dim g As Graphics = e.Graphics
Dim br As SolidBrush
Dim s As String
Dim c As FileHelper
Try
c = DirectCast(ListBox1.Items.Item(e.Index), FileHelper)
s = c.FileName
Catch ex As Exception
Trace.WriteLine(ex.ToString)
End Try
g.FillRectangle(Brushes.White, e.Bounds)
If CBool(e.State And DrawItemState.Selected) Then
g.FillRectangle(Brushes.LightBlue, e.Bounds)
End If
br = New SolidBrush(Color.Black)
g.DrawIcon(c.FileIcon, e.Bounds.Left, e.Bounds.Top)
g.DrawString(s, ListBox1.Font, br, e.Bounds.Left + 25, e.Bounds.Top)
br.Dispose()
End Sub
Helper classes
Public Class FileHelper
Dim ico As Icon
Dim strFile As String
Public Property FileIcon() As Icon
Get
Return ico
End Get
Set(ByVal Value As Icon)
ico = Value
End Set
End Property
Public Property FileName() As String
Get
Return strFile
End Get
Set(ByVal Value As String)
strFile = Value
End Set
End Property
End 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.Runtime.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.Runtime.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
---------------------
I now know how to gather the file type icons and I'm able to use them in a
listview. But a listbox does not have a .smallimagelist or .largeimagelist
member, so I don't know how to translate that so that I have the icon + the
string in a listbox. Is this possible, and if so, can you give me some idea
how to go about it.
Thanks for any help.
Bernie Yaeger