How can i put a exe's icon in a picture box??
Printable View
How can i put a exe's icon in a picture box??
Umm, If I want to get a programs Icon, I usually take a good picture of it in Windows Explorer using Print Screen, or you can find out what sort of images are stored in an exe by changing a programs icons, again using the Print Screen button.
Does anyone know a better way??
There's an Extracticon API call that can do this thus:
Which you could adapt to your needs....Code:Option Explicit
Private Declare Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconA" (ByVal hInst As Long, ByVal lpszExeFileName As String, ByVal nIconIndex As Long) As Long
'\\ --[IconsFromFilename]-------------------------------------------------------------------
'\\ Returns a collection of ApiIcon objects from the filename given.
'\\ ----------------------------------------------------------------------------------------
'\\ (c) 2001 - Merrion Computing. All rights to use, reproduce or publish this code reserved
'\\ Please check http://www.merrioncomputing.com for updates.
'\\ ----------------------------------------------------------------------------------------
Public Property Get IconsFromExeFilename(ByVal Filename As String) As Collection
Dim lIndex As Long
Dim lIconCount As Long
Dim lRet As Long
Dim colIcons As Collection
Dim thisIcon As ApiIcon
'\\ Initialise the collection
Set colIcons = New Collection
'\\ Get the number of items
lIconCount = ExtractIcon(App.hInstance, Filename, -1)
If lIconCount > 0 Then
For lIndex = 0 To lIconCount - 1
lRet = ExtractIcon(App.hInstance, Filename, lIndex)
If lRet > 0 Then
Set thisIcon = New ApiIcon
thisIcon.hIcon = lRet
colIcons.Add thisIcon
End If
Next lIndex
End If
HTH,
Duncan
With the example that you posted how do you use the icons??
Eg I would like
Also it doesnt quite work as the ApiIcon type is not declared.
ApiIcon is part of the EventVB.dll - however here is some code you would probably find more useful:
You need a form with a picture box (called Picture1) with AutoRedraw set to true, a filelist called File1 with mask set to *.exe and a drive list box called drive1.
HTH,Code:Option Explicit
Private Declare Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconA" (ByVal hInst As Long, ByVal lpszExeFileName As String, ByVal nIconIndex As Long) As Long
Private Declare Function DrawIconApi Lib "user32" Alias "DrawIcon" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal hIcon As Long) As Long
Private Sub Dir1_Change()
File1.Path = Dir1.Path
End Sub
Private Sub File1_Click()
Call DrawIconsFromExeFilename(File1.Filename)
End Sub
'\\ --[DrawIconsFromFilename]---------------------------------------------
'\\ (c) 2001 - Merrion Computing. All rights to use, reproduce or publish this code reserved
'\\ Please check http://www.merrioncomputing.com for updates.
'\\ --------------------------------------------------------------------------------
Public Sub DrawIconsFromExeFilename(ByVal Filename As String)
Dim lIndex As Long
Dim lIconCount As Long
Dim lRet As Long
Dim x As Long, y As Long
Picture1.Cls
'\\ Get the number of items
lIconCount = ExtractIcon(App.hInstance, Filename, -1)
If lIconCount > 0 Then
For lIndex = 0 To lIconCount - 1
lRet = ExtractIcon(App.hInstance, Filename, lIndex)
If lRet > 0 Then
Call DrawIconApi(Picture1.hdc, x, y, lRet)
If x > 200 Then
x = 0
y = y + 40
Else
x = x + 40
End If
End If
Next lIndex
End If
End Sub
Duncan