473,387 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Icons in DLL's

Hi folks. I'm currently in the midst of building an explorer - like viewer
for a bigger project I'm going to be working on soon. When building the
class for my Treeview and Listview controls, I'm using the ExtractIconEx API
function (see code below). I found that all the icons windows uses are not
necessarily in the Shell32.dll file, though. I did some digging, and found
a few other places where Icon files are being pulled from to be displayed in
explorer. I'm just curious about the best way one would go about pulling
the icon files out of multiple places, based upon either the folder icon (or
control panel icon) or the files in the folders (for the listview). I'd
rather not create my own library of icons to release with the program, as it
can get big, and I don't see the point when most of them come with windows
anyway. What's the best way to go about getting (from the correct place)
and displaying icons in a treeview (for folders, drives, etc...) and
listview (for folders and files) controls?

Thanks for any advice.

-Jason

Private Declare Function ExtractIconEx Lib "Shell32.dll" Alias
"ExtractIconExA" _
(ByVal lpszFile As String, ByVal nIconIndex As Integer, _
<[In](), Out()> ByVal phIconLarge() As Integer, _
<[In](), Out()> ByVal phIconSmall() As Integer, _
ByVal nIcons As Integer) As Integer

Private Sub GetSystemIconList()
Try
Dim iIndex As Integer
Dim strPath As String
Const iArrUB As Integer = 250
Dim hLargeIcon(iArrUB) As Integer
Dim hSmallIcon(iArrUB) As Integer
Dim IconHnd As IntPtr
Dim iFetchIcons As Integer

strPath = "%systemroot%\system32\shell32.dll"

iFetchIcons = ExtractIconEx(strPath, 0, hLargeIcon, hSmallIcon,
iArrUB)

For iIndex = 0 To iFetchIcons - 1
SmallSystemIconList.Images.Add(Bitmap.FromHicon(Ic onHnd.op_Explicit(hSmallIcon(iIndex))))
LargeSystemIconList.Images.Add(Bitmap.FromHicon(Ic onHnd.op_Explicit(hLargeIcon(iIndex))))
Next

Catch ex As Exception

End Try
End Sub
Nov 21 '05 #1
2 1984
Hi,

Use shgetfileinfo to get them. Add 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

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

"OpticTygre" <op********@adelphia.net> wrote in message
news:ss********************@adelphia.com...
Hi folks. I'm currently in the midst of building an explorer - like viewer
for a bigger project I'm going to be working on soon. When building the
class for my Treeview and Listview controls, I'm using the ExtractIconEx API
function (see code below). I found that all the icons windows uses are not
necessarily in the Shell32.dll file, though. I did some digging, and found
a few other places where Icon files are being pulled from to be displayed in
explorer. I'm just curious about the best way one would go about pulling
the icon files out of multiple places, based upon either the folder icon (or
control panel icon) or the files in the folders (for the listview). I'd
rather not create my own library of icons to release with the program, as it
can get big, and I don't see the point when most of them come with windows
anyway. What's the best way to go about getting (from the correct place)
and displaying icons in a treeview (for folders, drives, etc...) and
listview (for folders and files) controls?

Thanks for any advice.

-Jason

Private Declare Function ExtractIconEx Lib "Shell32.dll" Alias
"ExtractIconExA" _
(ByVal lpszFile As String, ByVal nIconIndex As Integer, _
<[In](), Out()> ByVal phIconLarge() As Integer, _
<[In](), Out()> ByVal phIconSmall() As Integer, _
ByVal nIcons As Integer) As Integer

Private Sub GetSystemIconList()
Try
Dim iIndex As Integer
Dim strPath As String
Const iArrUB As Integer = 250
Dim hLargeIcon(iArrUB) As Integer
Dim hSmallIcon(iArrUB) As Integer
Dim IconHnd As IntPtr
Dim iFetchIcons As Integer

strPath = "%systemroot%\system32\shell32.dll"

iFetchIcons = ExtractIconEx(strPath, 0, hLargeIcon, hSmallIcon,
iArrUB)

For iIndex = 0 To iFetchIcons - 1
SmallSystemIconList.Images.Add(Bitmap.FromHicon(Ic onHnd.op_Explicit(hSmallIcon(iIndex))))
LargeSystemIconList.Images.Add(Bitmap.FromHicon(Ic onHnd.op_Explicit(hLargeIcon(iIndex))))
Next

