Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Image List 2

Status
Not open for further replies.

ICISYD

Programmer
Dec 18, 2002
36
0
0
GB
Do you know how to load an icon, for example "C:\Program Files\Internet Explorer\iexplorer.exe,1" into a image list? Any help would be much appreciated.
 
Phew.
I don't know if you can rip icons of exe files with the imagelist.
I do know that you can use the program resource hacker to rip icons from executibles, dll's etc.


You can save the icon as ... and load the .ico file in the imagelist.

Patrick.
 
Here's a way using API functions. It uses an invisible Picture control to receive the icon for transfer to an image list.
Code:
Public Declare Function ExtractIconEx Lib "shell32.dll" _
Alias "ExtractIconExA" (ByVal lpszFile As String, _
ByVal nIconIndex As Long, phiconLarge As Long, _
phiconSmall As Long, ByVal nIcons As Long) As Long

Public Declare Function DrawIcon Lib "user32" (ByVal hdc As Long, _
ByVal x As Long, ByVal y As Long, ByVal hIcon As Long) As Long

Public Declare Function DestroyIcon Lib "user32" _
(ByVal hIcon As Long) As Long
'________________________________
Sub sAddIconToImageList(ByVal strExe As String, _
ByVal intFallBackID As Integer)

    '--- Given the name of an exe or dll file,
    '    extracts the first small icon
    '    and loads it in the image list bound to the
    '    treeview and listview controls
    
    '--- Parameters
    '     [In]
    '     strExe: full path & filename of exe or dll
    '     containing the icon resource
    '     intFallBackID: id of icon in res file to use
    '     if no icons found in the exe

    Dim objImg As ListImage  'New list image object
    Dim lngRtn As Long       'API function return value
    Dim ahLIcon(1 To 1) As Long 'Array to receive large icon handles
    Dim ahSIcon(1 To 1) As Long  'Array to receive small icon handles

    'Just in case we get an 'invalid picture' error at run-time
    On Error GoTo LocalErr
    
    'Extract the first icon and small icon from the file
    lngRtn = ExtractIconEx(strExe, CLng(0), ahLIcon(1), ahSIcon(1), 1)
     'Clear the picture box
    Set picIcon.Picture = LoadPicture("")
    picIcon.AutoRedraw = True
Reenter:
    If ahSIcon(1) = 0 Then
        'No icon, load a fall back
        Set picIcon.Picture = LoadResPicture(intFallBackID, vbResIcon)
    Else
        'Draw the icon in the picture box
        lngRtn = DrawIcon(picIcon.hdc, 0, 0, ahSIcon(1))
        'Assign the drawn image to the picture property
        picIcon.Picture = picIcon.Image
    End If
    picIcon.AutoRedraw = False
    picIcon.Refresh
    
    'Add the icon to the image list from the picture control
    Set objImg = imgIcon.ListImages.Add _
    (, strExe & CStr(mintImgCount), picIcon.Picture)
    'Increment the module level counter
    mintImgCount = mintImgCount + 1
    
    GoTo ExitSub
    
LocalErr:
    If Err.Number = 481 Then
        'Invalid picture, load the fallback icon
        ahSIcon(1) = 0
        Resume Reenter
    Else
        'Tidy up
        DestroyIcon ahLIcon(1)
        DestroyIcon ahSIcon(1)
        Set objImg = Nothing
        'Turn off local handler
        On Error GoTo 0
        'Pass the error up the call stack
        Err.Raise Err.Number, Err.Description
    End If
    
ExitSub:
    DestroyIcon ahLIcon(1)
    DestroyIcon ahSIcon(1)
    Set objImg = Nothing
    
End Sub
I also recall seeing a thread in the last month or so explaining how to access the system icon list in memory but I couldn't find it.

Paul Bent
Northwind IT Systems
 
See also thread222-510876 for extracting icon from executables as .ico files.
 
Incredible.
So much programming for just a simple icon.
I would just download the resource hacker util (see my above post) and save as. This saves a lot of API stuff.

Patrick.
 
Thanks for all the help. The code will certainly be useful.
 

Then how about marking the post, giving it a star, for the helpful and expert posted code, so it stands out for others to see and receive help on when they have the same question and are searching for answers.

Just click on:

" Mark this post as a helpful/expert post!"

below the post with the expert answer.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top