Sign in to follow this  
Followers 0
supersonic

Using icons larger than 32x32 pixel in buttons?

12 posts in this topic

Hi,

is it possible to use icons larger than 32x32 pixel with buttons created with GUICtrlCreateButton()?

I'm looking for using icons NOT bitmaps...

Greets,

-supersonic.

Share this post


Link to post
Share on other sites



Did you look in the "user defined functions" in the help file?

#Include <GuiButton.au3>
_GUICtrlButton_SetImage($hWnd, $sImageFile[,  $nIconId = -1[, $fLarge = False]])

Share this post


Link to post
Share on other sites

Yes I did. When using "False" for $fLarge the icon is resized to 16x16.

When using "True" the icon will be 32x32.

Did you look in the "user defined functions" in the help file?

#Include <GuiButton.au3>
_GUICtrlButton_SetImage($hWnd, $sImageFile[,  $nIconId = -1[, $fLarge = False]])

Share this post


Link to post
Share on other sites

This code displays a 48x48 icon for me.

#include <GUIConstantsEx.au3>
#include <GuiButton.au3>
#include <WindowsConstants.au3>

GUICreate("Buttons", 300, 300)
GUISetState()

$btn = GUICtrlCreateButton("Button1", 10, 10, 64, 64, $BS_ICON)
_GUICtrlButton_SetImage($btn, @DesktopDir & "\106.ico")

While 1
    $Msg = GUIGetMsg()
    If $Msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd

Share this post


Link to post
Share on other sites

I think there may be a problem in the call to _GUICtrlButton_SetImage when you specify the icon id to extract. In that function there is a call to _WinAPI_ExtractIconEx which always returns an error on an *.ico file.

Share this post


Link to post
Share on other sites

Replace _WinAPI_ExtractIconEx() at _WinAPI_PrivateExtractIcon() and it works.

Func _WinAPI_PrivateExtractIcon($sIcon, $iIndex, $iWidth, $iHeight)

    Local $hIcon, $tIcon = DllStructCreate('hwnd'), $tID = DllStructCreate('hwnd')
    Local $Ret = DllCall('user32.dll', 'int', 'PrivateExtractIcons', 'str', $sIcon, 'int', $iIndex, 'int', $iWidth, 'int', $iHeight, 'ptr', DllStructGetPtr($tIcon), 'ptr', DllStructGetPtr($tID), 'int', 1, 'int', 0)

    If (@error) Or ($Ret[0] = 0) Then
        Return SetError(1, 0, 0)
    EndIf

    $hIcon = DllStructGetData($tIcon, 1)

    If ($hIcon = Ptr(0)) Or (Not IsPtr($hIcon)) Then
        Return SetError(1, 0, 0)
    EndIf
    Return $hIcon
EndFunc   ;==>_WinAPI_PrivateExtractIcon

Share this post


Link to post
Share on other sites

Thanks for the reply. :-)

Please, can you give me an example how to implement this function?

Replace _WinAPI_ExtractIconEx() at _WinAPI_PrivateExtractIcon() and it works.

Func _WinAPI_PrivateExtractIcon($sIcon, $iIndex, $iWidth, $iHeight)

    Local $hIcon, $tIcon = DllStructCreate('hwnd'), $tID = DllStructCreate('hwnd')
    Local $Ret = DllCall('user32.dll', 'int', 'PrivateExtractIcons', 'str', $sIcon, 'int', $iIndex, 'int', $iWidth, 'int', $iHeight, 'ptr', DllStructGetPtr($tIcon), 'ptr', DllStructGetPtr($tID), 'int', 1, 'int', 0)

    If (@error) Or ($Ret[0] = 0) Then
        Return SetError(1, 0, 0)
    EndIf

    $hIcon = DllStructGetData($tIcon, 1)

    If ($hIcon = Ptr(0)) Or (Not IsPtr($hIcon)) Then
        Return SetError(1, 0, 0)
    EndIf
    Return $hIcon
EndFunc   ;==>_WinAPI_PrivateExtractIcon

Share this post


Link to post
Share on other sites

look up yashied's script in the examples. He give a couple of examples there.

Share this post


Link to post
Share on other sites

Try this. (I have not tried it)

#Include <GUIConstantsEx.au3>
#Include <GuiButton.au3>
#Include <WindowsConstants.au3>

GUICreate("Buttons", 300, 300)
GUISetState()

$btn = GUICtrlCreateButton("Button1", 10, 10, 64, 64, $BS_ICON)
_GUICtrlButton_SetIcon($btn, @DesktopDir & "\106.ico", 0, 48, 48)