Catch ex As Exception

End Try
End Sub

Nov 21 '05 #2
Hi Ken,

Thanks for the time you put into this! I'm sure it can help a lot of other
people out as well. I'll definately check out all the code. Thanks again!

-Jason

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi,

Use shgetfileinfo to get them. Add 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

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

"OpticTygre" <op********@adelphia.net> wrote in message
news:ss********************@adelphia.com...
Hi folks. I'm currently in the midst of building an explorer - like
viewer
for a bigger project I'm going to be working on soon. When building the
class for my Treeview and Listview controls, I'm using the ExtractIconEx
API
function (see code below). I found that all the icons windows uses are
not
necessarily in the Shell32.dll file, though. I did some digging, and
found
a few other places where Icon files are being pulled from to be displayed
in
explorer. I'm just curious about the best way one would go about pulling
the icon files out of multiple places, based upon either the folder icon
(or
control panel icon) or the files in the folders (for the listview). I'd
rather not create my own library of icons to release with the program, as
it
can get big, and I don't see the point when most of them come with windows
anyway. What's the best way to go about getting (from the correct place)
and displaying icons in a treeview (for folders, drives, etc...) and
listview (for folders and files) controls?

Thanks for any advice.

-Jason

Private Declare Function ExtractIconEx Lib "Shell32.dll" Alias
"ExtractIconExA" _
(ByVal lpszFile As String, ByVal nIconIndex As Integer, _
<[In](), Out()> ByVal phIconLarge() As Integer, _
<[In](), Out()> ByVal phIconSmall() As Integer, _
ByVal nIcons As Integer) As Integer

Private Sub GetSystemIconList()
Try
Dim iIndex As Integer
Dim strPath As String
Const iArrUB As Integer = 250
Dim hLargeIcon(iArrUB) As Integer
Dim hSmallIcon(iArrUB) As Integer
Dim IconHnd As IntPtr
Dim iFetchIcons As Integer

strPath = "%systemroot%\system32\shell32.dll"

iFetchIcons = ExtractIconEx(strPath, 0, hLargeIcon, hSmallIcon,
iArrUB)

For iIndex = 0 To iFetchIcons - 1

SmallSystemIconList.Images.Add(Bitmap.FromHicon(Ic onHnd.op_Explicit(hSmallIcon(iIndex))))

LargeSystemIconList.Images.Add(Bitmap.FromHicon(Ic onHnd.op_Explicit(hLargeIcon(iIndex))))
Next

Catch ex As Exception

End Try
End Sub

Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Eric | last post by:
I want to use embedded icon resources for my file type associations, but only the app.ico seems to show up when I go to choose the icons in the setup File Types editor. The same thing when I try...
1
by: Krazitchek | last post by:
Hi, how to navigate between icons stored in shell32.dll (or other .dll), choose one of them with the index, and one particular format of the choosen icon (16 colors, 256 colors, XP) ? How to...
2
by: emde | last post by:
Any tips on using the icons in shell32.dll as the image on a button in a form? Thanks.
6
by: B-Dog | last post by:
Does anyone know where I can find some professional looking icons that are the standard windows collection for developers? The ones that came on the VS cd are pretty lame. Thanks
1
by: Henrik | last post by:
I'm sorry to trouble you once again but somebody must know how to get a list of the desktop icons and their respective positions. I know that I have to look into shell but I have no idea how to...
4
by: JR | last post by:
Hi, How can I add multible icons into 1 file likeSHELL32.dll from windows it has about 50 Jan
1
by: Jon Slaughter | last post by:
I'm trying to create a windows explorer like app and the problem I'm having is getting the proper icon for the folders and items. In windows explorer there are many different icons and most seem...
2
by: Pebeco2 | last post by:
Is there a procedure to add Icons from clip art or other internet downloads to "shell32.dll" so they may bused for desk top Icons? Or is there another way to use downloaded pics to use as desk...
5
by: optictygre | last post by:
Where can I find the dll or exe which contains the embedded Visual Studio 2005 Icons that the application uses? I know that VS comes with an icon library, but practically none of the icons which...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.