Jump to content
Sign in to follow this  

Extracting icons

Recommended Posts


Hi everyone,

I've recently picked up a project I left quite some time ago. It was going to be a shortcut bar, but instead of being a bar it would have the shape of a ring.

Anyway I got quite far with the code but now I'm stuck and I really can't figure out how to continue/get this to work.

As the description says, I can't figure out how to extract icons from all sorts of stuff. I've searched the forums and the help files and I did find the "_WinAPI_ExtractIconEx" command, but I can't get it to work (or it doesn't work the way I think it works...). The icon needs to be drawn with "_WinAPI_DrawIconEx" after the extraction. The code does work with "_WinAPI_LoadShell32Icon", so the drawing of the icons should be fine.

The error I'm constantly getting is "_WinAPI_DrawIconEx: The operation completed successfully" and then it closes. So I guess something goes wrong with the extraction or I simply don't put the right variables in the right places.

I've looked at other scripts (mainly icon extractors and stuff) and at the example in the help file. The example is as good as useless, seeing how many icons shell32 has is easy, but when you extract a icon there apparently needs to be done much more. It would be nice to see a example of a icon extraction in the help file as well >_<

As for the scripts I looked at, they apparently don't use "_WinAPI_ExtractIconEx" command and I simply can't figure out where exactly the code is that extracts a icon.

Anyway I hope someone can give me a example (or point me to one) of a simple icon extraction from a shortcut or .exe.

Thanks in advance :(

Edited by CyberneticSoldier

Share this post

Link to post
Share on other sites

Hi, don't know if it helps but hopefully it can give you some insight to how it works

#include <WinAPI.au3>

Global $IconFile = @WindowsDir & "\explorer.exe"
Global $hGui, $IconCount, $tLarge, $pLarge, $tSmall, $pSmall, $hDC, $iX = 5, $iY = 5 

$hGui = GUICreate("", 725, 100)

; Checking To see if the file has icons
$IconCount = _WinAPI_ExtractIconEx($IconFile, -1, 0, 0, 0)
If $IconCount > 0 Then
    ; Create a Struct for Large icon handles
    $tLarge = DllStructCreate("int[" & $IconCount & "]")
    ; Get a pointer to the Large icon struct
    $pLarge =  DllStructGetPtr($tLarge)
    ; Create a struct for Small icon handles
    $tSmall = DllStructCreate("int[" & $IconCount & "]")
    ; Get a pointer to the Small icon struct
    $pSmall =  DllStructGetPtr($tSmall)
    ; Pass the small and large pointers along with how many icons to extract 
    $Extracted = _WinAPI_ExtractIconEx($IconFile, 0, $pLarge, $pSmall, $IconCount)
    ; Just Setting the window title with how many icon were extracted
    WinSetTitle($hGui, "",  "Icons Extracted: " & $Extracted)
    ; Get the Gui DC
    $hDC = _WinAPI_GetDC($hGui)

    ; Loop through the icon handles in the Large and Small struct and draw the icon on the gui.
    For $i = 1 To $IconCount
        _WinAPI_DrawIconEx($hDC, $iX, $iY,  DllStructGetData($tLarge, 1, $i), 32, 32)
        ; Destroy the large icon now it's drawn on the gui
        _WinAPI_DestroyIcon(DllStructGetData($tLarge, 1, $i))
        _WinAPI_DrawIconEx($hDC, $iX, $iY + 50,  DllStructGetData($tSmall, 1, $i), 16, 16)
        ; Destroy the small icon now it's drawn on the gui
        _WinAPI_DestroyIcon(DllStructGetData($tSmall, 1, $i))
        $iX += 40
    ; Release the GUI DC now I've finished drawing the icons
    _WinAPI_ReleaseDC($hGui, $hDC)
    ; If I'm here it means there were no icons found in the file
    WinSetTitle($hGui, "",  "Icons Extracted: 0")
Until GUIGetMsg() = -3


Share this post

Link to post
Share on other sites

Alright thanks smashly, that sure looks like a clear example to me >_<

I don't have autoit on this pc, but I'll try it as soon as I can.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this