Sign in to follow this  
Followers 0
trol4ever

How to bind a funcion to a button ?

14 posts in this topic

Can someone show me a code , how to bind a button to a funcion :

Example :

$Button2 = GUICtrlCreateButton("Hide Window", 128, 16, 89, 33, 0)

with

Func Hide()

GUISetState(@SW_HIDE)

EndFunc

Share this post


Link to post
Share on other sites



GUICtrlSetOnEvent


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

... tell me what i'm wrong becouse :

GUICtrlSetOnEvent($Button2, "Hide")

its not working :|

Share this post


Link to post
Share on other sites

Ofcourse it works...u are doing it wrong...search the help file for "guisetonevent"...there is a great example there...

Share this post


Link to post
Share on other sites

You must specify that you are using on event mode.

Try adding this to the top of your script:

Opt ("GUIOnEventMode", 1)

Share this post


Link to post
Share on other sites

You must specify that you are using on event mode.

Try adding this to the top of your script:

Opt ("GUIOnEventMode", 1)
It can also be done in the idle loop

While 1
   $msg = GUIGetMsg()
   Switch $Msg
      Case $Button2
         Hide()
   EndSwitch
WEnd

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

So :

#include <GUIConstants.au3>
Opt ("GUIOnEventMode", 1)

Global $Paused = 0, $IsHotkey = 0
HotKeySet("{F1}", "PauseByHotKey")
HotKeySet("{F2}", "Hide")
HotKeySet("{F3}", "Show")
HotKeySet("{F4}", "Escape")

$Button1 = GUICtrlCreateButton("Pause", 16, 16, 97, 73, 0)
$Button2 = GUICtrlCreateButton("Hide Window", 128, 16, 89, 33, 0)
$Button3 = GUICtrlCreateButton("Show Window", 128, 56, 89, 33, 0)
$Input2 = GUICtrlCreateInput("F1 = Start/Pause", 16, 120, 97, 21)
$Input3 = GUICtrlCreateInput("F2 = Hide Window", 120, 120, 97, 21)
$Input4 = GUICtrlCreateInput("F3 = Show Window", 64, 144, 113, 21)

GUISetOnEvent($GUI_EVENT_CLOSE, "Escape")
GUICtrlSetOnEvent($Button1, "Pause")
GUICtrlSetOnEvent($Button2, "Hide")
GUICtrlSetOnEvent($Button3, "Show")

GUISetState(@SW_SHOW)

While 1
   Sleep(100)
WEnd

Func PauseByHotKey()
    If $Paused Then $IsHotkey = 1
    Pause()
EndFunc

Func Pause()
    Opt("GuiOnEventMode", 0)
    GUICtrlSetData($Button1, "Start")
    $Paused = 1
    While $Paused
        Sleep(10)
        $Msg = GUIGetMsg()
        ToolTip('ScripT Is On PausE ModE',0,0)
        If $Msg = $Button1 Or $IsHotkey = 1 Then ExitLoop
    WEnd
    $IsHotkey = 0
    $Paused = 0
    ToolTip("")
    Opt("GuiOnEventMode", 1)
    GUICtrlSetData($Button1, "Pause")
EndFunc

Func Hide()
    GUISetState(@SW_HIDE)
EndFunc

Func Show()
    GUISetState(@SW_SHOW)
EndFunc

Func Escape()
    Exit
EndFunc

and :| no result , The hotkeys work but the buttons don't

Edited by trol4ever

Share this post


Link to post
Share on other sites

The code for that GUI is a bit hard on the eyes.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Ok , figured it out , the buttons do not work when the script is paused .

However ... when i start the scrip how can i make it to start only when i push the "Start" button ?

without adding Pause() in front ...

Share this post


Link to post
Share on other sites

humm, maybe this-

#include <GUIConstants.au3>
Opt ("GUIOnEventMode", 1)

