Jump to content
Sign in to follow this  

Icon button with a bg pic that toggles?

Recommended Posts



I am using MrCreator's GUICtrlSetOnHover and Yashied's Icons.au3 to create tri-state buttons. All works well for me in a standard "show this pic until hovered and then show this pic..." -setup.

Now what I'm trying to create is a tri-state icon button. The icon images will be static and only the bgs will swap so I thought I could use "same setup as before + icon over it" ..but I ran into a snag. Similar to a toolbar, some buttons have no "up state" image. You will only see the icon until being moused over then the bg image also appears to indicate that it is active. I couldn't figure out how to trigger a hover event for the button with no visible or enabled item to hover?

.. while on the subject,.. does anybody happen to know if it would be more demanding on an application to use a png for the button's icon rather than an ico?


Yashied's original code:

#Include <GUICtrlSetOnHover.au3>
#Include <GUIConstantsEx.au3>
#Include <GDIPlus.au3>

Opt('MustDeclareVars', 1)

Global $hForm, $Pic1, $hPic1, $Pic2, $hPic2, $nMsg


$hForm = GUICreate('Form1', 625, 467, 192, 124)
$Pic1 = GUICtrlCreatePic(@ScriptDir & '\options_logo.jpg', 0, 0, 196, 466)
$hPic1 = GUICtrlGetHandle($Pic1)
GUICtrlSetState(-1, $GUI_DISABLE)
$Pic2 = GUICtrlCreatePic('', 32, 112, 162, 59)
$hPic2 = GUICtrlGetHandle($Pic2)
_SetImageHover($Pic2, @ScriptDir & '\performance.png')
GUICtrlSetOnHover($Pic2, 'HoverPic', 'LeaveHoverPic')

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE

Func HoverPic($CtrlID)
        Case $CtrlID = $Pic2
            _WinAPI_InvalidateRect($hPic1, _ControlGetRect($hPic2))
            _SetImageHover($Pic2, @ScriptDir & '\performance_over.png')
EndFunc   ;==>HoverPic

Func LeaveHoverPic($CtrlID)
        Case $CtrlID = $Pic2
            _WinAPI_InvalidateRect($hPic1, _ControlGetRect($hPic2))
            _SetImageHover($Pic2, @ScriptDir & '\performance.png')
EndFunc   ;==>LeaveHoverPic

Func _ControlGetRect($hWnd)

    Local $Pos = ControlGetPos($hWnd, '', '')
    Local $tRect = DllStructCreate('int;int;int;int')

    DllStructSetData($tRect, 1, $Pos[0])
    DllStructSetData($tRect, 2, $Pos[1])
    DllStructSetData($tRect, 3, $Pos[0] + $Pos[2])
    DllStructSetData($tRect, 4, $Pos[1] + $Pos[3])
    Return $tRect
EndFunc   ;==>_ControlGetRect

Func _SetImageHover($ControlID, $sImage)

    Local $hWnd = GUICtrlGetHandle($ControlID)
    Local $hImage, $hBitmap

;    _GDIPlus_Startup()
    $hImage = _GDIPlus_BitmapCreateFromFile($sImage)
    $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
;    _GDIPlus_Shutdown()
    _WinAPI_DeleteObject(_SendMessage($hWnd, 0x0173))
    _SendMessage($hWnd, 0x0172, 0, $hBitmap)
    $hImage = _SendMessage($hWnd, 0x0173)
    If $hImage <> $hBitmap Then
EndFunc   ;==>_SetImageHover

Share this post

Link to post
Share on other sites


Edited by playlet

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