Jump to content

Sending "_WinAPI_ExtractIconEx" with "GUICtrlSendMsg" to "GUICtrlCreateIcon"


Go to solution Solved by ioa747,

Recommended Posts

Posted

Hello.
I'm new to the forum and not an experienced coder, but over the course of more than ten years I've already created a few little helpers with AutoIt. A big thank you to the developers of and the community around AutoIt, which I can no longer do without. The many posts here in the forum have always been a great help to me.

Now I have a question for which I couldn't find anything in the forum. It relates to "Button Icons With _WinAPI_ExtractIconEx" from Bilgus:

https://www.autoitscript.com/forum/topic/193150-button-icons-with-_winapi_extracticonex/

There, icons extracted via _WinAPI_ExtractIconEx are sent via GUICtrlSendMsg to buttons created with GUICtrlCreateButton. The method used can obviously not be transferred to icon controls (GUICtrlCreateIcon) without further ado. Can this work at all? Unfortunately, I don't understand it.

I have reduced Bilgus' script to the bare essentials as an example to demonstrate both in comparison. Perhaps someone knows the solution to this, if it works at all as I imagine.

Thanks again to everyone.

 

#include <GuiButton.au3>
#include <GuiConstantsEx.au3>
#include <StaticConstants.au3>
#include <WinAPITheme.au3>
#include <WindowsConstants.au3>

$hMainGUI = GUICreate ("Example", 300, 300)
GUISetBkColor (0xFFFFFF, $hMainGUI)
GUISetState (@SW_SHOW, $hMainGUI)

$iIconCount = _WinAPI_ExtractIconEx ("shell32.dll", -1, 0, 0, 0)
$tResIcons  = DllStructCreate ("HANDLE[" & $iIconCount & "]")
$pResIcons  = DllStructGetPtr ($tResIcons)
$iIconCount = _WinAPI_ExtractIconEx ("shell32.dll", 0, $pResIcons, 0, $iIconCount)

$idButton = GUICtrlCreateButton ("", 50, 50, 45, 45, $BS_ICON)
GUICtrlSetImage (-1, "shell32.dll", 16781)
$idIcon   = GUICtrlCreateIcon ("shell32.dll", 2, 50, 170, 45, 45)
GUICtrlSetImage (-1, "shell32.dll", 16780)

Sleep (1000)

; This works.
GUICtrlSendMsg ($idButton, $BM_SETIMAGE, $IMAGE_ICON, DllStructGetData ($tResIcons, 1, 297))
GUICtrlCreateLabel ("This works.", 50, 100)

Sleep (1000)

; Does not work.
GUICtrlSendMsg ($idIcon, $BM_SETIMAGE, $IMAGE_ICON, DllStructGetData ($tResIcons, 1, 297))
GUICtrlCreateLabel ("Does not work.", 50, 220)

For $i = 1 To $iIconCount
    _WinAPI_DestroyIcon (DllStructGetData ($tResIcons, 1, $i))
Next

While True
    $nMsg = GUIGetMsg ()
    If $nMsg = $GUI_EVENT_CLOSE Then Exit
WEnd

 

Example.au3

Posted
  • Solution
Posted (edited)

 

; This works. -> because it's a button, and $BM_SETIMAGE = 0xF7 is ButtonConstants 
GUICtrlSendMsg ($idButton, $BM_SETIMAGE, $IMAGE_ICON, DllStructGetData ($tResIcons, 1, 297))
GUICtrlCreateLabel ("This works.", 50, 100)

Sleep (1000)

; Does not work.  -> because it's not a button,  -> $STM_SETIMAGE = 0x0172 is StaticConstants -> Now works
GUICtrlSendMsg ($idIcon, $STM_SETIMAGE, $IMAGE_ICON, DllStructGetData ($tResIcons, 1, 297))
GUICtrlCreateLabel ("Now works.", 50, 220)

https://learn.microsoft.com/en-us/windows/win32/controls/individual-control-info

#include <ButtonConstants.au3>  ; C:\Program Files (x86)\AutoIt3\Include\ButtonConstants.au3
#include <StaticConstants.au3>     ; C:\Program Files (x86)\AutoIt3\Include\StaticConstants.au3

Edited by ioa747

I know that I know nothing

Posted

@ioa747 and @Nine

Great, thank you both. Both solutions work perfectly. I'll keep them both in mind in case they produce different results under different circumstances.

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...