Jump to content

Button with images


Holger
 Share

Recommended Posts

Cause often there are question how to use images on buttons I post my old scrap here.

From XP on Buttons do support Image_Lists.

More information you can find here:

http://windowssdk.msdn.microsoft.com/en-us...y/ms673340.aspx

In the following example I just use small icons (16x16) for the imagelist.

You can also use large icons or bmp-images (and maybe other formats) in these lists...

More infos here:

http://windowssdk.msdn.microsoft.com/en-us...y/ms671672.aspx

Here is now the scrap/beginning/code sample ;):

#include <GUIConstants.au3>

Global Const $ILC_MASK                      = 0x0001
Global Const $ILC_COLOR32                   = 0x0020

Global Const $BCM_FIRST                     = 0x1600
Global Const $BCM_SETIMAGELIST              = $BCM_FIRST + 0x0002

Global Const $BUTTON_IMAGELIST_ALIGN_LEFT   = 0
Global Const $BUTTON_IMAGELIST_ALIGN_RIGHT  = 1
Global Const $BUTTON_IMAGELIST_ALIGN_TOP    = 2
Global Const $BUTTON_IMAGELIST_ALIGN_BOTTOM = 3
Global Const $BUTTON_IMAGELIST_ALIGN_CENTER = 4 ; Doesn't draw text


$hGUI   = GUICreate("Colored button")

$btn1   = GUICtrlCreateButton("Button1", 10, 10, 90, 25)
Button_AddIcon($btn1, "shell32.dll", 6, $BUTTON_IMAGELIST_ALIGN_LEFT)

$btn2   = GUICtrlCreateButton("Button2", 10, 50, 90, 25)
Button_AddIcon($btn2, "shell32.dll", 21, $BUTTON_IMAGELIST_ALIGN_RIGHT)

$btn3   = GUICtrlCreateButton("Button3", 10, 90, 50, 50)
Button_AddIcon($btn3, "shell32.dll", 32, $BUTTON_IMAGELIST_ALIGN_TOP)

$btn4   = GUICtrlCreateButton("Button4", 10, 140, 50, 50)
Button_AddIcon($btn4, "shell32.dll", 14, $BUTTON_IMAGELIST_ALIGN_BOTTOM)

GUISetState()

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

Exit


Func Button_AddIcon($nID, $sIconFile, $nIconID, $nAlign)
    Local $hIL      = ImageList_Create(16, 16, BitOr($ILC_MASK, $ILC_COLOR32), 0, 1)
    
    Local $stIcon   = DllStructCreate("int")
    ExtractIconEx($sIconFile, $nIconID, DllStructGetPtr($stIcon), 0, 1)
    
    ImageList_AddIcon($hIL, DllStructGetData($stIcon, 1))
    DestroyIcon(DllStructGetData($stIcon, 1))
    
    Local $stBIL    = DllStructCreate("dword;int[4];uint")

    DllStructSetData($stBIL, 1, $hIL)
    
    DllStructSetData($stBIL, 2, 1, 1)
    DllStructSetData($stBIL, 2, 1, 2)
    DllStructSetData($stBIL, 2, 1, 3)
    DllStructSetData($stBIL, 2, 1, 4)

    DllStructSetData($stBIL, 3, $nAlign)
    
    GUICtrlSendMsg($nID, $BCM_SETIMAGELIST, 0, DllStructGetPtr($stBIL))
EndFunc


Func ImageList_Create($nImageWidth, $nImageHeight, $nFlags, $nInitial, $nGrow)
    Local $hImageList = DllCall('comctl32.dll', 'hwnd', 'ImageList_Create', _
                        'int', $nImageWidth, _
                        'int', $nImageHeight, _
                        'int', $nFlags, _
                        'int', $nInitial, _
                        'int', $nGrow)
    Return $hImageList[0]
EndFunc


Func ImageList_AddIcon($hIml, $hIcon)
    Local $nIndex = DllCall('comctl32.dll', 'int', 'ImageList_AddIcon', _
                    'hwnd', $hIml, _
                    'hwnd', $hIcon)
    Return $nIndex[0]
EndFunc


Func ImageList_Destroy($hIml)
    Local $bResult = DllCall('comctl32.dll', 'int', 'ImageList_Destroy', _
                    'hwnd', $hIml)
    Return $bResult[0]
EndFunc


Func ExtractIconEx($sIconFile, $nIconID, $ptrIconLarge, $ptrIconSmall, $nIcons)
    Local $nCount = DllCall('shell32.dll', 'int', 'ExtractIconEx', _
                    'str', $sIconFile, _
                    'int', $nIconID, _
                    'ptr', $ptrIconLarge, _
                    'ptr', $ptrIconSmall, _
                    'int', $nIcons)
    Return $nCount[0]
EndFunc


Func DestroyIcon($hIcon)
    Local $bResult = DllCall('user32.dll', 'int', 'DestroyIcon', _
                    'hwnd', $hIcon)
    Return $bResult[0]
EndFunc

Maybe it will be helpfull for somebody...good luck :whistle:

Greets

Holger

Link to comment
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
 Share

  • Recently Browsing   0 members

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