Sign in to follow this  
Followers 0
eJan

GUICtrlCreateMenu to work as button

3 posts in this topic

Can it be done to have buttons like menus, when the mouse is hovering over the specified control to show another style (like search button in Windows Explorer)?

I have tried:

#include <GUIConstants.au3>

GUICreate("My GUI menu", 300, 200)

Global $defaultstatus = "Ready"
Global $status

$menu = GUICtrlCreateMenu("To work as button")
GUICtrlSetFont($menu, 9, 400, 2, "Arial Bold"); ?
GUICtrlSetPos($menu, 60, 60); ?

$infoitem = GUICtrlCreateMenuItem("Event", $menu)

$button = GUICtrlCreateButton("Button", 1, 1, 70, 20)
GUICtrlSetFont($button, 9, 400, 2, "Arial Bold")
GUICtrlSetPos($button, 0, 15)

$okbutton = GUICtrlCreateButton("OK", 50, 130, 70, 20)
$cancelbutton = GUICtrlCreateButton("Cancel", 180, 130, 70, 20)

GUISetState()
While 1
    $msg = GUIGetMsg()
    
    If $msg = $GUI_EVENT_CLOSE Or $msg = $cancelbutton Then Exit
;If $msg = $menu Then "Event"
    If $msg = $button Then MsgBox(0, "Event", "Button pressed", 1)
    If $msg = $infoitem Then MsgBox(0, "Info", "Only a test...", 1)
WEnd
GUIDelete()

Exit

Share this post


Link to post
Share on other sites



Try this simple "framework", with this you can hovering any control with different event functions. GUI not necessarily should be in event mode for work this, only one global variable $vCallData should be set.

#include <GUIConstants.au3>

$hGUI = GUICreate("My GUI menu", 300, 200)

Global $defaultstatus = "Ready"
Global $status, $vCallData

$menu = GUICtrlCreateMenu("To work as button")
GUICtrlSetFont($menu, 9, 400, 2, "Arial Bold"); ?
GUICtrlSetPos($menu, 60, 60)
$infoitem = GUICtrlCreateMenuItem("Event", $menu)

$btn1 = _GUICreateBiStateControl(GUICtrlCreateButton("Button",  1, 1, 70, 20, $BS_FLAT), $hGUI, "OnHover", "OnLeave")
$btn2 = _GUICreateBiStateControl(GUICtrlCreateButton("Button", 80, 1, 70, 20, $BS_FLAT), $hGUI, "OnHover", "OnLeave")
$edit = _GUICreateBiStateControl(GUICtrlCreateEdit("", 80, 30, 100, 100, 0, 0), $hGUI, "OnEditHover", "OnEditLeave")

$okbutton = GUICtrlCreateButton("OK", 50, 130, 70, 20)
$cancelbutton = GUICtrlCreateButton("Cancel", 180, 130, 70, 20)

GUISetState()

While 1
    _CheckBiStateControl($btn1)
    _CheckBiStateControl($btn2)
    _CheckBiStateControl($edit)
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Or $msg = $cancelbutton Then Exit

    If $msg = $btn1[0] Then MsgBox(0, "Event", "First button pressed", 1)
    If $msg = $btn2[0] Then MsgBox(0, "Event", "Second button pressed", 1)

    If $msg = $okbutton Then MsgBox(0, "Event", "Button pressed", 1)
    If $msg = $infoitem Then MsgBox(0, "Info", "Only a test...", 1)
WEnd
GUIDelete()

Exit

Func _GUICreateBiStateControl($hControl, $hGUI, $hover_func, $leave_func)
    Local $cPos = ControlGetPos($hGUI, "", $hControl)
    Local $hBSControl[8]
    $hBSControl[0] = $hControl
    $hBSControl[1] = $cPos[0]
    $hBSControl[2] = $cPos[1]
    $hBSControl[3] = $cPos[0] + $cPos[2]
    $hBSControl[4] = $cPos[1] + $cPos[3]
    $hBSControl[5] = $hover_func
    $hBSControl[6] = $leave_func
    $hBSControl[7] = 0
    Return $hBSControl
EndFunc

Func _CheckBiStateControl(ByRef $hControl)
    Local $info = GUIGetCursorInfo()
    If $info[0] > $hControl[1] and $info[0] < $hControl[3] and _
       $info[1] > $hControl[2] and $info[1] < $hControl[4] Then
        If $hControl[7] Then Return 0
        $vCallData = $hControl[0]
        Call($hControl[5])
        $hControl[7] = 1
    ElseIf $hControl[7] Then 
        $vCallData = $hControl[0]
        Call($hControl[6])
        $hControl[7] = 0
    Endif
EndFunc

Func OnHover()
    GUICtrlSetStyle($vCallData, 0)
EndFunc

Func OnLeave()
    GUICtrlSetStyle($vCallData, $BS_FLAT)
EndFunc

Func OnEditHover()
    GUICtrlSetStyle($vCallData, 0, $WS_EX_STATICEDGE)
EndFunc

Func OnEditLeave()
    GUICtrlSetStyle($vCallData, 0, 0)
EndFunc

Share this post


Link to post
Share on other sites

Thanks Lazycat this works fine, I'll try to add labels instead buttons.

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