Sign in to follow this  
Followers 0
rony2006

Exit a loop when pressing a button

4 posts in this topic

Hello,

I have the following script:

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



;=======================================
;Asteapta semnal pentru CAPTURA 1
;=======================================



;=======================================
;Definire coordonate captura
;=======================================

Global $xs =    GUICtrlRead ($startx)
Global $ys =    GUICtrlRead ($starty)
Global $xe =    GUICtrlRead ($endx)
Global $ye =    GUICtrlRead ($endy)

;=======================================
;Captura prima poza
;=======================================

    Local $hBmp
    $hBmp = _ScreenCapture_Capture(@scriptdir & "\1.jpg",$xs,$ys,$xe,$ye)

;=======================================
;Realizeaza prima citire + trimite datele in casuta + afiseaza imaginea in GUI
;=======================================
_GDIPlus_Startup()
    $hex_avColor_1 = Screen_Area_Average_Color($xs,$ys,$xe,$ye)
    Guictrlsetdata ($value1,$hex_avColor_1/10000  )






        Case $live1


        While 2
        sleep (100)
        If $nMsg = $stop then exitloop
        Global $xs =    GUICtrlRead ($startx)
        Global $ys =    GUICtrlRead ($starty)
        Global $xe =    GUICtrlRead ($endx)
        Global $ye =    GUICtrlRead ($endy)

        Local $hBmp
        $hBmp = _ScreenCapture_Capture(@scriptdir & "\1.jpg",$xs,$ys,$xe,$ye)
        GUICtrlSetImage ($p1,"1.jpg" )

        _GDIPlus_Startup()
    $hex_avColor_1 = Screen_Area_Average_Color($xs,$ys,$xe,$ye)
Guictrlsetdata ($val1,$hex_avColor_1/10000  )
    ConsoleWrite($hex_avColor_1 & @CRLF)
        Wend



        Case $stop
        
        Exitloop 2



    EndSwitch
WEnd

 

When I press $live1 a while loop will start and will run but when i press $stop i want the loop from $live1 to stop, but it doesnt work. If I click $stop nothing happens.

Anybody knows why?

Share this post


Link to post
Share on other sites



#include <MsgBoxConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ScreenCapture.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
#include <ScreenCapture.au3>
#include <GUIConstantsEx.au3>
#include <ScreenCapture.au3>
#include <Color.au3>


