Sign in to follow this  
Followers 0
enaiman

@GUI_CtrlID and how to interrupt a script

4 posts in this topic

After more than 2 hours try to make a script work (http://www.autoitscript.com/forum/index.php?showtopic=76057) I gave up and decided to ask for help.

It's quite confusing @GUI_CtrlID and it's behaviour.

I've prepared a little demo script:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 216, 135, 193, 125)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")
$Label1 = GUICtrlCreateLabel("Label1", 40, 16, 140, 17)
$Button1 = GUICtrlCreateButton("Button1", 24, 80, 169, 25, 0)
GUICtrlSetOnEvent(-1, "Button1Click")
GUISetState(@SW_SHOW)
$pause = 1      ;a simple flag
#EndRegion ### END Koda GUI section ###

While 1
    Sleep(100)
WEnd

Func Button1Click()
    Switch @GUI_CtrlId
        Case $Button1
            $pause = Not $pause     ;attempt to toggle the flag
            If $pause = 0 Then      
                asdf()              ;execute a function
            Else
                MsgBox(0, "", "paused") ;if pause flag is set then display a message
            EndIf
    EndSwitch
EndFunc
Func Form1Close()
    Exit
EndFunc
Func asdf()
    For $i=1 To 1000
        Sleep(50)
        GUICtrlSetData($Label1, $i)
        If $pause = 1 Then
            ExitLoop
        EndIf
    Next
    MsgBox(0, "out of loop", "paused")
EndFunc

I know I'm doing something wrong but I can't figure what. All I try to accomplish is to break "asdf" execution when the button is pressed.

Thank you,


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

See monoceres answer in this this:

http://www.autoitscript.com/forum/index.ph...l=button+hotkey

that should work..

Regards muttley

EDIT:

I've posted an example so you can see how it works:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Opt("GUIOnEventMode", 1)
Global $flag=False

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 216, 135, 193, 125)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")
$Label1 = GUICtrlCreateLabel("Label1", 40, 16, 140, 17)
$Button1 = GUICtrlCreateButton("Button1", 24, 80, 169, 25, 0)
GUICtrlSetOnEvent(-1, "Button1Click")
GUISetState(@SW_SHOW)
$pause = 1   ;a simple flag
#EndRegion ### END Koda GUI section ###

Do
    
    Sleep(25)
    While $flag
        For $i=1 To 1000
        Sleep(50)
        GUICtrlSetData($Label1, $i)
        If $pause = 1 Then
            ExitLoop
        EndIf
    Next
    WEnd
    
Until False

Func Button1Click()
    $flag = Not $flag
    Switch @GUI_CtrlId
        Case $Button1
            $pause = Not $pause ;attempt to toggle the flag
            If $pause = 0 Then  
                       ;execute a function
            Else
                MsgBox(0, "", "paused");if pause flag is set then display a message
            EndIf
    EndSwitch
EndFunc
Func Form1Close()
    Exit
EndFunc
Edited by newbiescripter

Share this post


Link to post
Share on other sites

enaiman

Hi! Try this:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Opt("GUIOnEventMode", 1)

Global $pause = False    ;a simple flag

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 216, 135, 193, 125)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")

$Label1 = GUICtrlCreateLabel("Label1", 40, 16, 140, 17, $BS_NOTIFY)

$Button1 = GUICtrlCreateButton("Button1", 24, 80, 169, 25, 0)
GUICtrlSetOnEvent(-1, "asdf")

GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")

GUISetState(@SW_SHOW)

#EndRegion ### END Koda GUI section ###

While 1
    Sleep(100)
WEnd

Func WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    Local $iIDFrom = BitAND($wParam, 0xFFFF) ;LoWord
    Local $iCode = BitShift($wParam, 16) ;HiWord
    
    Switch $iIDFrom
        Case $Button1
            Switch $iCode
                Case $BN_CLICKED
                    $pause = Not $pause
            EndSwitch
    EndSwitch
    
    Return $GUI_RUNDEFMSG
EndFunc

Func Form1Close()
    Exit
EndFunc

Func asdf()
    For $i=1 To 1000
        If $pause = False Then ExitLoop
        Sleep(50)
        GUICtrlSetData($Label1, $i)
    Next
EndFunc
muttley

Share this post


Link to post
Share on other sites

Thank you very much newbiescripter and rasim :)

2 great examples ... I'll take my time to understand them :(

muttley


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

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