While 1
    $Msg = GUIGetMsg()
    If $Msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd

Func _GUICtrlButton_SetIcon($hWnd, $sIcon, $iIndex, $iWidth, $iHeight)
    If Not IsHWnd($hWnd) Then
        $hWnd = GUICtrlGetHandle($hWnd)
        If $hWnd = 0 Then
            Return False
        EndIf
    EndIf
    Local $hIcon = _WinAPI_PrivateExtractIcon($sIcon, $iIndex, $iWidth, $iHeight)
    If @error Then
        Return False
    EndIf
    Local $hPrev = _SendMessage($hWnd, 0xF7, 1, $hIcon)
    If $hPrev Then
        If Not _WinAPI_DeleteObject($hPrev) Then
            _WinAPI_DestroyIcon($hPrev)
        EndIf
    EndIf
    _WinAPI_UpdateWindow($hWnd)
    Return True
EndFunc   ;==>_GUICtrlButton_SetIcon

Func _WinAPI_PrivateExtractIcon($sIcon, $iIndex, $iWidth, $iHeight)
    Local $hIcon, $tIcon = DllStructCreate('hwnd'), $tID = DllStructCreate('hwnd')
    Local $Ret = DllCall('user32.dll', 'int', 'PrivateExtractIcons', 'str', $sIcon, 'int', $iIndex, 'int', $iWidth, 'int', $iHeight, 'ptr', DllStructGetPtr($tIcon), 'ptr', DllStructGetPtr($tID), 'int', 1, 'int', 0)
    If (@error) Or ($Ret[0] = 0) Then
        Return SetError(1, 0, 0)
    EndIf
    $hIcon = DllStructGetData($tIcon, 1)
    If ($hIcon = Ptr(0)) Or (Not IsPtr($hIcon)) Then
        Return SetError(1, 0, 0)
    EndIf
    Return $hIcon
EndFunc   ;==>_WinAPI_PrivateExtractIcon
1 person likes this

Share this post


Link to post
Share on other sites

It work's! :-) Thank you soo much!

Try this. (I have not tried it)

#Include <GUIConstantsEx.au3>
#Include <GuiButton.au3>
#Include <WindowsConstants.au3>

GUICreate("Buttons", 300, 300)
GUISetState()

$btn = GUICtrlCreateButton("Button1", 10, 10, 64, 64, $BS_ICON)
_GUICtrlButton_SetIcon($btn, @DesktopDir & "\106.ico", 0, 48, 48)

While 1
    $Msg = GUIGetMsg()
    If $Msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd

Func _GUICtrlButton_SetIcon($hWnd, $sIcon, $iIndex, $iWidth, $iHeight)
    If Not IsHWnd($hWnd) Then
        $hWnd = GUICtrlGetHandle($hWnd)
        If $hWnd = 0 Then
            Return False
        EndIf
    EndIf
    Local $hIcon = _WinAPI_PrivateExtractIcon($sIcon, $iIndex, $iWidth, $iHeight)
    If @error Then
        Return False
    EndIf
    Local $hPrev = _SendMessage($hWnd, 0xF7, 1, $hIcon)
    If $hPrev Then
        If Not _WinAPI_DeleteObject($hPrev) Then
            _WinAPI_DestroyIcon($hPrev)
        EndIf
    EndIf
    _WinAPI_UpdateWindow($hWnd)
    Return True
EndFunc   ;==>_GUICtrlButton_SetIcon

Func _WinAPI_PrivateExtractIcon($sIcon, $iIndex, $iWidth, $iHeight)
    Local $hIcon, $tIcon = DllStructCreate('hwnd'), $tID = DllStructCreate('hwnd')
    Local $Ret = DllCall('user32.dll', 'int', 'PrivateExtractIcons', 'str', $sIcon, 'int', $iIndex, 'int', $iWidth, 'int', $iHeight, 'ptr', DllStructGetPtr($tIcon), 'ptr', DllStructGetPtr($tID), 'int', 1, 'int', 0)
    If (@error) Or ($Ret[0] = 0) Then
        Return SetError(1, 0, 0)
    EndIf
    $hIcon = DllStructGetData($tIcon, 1)
    If ($hIcon = Ptr(0)) Or (Not IsPtr($hIcon)) Then
        Return SetError(1, 0, 0)
    EndIf
    Return $hIcon
EndFunc   ;==>_WinAPI_PrivateExtractIcon

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  
Followers 0