Showing Drive, Folder and File Icons using Shell32.DLL

  • Thread starter Thread starter MRM256
  • Start date Start date
M

MRM256

Guest
Hi Everyone,

I am working on a project where I need the ability to see hidden Folders and Files. None of the Open dialog controls have that ability, so I was forced to find a workaround.

The workaround I found was to have three list boxes; one for drives, another for directories(folders) and lastly for files. To make it look more professional, I replaced the Drive list box with a combo box. I can show the drive icons in the combo box. I also want to show the folder icons in the folders list, and ditto for file icons. The form is designed like:

1523961.png

When running...

1523962.png

At this time there isn't much code.

Imports System.Runtime.InteropServices
Imports System.IO
Public Class frmMain
#Region " Declare Ansi Function SHGetFileInfo"
Private Declare Ansi 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
#End Region

#Region "Structure SHFILEINFO"
Public Structure SHFILEINFO
Public hIcon As IntPtr
Public iIcon As Integer
Public dwAttributes As Integer
<MarshalAs(UnmanagedType.ByValTStr,
SizeConst:=260)> Public szDisplayName As String
<MarshalAs(UnmanagedType.ByValTStr,
SizeConst:=80)> Public szTypeName As String
End Structure
#End Region

Private Const SHGFI_ICON As Integer = &H100
Private Const SHGFI_SMALLICON As Integer = &H1
Private Const SHGFI_DISPLAYNAME As Integer = &H200
Private Const SHGFI_TYPENAME As Integer = &H400

Private Const FILE_ATTRIBUTE_NORMAL As Integer = &H80

Private WithEvents Img1 As New ImageList

Private Sub FrmMain_Load(sender As Object, e As EventArgs) Handles Me.Load
For Each fi In IO.Directory.GetLogicalDrives
Dim shinfo As New SHFILEINFO()

SHGetFileInfo(fi, FILE_ATTRIBUTE_NORMAL, shinfo,
Marshal.SizeOf(shinfo),
SHGFI_ICON + SHGFI_SMALLICON +
SHGFI_DISPLAYNAME + SHGFI_TYPENAME)
Dim myIcon As Icon = Nothing
myIcon = Icon.FromHandle(shinfo.hIcon)
Img1.Images.Add(myIcon)
Next

CboDrives.DrawMode = DrawMode.OwnerDrawVariable
CboDrives.ItemHeight = 20
CboDrives.Items.AddRange(IO.Directory.GetLogicalDrives)
End Sub

Private Sub CboDrives_DrawItem(sender As Object,
e As DrawItemEventArgs) _
Handles CboDrives.DrawItem

e.Graphics.DrawImage(img1.Images(e.Index),
New Point(1, e.Bounds.Top + 1))
e.Graphics.DrawString(CboDrives.Items(e.Index).ToString,
CboDrives.Font,
Brushes.Black,
18, e.Bounds.Top + 1)
End Sub

Private Sub CboDrives_SelectedIndexChanged(sender As Object,
e As EventArgs) _
Handles CboDrives.SelectedIndexChanged
FoldersList.Items.Clear()

Try
Dim drive As DriveInfo = DirectCast(CboDrives.SelectedItem, DriveInfo)
For Each dirInfo As DirectoryInfo In drive.RootDirectory.GetDirectories()
FoldersList.Items.Add(dirInfo)
Next

Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
End Class



I chose to use the Shell32.DLL, because it usually has all the standard icons a programmer needs.

Does someone know how to place the directory Names along with their icons in a list box using code similar to that shown in the Form_Load event?

Or if you have a better way please share :-).

Thanks in advance,





MRM256

Continue reading...
 
Back
Top