$AOI = GUICreate("AOI",550,254,-1,-1,-1,-1)
$p1 = GUICtrlCreatePic("",17,17,309,120,-1,-1)
GUICtrlCreateLabel("Limita:",346,47,50,15,-1,-1)
GUICtrlSetBkColor(-1,"-2")
GUICtrlCreateLabel("Start X:",346,72,50,15,-1,-1)
GUICtrlSetBkColor(-1,"-2")
GUICtrlCreateLabel("Start Y:",346,97,50,15,-1,-1)
GUICtrlSetBkColor(-1,"-2")
GUICtrlCreateLabel("End X:",346,122,50,15,-1,-1)
GUICtrlSetBkColor(-1,"-2")
GUICtrlCreateLabel("End Y:",346,147,50,15,-1,-1)
GUICtrlSetBkColor(-1,"-2")
$rezultat = GUICtrlCreateLabel("in asteptare...",21,17,374,30,-1,-1)
GUICtrlSetFont(-1,14,700,0,"Verdana")
GUICtrlSetColor(-1,"0xFF8000")
GUICtrlSetBkColor(-1,"-2")
$limita = GUICtrlCreateInput("",408,47,109,20,-1,$WS_EX_CLIENTEDGE)
GUICtrlSetBkColor(-1,"0xFF8000")
GUICtrlCreateLabel("Program:",346,24,50,15,-1,-1)
GUICtrlSetBkColor(-1,"-2")
$startx = GUICtrlCreateInput("675",408,72,109,20,-1,$WS_EX_CLIENTEDGE)
GUICtrlSetBkColor(-1,"0x80FFFF")
GUICtrlCreateInput("",408,22,109,20,-1,$WS_EX_CLIENTEDGE)
GUICtrlSetBkColor(-1,"0xDADADA")
$starty = GUICtrlCreateInput("600",408,97,109,20,-1,$WS_EX_CLIENTEDGE)
GUICtrlSetBkColor(-1,"0x80FFFF")
$val1 = GUICtrlCreateLabel("",21,102,180,15,-1,-1)
GUICtrlSetFont(-1,10,400,0,"Verdana")
GUICtrlSetColor(-1,"0x00FF00")
GUICtrlSetBkColor(-1,"-2")
$endx = GUICtrlCreateInput("709",408,122,109,20,-1,$WS_EX_CLIENTEDGE)
GUICtrlSetBkColor(-1,"0x80FFFF")
$val2 = GUICtrlCreateLabel("",21,117,180,15,-1,-1)
GUICtrlSetFont(-1,10,400,0,"Verdana")
GUICtrlSetColor(-1,"0x00FF00")
GUICtrlSetBkColor(-1,"-2")
$endy = GUICtrlCreateInput("608",408,147,109,20,-1,$WS_EX_CLIENTEDGE)
GUICtrlSetBkColor(-1,"0x80FFFF")
$rezultationio = GUICtrlCreateLabel("",213,11,113,51,-1,-1)
GUICtrlSetFont(-1,36,700,0,"Verdana")
GUICtrlSetColor(-1,"0xFF0000")
GUICtrlSetBkColor(-1,"-2")
$log = GUICtrlCreateInput("",17,153,255,80,BitOr($ES_AUTOVSCROLL,$ES_MULTILINE,$WS_VSCROLL),$WS_EX_CLIENTEDGE)
$live1 = GUICtrlCreateButton("Live",417,207,45,30,-1,-1)
$start = GUICtrlCreateButton("Start",352,207,54,30,-1,-1)
GUICtrlCreateLabel("Delay:",346,172,50,15,-1,-1)
GUICtrlSetBkColor(-1,"-2")
$stop = GUICtrlCreateButton("Stop",472,207,45,30,-1,-1)
GUISetState(@SW_SHOW,$AOI)






Func Screen_Area_Average_Color($x_pos,$y_pos,$x_size,$y_size,$bRetunAsHex = 0)
    Local $Output = -1, _
    $hHBmp = _ScreenCapture_Capture("", $x_pos, $y_pos, $x_size, $y_size) ;create a GDI bitmap by capturing full screen of the desktop
    If @error Then Return SetError(1)
    Local $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hHBmp) ;convert GDI bitmap to GDI+ bitmap
    If Not @error Then
        Local $hBitmap_Scaled = _GDIPlus_ImageResize($hBitmap, 1, 1) ;resize image to 1x1
        If Not @error Then
            $Output = _GDIPlus_BitmapGetPixel($hBitmap_Scaled,0,0) ; Get  color of the 1x1 pixel
            If Not @error Then
                If $bRetunAsHex Then $Output = Hex($Output,6)
            EndIf
            _GDIPlus_BitmapDispose($hBitmap_Scaled) ; release $hBitmap_Scaled
        EndIf

         _GDIPlus_BitmapDispose($hBitmap) ; release $hBitmap
    EndIf
    _WinAPI_DeleteObject($hHBmp) ;release GDI bitmap resource because not needed anymore
    If $Output = -1 Then Return SetError(1)
    Return $Output
EndFunc



HotKeySet("{ESC}","Quit")
Func Quit()
    Exit
EndFunc














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



;=======================================
;Asteapta semnal pentru CAPTURA 1
;=======================================



;=======================================
;Definire coordonate captura
;=======================================

Global $xs =    GUICtrlRead ($startx)
Global $ys =    GUICtrlRead ($starty)
Global $xe =    GUICtrlRead ($endx)
Global $ye =    GUICtrlRead ($endy)

