我是autoIt的新手。我确信这是一个常见问题,但我在论坛上搜索并没有任何运气。
我制作了一个需要用户输入的GUI。当按下开始按钮时,它会启动带计数器的while循环,并在计数器达到用户输入的任何数字后停止。
问题有时我想在计数器达到用户输入的数字之前结束脚本。但是,当我按下停止按钮时,脚本没有响应。
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 0)
#Region ### START Koda GUI section ### Form=c:\users\atkinspr\documents\main1.kxf
Global $main1 = GUICreate("main1", 543, 313, 281, 283)
Global $Input1 = GUICtrlCreateInput("", 40, 128, 121, 24)
Global $start = GUICtrlCreateButton("Start", 40, 168, 123, 25)
Global $Label1 = GUICtrlCreateLabel("Counter: ", 40, 232, 70, 25)
Global $stop = GUICtrlCreateButton("Stop", 200, 168, 123, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $start
$DataFromUser = GUICtrlRead($input1)
$i = 0
while $i < $DataFromUser
$i = $i + 1
Sleep(1000)
GUICtrlSetData($Label1, "Counter: " &$i)
ConsoleWrite("output: " &$i &@LF )
WEnd
Case $stop
Exit
EndSwitch
WEnd
答案 0 :(得分:2)
如果您希望GUIGetMsg()
正常工作,则需要避免以其方式添加Sleep。
这是您问题的快速解决方案
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 0)
#Region ### START Koda GUI section ### Form=c:\users\atkinspr\documents\main1.kxf
Global $main1 = GUICreate("main1", 543, 313, 281, 283)
Global $Input1 = GUICtrlCreateInput("", 40, 128, 121, 24)
Global $start = GUICtrlCreateButton("Start", 40, 168, 123, 25)
Global $Label1 = GUICtrlCreateLabel("Counter: ", 40, 232, 70, 25)
Global $stop = GUICtrlCreateButton("Stop", 200, 168, 123, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Global $Counter = 0
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $start
AdlibRegister("Countdown", 1000)
Case $stop
AdlibUnRegister("Countdown")
ConsoleWrite("timer canceled!" & @LF)
$Counter = 0
EndSwitch
WEnd
Func Countdown()
$DataFromUser = GUICtrlRead($Input1)
$Counter += 1
If $Counter > $DataFromUser Then
AdlibUnRegister("Countdown")
ConsoleWrite("timer ended!" & @LF)
Return
EndIf
GUICtrlSetData($Label1, "Counter: " & $Counter)
ConsoleWrite("output: " & $Counter & @LF)
EndFunc ;==>Countdown
答案 1 :(得分:1)
更好的方法:使用OnEventMode。
#include <GUIConstantsEx.au3>
Opt('GUIOnEventMode', 1)
Global $iCount = 0, $iTimeMax = 0
Global $main1 = GUICreate("main1", 543, 313, 281, 283)
GUISetOnEvent(-3, '_exit')
Global $Input1 = GUICtrlCreateInput("", 40, 128, 121, 24)
Global $start = GUICtrlCreateButton("Start", 40, 168, 123, 25)
GUICtrlSetOnEvent(-1, '_onClick')
Global $Label1 = GUICtrlCreateLabel("Counter: ", 40, 232, 70, 25)
Global $stop = GUICtrlCreateButton("Stop", 200, 168, 123, 25)
GUICtrlSetState(-1, $GUI_DISABLE)
GUICtrlSetOnEvent(-1, '_onClick')
GUISetState()
While 1
Sleep(10)
WEnd
Func _onClick()
Switch @GUI_CtrlId
Case $start
GUICtrlSetState($start, $GUI_DISABLE)
GUICtrlSetState($stop, $GUI_ENABLE)
$iTimeMax = GUICtrlRead($Input1)
AdlibRegister('_setLabel', 1000)
Case $stop
_stop()
EndSwitch
EndFunc
Func _setLabel()
$iCount += 1
GUICtrlSetData($Label1, "Counter: " & $iCount)
If $iCount = $iTimeMax Then _stop()
EndFunc
Func _stop()
AdlibUnRegister('_setLabel')
$iCount = 0
GUICtrlSetState($start, $GUI_ENABLE)
GUICtrlSetState($stop, $GUI_DISABLE)
EndFunc
Func _exit()
Exit
EndFunc