Global $Paused = 0
HotKeySet("{F1}", "Pause")
HotKeySet("{F2}", "Hide")
HotKeySet("{F3}", "Show")
HotKeySet("{F4}", "Escape")
GUICreate("SomeGUI")

$Button1 = GUICtrlCreateButton("Pause", 16, 16, 97, 73, 0)
$Button2 = GUICtrlCreateButton("Hide Window", 128, 16, 89, 33, 0)
$Button3 = GUICtrlCreateButton("Show Window", 128, 56, 89, 33, 0)
$Input2 = GUICtrlCreateInput("F1 = Start/Pause", 16, 120, 97, 21)
$Input3 = GUICtrlCreateInput("F2 = Hide Window", 120, 120, 97, 21)
$Input4 = GUICtrlCreateInput("F3 = Show Window", 64, 144, 113, 21)

GUISetOnEvent($GUI_EVENT_CLOSE, "Escape")
GUICtrlSetOnEvent($Button1, "Pause")
GUICtrlSetOnEvent($Button2, "Hide")
GUICtrlSetOnEvent($Button3, "Show")

GUISetState(@SW_SHOW)

While 1
    Sleep(100)
    if $Paused=0 then ContinueLoop
    ToolTip('ScripT Is On PausE ModE',0,0)
WEnd

Func Pause()
    if $Paused=0 then
        GUICtrlSetData($Button1, "Start")
        $Paused = 1
    Else
        $Paused = 0
        ToolTip("")
        GUICtrlSetData($Button1, "Pause")
    EndIf
EndFunc

Func Hide()
    GUISetState(@SW_HIDE)
EndFunc

Func Show()
    GUISetState(@SW_SHOW)
EndFunc

Func Escape()
    Exit
EndFunc

Share this post


Link to post
Share on other sites

still doesn't work ... it starts with the macro , u need to pause it ... i want to start with the macro paused and work only when i push Start .

Share this post


Link to post
Share on other sites

Maybe...

#include <GUIConstants.au3>
Opt ("GUIOnEventMode", 1)

Global $Paused = 1
HotKeySet("{F1}", "Pause")
HotKeySet("{F2}", "Hide")
HotKeySet("{F3}", "Show")
HotKeySet("{F4}", "Escape")
GUICreate("SomeGUI")

$Button1 = GUICtrlCreateButton("Pause", 16, 16, 97, 73, 0)
$Button2 = GUICtrlCreateButton("Hide Window", 128, 16, 89, 33, 0)
$Button3 = GUICtrlCreateButton("Show Window", 128, 56, 89, 33, 0)
$Input2 = GUICtrlCreateInput("F1 = Start/Pause", 16, 120, 97, 21)
$Input3 = GUICtrlCreateInput("F2 = Hide Window", 120, 120, 97, 21)
$Input4 = GUICtrlCreateInput("F3 = Show Window", 64, 144, 113, 21)

GUISetOnEvent($GUI_EVENT_CLOSE, "Escape")
GUICtrlSetOnEvent($Button1, "Pause")
GUICtrlSetOnEvent($Button2, "Hide")
GUICtrlSetOnEvent($Button3, "Show")

GUISetState(@SW_SHOW)

While 1
    Sleep(100)
    if $Paused=0 then ContinueLoop
    ToolTip('ScripT Is On PausE ModE',0,0)
WEnd

Func Pause()
    if $Paused=0 then
        GUICtrlSetData($Button1, "Start")
        $Paused = 1
    Else
        $Paused = 0
        ToolTip("")
        GUICtrlSetData($Button1, "Pause")
    EndIf
EndFunc

Func Hide()
    GUISetState(@SW_HIDE)
EndFunc

Func Show()
    GUISetState(@SW_SHOW)
EndFunc

Func Escape()
    Exit
EndFunc

Share this post


Link to post
Share on other sites

thx bert ... it works , but ? what have u done ? what changes u made ?

Share this post


Link to post
Share on other sites

thx bert ... it works , but ? what have u done ? what changes u made ?

Global Paused = 1

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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