;=======================================
;Captura prima poza
;=======================================

    Local $hBmp
    $hBmp = _ScreenCapture_Capture(@scriptdir & "\1.jpg",$xs,$ys,$xe,$ye)

;=======================================
;Realizeaza prima citire + trimite datele in casuta + afiseaza imaginea in GUI
;=======================================
_GDIPlus_Startup()
    $hex_avColor_1 = Screen_Area_Average_Color($xs,$ys,$xe,$ye)
    Guictrlsetdata ($val1,$hex_avColor_1/10000  )






        Case $live1


        While 2
        sleep (100)
        If $nMsg = $stop then exitloop
        Global $xs =    GUICtrlRead ($startx)
        Global $ys =    GUICtrlRead ($starty)
        Global $xe =    GUICtrlRead ($endx)
        Global $ye =    GUICtrlRead ($endy)

        Local $hBmp
        $hBmp = _ScreenCapture_Capture(@scriptdir & "\1.jpg",$xs,$ys,$xe,$ye)
        GUICtrlSetImage ($p1,"1.jpg" )

        _GDIPlus_Startup()
    $hex_avColor_1 = Screen_Area_Average_Color($xs,$ys,$xe,$ye)
Guictrlsetdata ($val1,$hex_avColor_1/10000  )
Guictrlsetdata ($log,$hex_avColor_1/10000 & @CRLF , "gf" )
    ;ConsoleWrite($hex_avColor_1 & @CRLF)
        Wend



        Case $stop
        
        Exitloop 2



    EndSwitch
WEnd

here is the full code.

I dont understand to much from your link

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

You can do it this way:

#include <MsgBoxConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ScreenCapture.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
#include <ScreenCapture.au3>
#include <GUIConstantsEx.au3>
#include <ScreenCapture.au3>
#include <Color.au3>
#include <Array.au3>

