Hi,
Since the imagelist.image s.contains is not support the best
suggestion I have is to create a hashtable. Store the index of icon in the
hashtable with the icon as the key. Use the hashtable.conta ins method to
see if you already added the icon. Quick example.
Private Declare Function ExtractIconEx Lib "shell32.dl l" Alias
"ExtractIconExA " _
(ByVal lpszFile As String, ByVal nIconIndex As Integer, _
ByVal phiconLarge() As IntPtr, ByVal phiconSmall() As IntPtr, _
ByVal nIcons As Integer) As Integer
Dim hticons As New Hashtable
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
Dim arIcons As New ArrayList
Dim strPath As String
ImageList1.Imag es.Clear()
ImageList1.Imag eSize = New Size(32, 32)
strPath = "C:\Windows\Exp lorer.exe"
arIcons = GetIcon(strPath )
Me.Text = strPath & " icons"
Dim clsIcon As clsIconsFromFil e
Dim y As Integer = 0
For Each clsIcon In arIcons
ImageList1.Imag es.Add(clsIcon. Large)
hticons.Add(cls Icon.Large, y)
y += 1
Dim fs As New System.IO.FileS tream(y.ToStrin g & ".ico", IO.FileMode.Cre ate)
clsIcon.Large.S ave(fs)
fs.Close()
Next
Dim ico As Icon
ico = DirectCast(arIc ons.Item(0), clsIconsFromFil e).Large
Debug.WriteLine (hticons.Contai nsKey(ico))
ListView1.Items .Clear()
Dim x As Integer
For x = 0 To ImageList1.Imag es.Count - 1
Dim lvi As New ListViewItem
lvi.ImageIndex = x
ListView1.Items .Add(lvi)
Next
End Sub
Private Function GetIcon(ByVal Path As String) As ArrayList
Dim Large() As IntPtr
Dim Small() As IntPtr
Dim i, x As Integer
Dim cIcon As clsIconsFromFil e
Dim arIcons As ArrayList
i = ExtractIconEx(P ath, -1, Large, Small, -1) ' get number of icons
ReDim Large(i - 1)
ReDim Small(i - 1)
i = ExtractIconEx(P ath, 0, Large, Small, i)
arIcons = New ArrayList
For x = 0 To i - 1
cIcon = New clsIconsFromFil e
cIcon.Large = Icon.FromHandle (Large(x))
cIcon.Small = Icon.FromHandle (Small(x))
arIcons.Add(cIc on)
Next x
Return arIcons
End Function
' Here is the helper class
Public Class clsIconsFromFil e
Private m_icLarge As Icon
Private m_icSmall As Icon
Public Property Large() As Icon
Get
Return m_icLarge
End Get
Set(ByVal Value As Icon)
m_icLarge = Value
End Set
End Property
Public Property Small() As Icon
Get
Return m_icSmall
End Get
Set(ByVal Value As Icon)
m_icSmall = Value
End Set
End Property
End Class
Ken
--------------------------
" Just Me" <ne********@a-znet.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
If I add an image to an imagelist using
imageList.Image s.Add...
And use it with a tree node
tn.ImageIndex = imageList.Image s.Count-1
This is the actual code:
Win32.Shell.SHG etFileInfo(Node Path, 256, fileinfo, cbFileInfo, flags)
tvFolders.Image List.Images.Add (Icon.FromHandl e(fileinfo.hIco n))
tn.ImageIndex = tvFolders.Image List.Images.Cou nt - 1
It may be that the image is already in the imagelist.
I'd like to check.
The following was suggested to get the index:
Index = imageList.Image s.IndexOf(myIma ge)
But I find IndexOf is not supported.
The only thing I can think of is to save hIcon in an two column array
along
with Count=1 and check before I add to the imagelist. But if the imagelist
can tell me if it's already in it I'd rather not have an extra data
structure around.
1)Anyone know how to check the ImageList??
2)If I must have the data structure is an array (hIcon,index) the best
one?