First we need to add this, OBSERVE that this is in the Class, not outside, this is not an import.
Declare Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconExA" (ByVal lpszFile As String, ByVal nIconIndex As Integer, ByRef phiconLarge As Integer, ByRef phiconSmall As Integer, ByVal nIcons As Integer) As Integer
This is what we use to get the handle from the icons so we can create the icons.
Create a new function:
Private Function ReturnIcon(ByVal Path As String, ByVal Index As Integer, Optional ByVal small As Boolean = False) As Icon Dim bigIcon As Integer Dim smallIcon As Integer
Path is the path where the .exe is on your computer, Index is which of the exe file's icons we want starting at 0, we will create the program so if it fail to get the wanted icon it will try to get the icon at index 0 instead. Small is optional and will make the function return the small version of the icon instead. bigIcon and smallIcon is not really the icons, they are integers and will be used to create the Icon we want to return.
Try to extract icon with the right path and index:
ExtractIcon(Path, Index, bigIcon, smallIcon, 1)
If this worked, bigIcon and smallIcon have now changed in value.
If it didn't work it's still 0, and if that's the case:
If bigIcon = 0 Then ExtractIcon(Path, 0, bigIcon, smallIcon, 1) End If
...try to get the icon at index 0.
If one of the above thing worked we can use one of the integers to get the icon we want, else return nothing:
If bigIcon <> 0 Then If small = False Then Return Icon.FromHandle(bigIcon) Else Return Icon.FromHandle(smallIcon) End If Else Return Nothing End If
Here it creates an icon from handle with the bigIcon integer or the smallIcon integer. The Icon created is returned as the function's value.
Then end the function:
End Function
And there we go, here's an example how to use it, observe that on error will occur if the function returns nothing:
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.BackgroundImage = ReturnIcon("C:\Program Files\Internet Explorer\iexplore.exe", 0).ToBitmap
End Sub
Here I just add the icon to the background of the mainform just for test. Remember if you want to use the icons in a picturebox or something like that add .ToBitmap at the end as shown above.
I'm sure you can find this usefully :D


Sign In
Create Account


Back to top