$AOI = GUICreate("AOI", 550, 254, -1, -1, -1, -1)
$p1 = GUICtrlCreatePic("", 17, 17, 309, 120, -1, -1)
GUICtrlCreateLabel("Limita:", 346, 47, 50, 15, -1, -1)
GUICtrlSetBkColor(-1, "-2")
GUICtrlCreateLabel("Start X:", 346, 72, 50, 15, -1, -1)
GUICtrlSetBkColor(-1, "-2")
GUICtrlCreateLabel("Start Y:", 346, 97, 50, 15, -1, -1)
GUICtrlSetBkColor(-1, "-2")
GUICtrlCreateLabel("End X:", 346, 122, 50, 15, -1, -1)
GUICtrlSetBkColor(-1, "-2")
GUICtrlCreateLabel("End Y:", 346, 147, 50, 15, -1, -1)
GUICtrlSetBkColor(-1, "-2")
$rezultat = GUICtrlCreateLabel("in asteptare...", 21, 17, 374, 30, -1, -1)
GUICtrlSetFont(-1, 14, 700, 0, "Verdana")
GUICtrlSetColor(-1, "0xFF8000")
GUICtrlSetBkColor(-1, "-2")
$limita = GUICtrlCreateInput("", 408, 47, 109, 20, -1, $WS_EX_CLIENTEDGE)
GUICtrlSetBkColor(-1, "0xFF8000")
GUICtrlCreateLabel("Program:", 346, 24, 50, 15, -1, -1)
GUICtrlSetBkColor(-1, "-2")
$startx = GUICtrlCreateInput("675", 408, 72, 109, 20, -1, $WS_EX_CLIENTEDGE)
GUICtrlSetBkColor(-1, "0x80FFFF")
GUICtrlCreateInput("", 408, 22, 109, 20, -1, $WS_EX_CLIENTEDGE)
GUICtrlSetBkColor(-1, "0xDADADA")
$starty = GUICtrlCreateInput("600", 408, 97, 109, 20, -1, $WS_EX_CLIENTEDGE)
GUICtrlSetBkColor(-1, "0x80FFFF")
$val1 = GUICtrlCreateLabel("", 21, 102, 180, 15, -1, -1)
GUICtrlSetFont(-1, 10, 400, 0, "Verdana")
GUICtrlSetColor(-1, "0x00FF00")
GUICtrlSetBkColor(-1, "-2")
$endx = GUICtrlCreateInput("709", 408, 122, 109, 20, -1, $WS_EX_CLIENTEDGE)
GUICtrlSetBkColor(-1, "0x80FFFF")
$val2 = GUICtrlCreateLabel("", 21, 117, 180, 15, -1, -1)
GUICtrlSetFont(-1, 10, 400, 0, "Verdana")
GUICtrlSetColor(-1, "0x00FF00")
GUICtrlSetBkColor(-1, "-2")
$endy = GUICtrlCreateInput("608", 408, 147, 109, 20, -1, $WS_EX_CLIENTEDGE)
GUICtrlSetBkColor(-1, "0x80FFFF")
$rezultationio = GUICtrlCreateLabel("", 213, 11, 113, 51, -1, -1)
GUICtrlSetFont(-1, 36, 700, 0, "Verdana")
GUICtrlSetColor(-1, "0xFF0000")
GUICtrlSetBkColor(-1, "-2")
$log = GUICtrlCreateInput("", 17, 153, 255, 80, BitOR($ES_AUTOVSCROLL, $ES_MULTILINE, $WS_VSCROLL), $WS_EX_CLIENTEDGE)
$live1 = GUICtrlCreateButton("Live", 417, 207, 45, 30, -1, -1)
$start = GUICtrlCreateButton("Start", 352, 207, 54, 30, -1, -1)
GUICtrlCreateLabel("Delay:", 346, 172, 50, 15, -1, -1)
GUICtrlSetBkColor(-1, "-2")
$stop = GUICtrlCreateButton("Stop", 472, 207, 45, 30, -1, -1)
GUISetState(@SW_SHOW, $AOI)
;HotKeySet("{ESC}", "Quit") ;better place declaring a hotkey but not needed
Opt("GUICloseOnESC", 1) ;1=ESC  closes, 0=ESC won't close


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



            ;=======================================
            ;Asteapta semnal pentru CAPTURA 1
            ;=======================================



            ;=======================================
            ;Definire coordonate captura
            ;=======================================

            Global $xs = GUICtrlRead($startx)
            Global $ys = GUICtrlRead($starty)
            Global $xe = GUICtrlRead($endx)
            Global $ye = GUICtrlRead($endy)

            ;=======================================
            ;Captura prima poza
            ;=======================================

            Local $hBmp
            $hBmp = _ScreenCapture_Capture(@ScriptDir & "\1.jpg", $xs, $ys, $xe, $ye)

            ;=======================================
            ;Realizeaza prima citire + trimite datele in casuta + afiseaza imaginea in GUI
            ;=======================================
            _GDIPlus_Startup()
            $hex_avColor_1 = Screen_Area_Average_Color($xs, $ys, $xe, $ye)
            GUICtrlSetData($val1, $hex_avColor_1 / 10000)

        Case $live1
            While 2
                $nMsg = _WaitForGuiGetMsg(100, String($GUI_EVENT_CLOSE) & '|' & String($stop))
                ;If $nMsg Then ConsoleWrite('live: ' & $nMsg & @CRLF)
                If $nMsg = $stop Then ExitLoop
                If $nMsg = $GUI_EVENT_CLOSE Then Exit
                Global $xs = GUICtrlRead($startx)
                Global $ys = GUICtrlRead($starty)
                Global $xe = GUICtrlRead($endx)
                Global $ye = GUICtrlRead($endy)

                Local $hBmp
                $hBmp = _ScreenCapture_Capture(@ScriptDir & "\1.jpg", $xs, $ys, $xe, $ye)
                GUICtrlSetImage($p1, "1.jpg")

                _GDIPlus_Startup()
                $hex_avColor_1 = Screen_Area_Average_Color($xs, $ys, $xe, $ye)
                GUICtrlSetData($val1, $hex_avColor_1 / 10000)
                GUICtrlSetData($log, $hex_avColor_1 / 10000 & @CRLF, "gf")
                ;ConsoleWrite($hex_avColor_1 & @CRLF)
            WEnd

        Case $stop
            ;ExitLoop 2  this would exit the main loop, so don't do this
    EndSwitch
WEnd

Func Screen_Area_Average_Color($x_pos, $y_pos, $x_size, $y_size, $bRetunAsHex = 0)
    Local $Output = -1, _
            $hHBmp = _ScreenCapture_Capture("", $x_pos, $y_pos, $x_size, $y_size) ;create a GDI bitmap by capturing full screen of the desktop
    If @error Then Return SetError(1)
    Local $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hHBmp) ;convert GDI bitmap to GDI+ bitmap
    If Not @error Then
        Local $hBitmap_Scaled = _GDIPlus_ImageResize($hBitmap, 1, 1) ;resize image to 1x1
        If Not @error Then
            $Output = _GDIPlus_BitmapGetPixel($hBitmap_Scaled, 0, 0) ; Get  color of the 1x1 pixel
            If Not @error Then
                If $bRetunAsHex Then $Output = Hex($Output, 6)
            EndIf
            _GDIPlus_BitmapDispose($hBitmap_Scaled) ; release $hBitmap_Scaled
        EndIf

        _GDIPlus_BitmapDispose($hBitmap) ; release $hBitmap
    EndIf
    _WinAPI_DeleteObject($hHBmp) ;release GDI bitmap resource because not needed anymore
    If $Output = -1 Then Return SetError(1)
    Return $Output
EndFunc   ;==>Screen_Area_Average_Color

Func Quit()
    Exit
EndFunc   ;==>Quit

Func _WaitForGuiGetMsg($iMSec=0, $sIDs = '-3')
    ;syntax _WaitForGuiGetMsg([$iMSec,] [$sIDs]])
    ;Parameters $iMSec:     optional maximum Time waiting for a Message
    ;                       if 0 waiting until $GUI_EVENT_CLOSE
    ;           $sIDs:      a string with all watched Message-IDs seperated by "|"
    ;                       if string is empty only -3 $GUI_EVENT_CLOSE is watched
    ;return     $iMsg       one of the watched messageIDs or 0
    ;
    ;Author:    autoBert
    ;first published in https://www.autoitscript.com/forum/topic/182618-exit-a-loop-when-pressing-a-button/#comment-1311549
    Local $aIDs = StringSplit($sIDs, '|', 2)
    Local $iStart = TimerInit()
    Local $iMsg
    Local $iRet = 0
    Local $iDiff = 0
    Do
        $iMsg = GUIGetMsg()
        For $i = 0 To UBound($aIDs) - 1
            If $aIDs[$i] = $iMsg Then
                $iRet = $iMsg
                ExitLoop 2
            EndIf
        Next
        If $iMSec >0 Then $iDiff=TimerDiff($iStart)
    Until $iDiff >= $iMSec
    Return $iRet
EndFunc   ;==>_WaitForGuiGetMsg

the func _WaitForGuiGetMsg:

;syntax _WaitForGuiGetMsg([$iMSec,] [$sIDs]])
    ;Parameters $iMSec:     optional maximum Time waiting for a Message
    ;                       if 0 waiting until $GUI_EVENT_CLOSE
    ;           $sIDs:      optional a string with all watched Message-IDs seperated by "|"
    ;                       if string is empty only -3 $GUI_EVENT_CLOSE is watched
    ;return     $iMsg       one of the watched messageIDs or 0

is used instead of a normal sleep. The returned messageID must be handled in scriptcode (already done by me)

Edited by AutoBert

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