PDA

Показать полную графическую версию : [решено] Остановить Func нажатием на кнопку...


JaguarSP
21-04-2010, 16:54
Надо остановить Func нажатием на кнопку или даже не только остановить func а вообще закрыть.

Func _Timer()
For $Start = $paz To 0 Step -1
Select
Case $Start >= 7
GUICtrlSetColor($Label1, 0x00FF00)
GUICtrlSetData($Label1, $Start)
Case $Start > 3
GUICtrlSetColor($Label1, 0xFF8000)
GUICtrlSetData($Label1, $Start)
Case $Start >= 0
GUICtrlSetColor($Label1, 0xFF0000)
GUICtrlSetData($Label1, $Start)
EndSelect
Sleep(1000)
Next
Return
EndFunc

http://forum.oszone.net/thread-150169.html это не подходит...

alt-andrew
21-04-2010, 18:10
А если задать горячие клавиши?
HotKeySet("{ESC}", "Terminate")

Func Terminate()
Exit 0
EndFunc

JaguarSP
21-04-2010, 19:02
так то оно да... а можно сделать чтобы хоткей был переменной что то типа:

HotKeySet("$cancel", "Terminate")
$cancel = GUICtrlCreateButton("Cancel", 295, 422, 100, 53, 0)


Func Terminate()
Exit 0
EndFunc

alt-andrew
21-04-2010, 20:03
Насколько я знаю GUICtrlCreateButton создает кнопку для GUI,соответственно у вас есть окно,а почему бы тогда не сделать так:
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$Form1 = GUICreate("Form1", 164, 162, 388, 278)
$Button1 = GUICtrlCreateButton("Button1", 40, 104, 75, 25)
GUISetState(@SW_SHOW)



While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button1
Exit
EndSwitch
WEnd

Medic84
21-04-2010, 23:13
Только вся проблема заключается в том, что цикл While..WEnd не обрабатывается во время выполнения определенной функции. Поэтому нужно устанавливать проверку на нажатие кнопки $bitton1 в самой функции...

JaguarSP
21-04-2010, 23:42
Medic84 ага именно так...
устанавливать проверку на нажатие кнопки $bitton1 в самой функции... »
вот именно это и надо... а как это сделать?

Medic84
21-04-2010, 23:46
Ну как. В коде пишешь чуть ли не после каждой строчки
if $nMsg = $button1 Then Exit

JaguarSP
22-04-2010, 00:34
пробовал во все участки функции вставлять... =((( не помогает, может быть что не так делаю? попробуй сам вставь вот func

Func _Timer()
For $Start = $paz To 0 Step -1
Select
Case $Start >= 7
GUICtrlSetColor($Label1, 0x00FF00)
GUICtrlSetData($Label1, $Start)
Case $Start > 3
GUICtrlSetColor($Label1, 0xFF8000)
GUICtrlSetData($Label1, $Start)
Case $Start >= 0
GUICtrlSetColor($Label1, 0xFF0000)
GUICtrlSetData($Label1, $Start)
EndSelect
Sleep(1000)
Next
Return
EndFunc

madmasles
22-04-2010, 02:28
JaguarSP,
А если примерно так:#include <GuiConstantsEx.au3>

Global (http://www.autoitscript.com/autoit3/docs/keywords.htm#Global) $minuts = 00, $secunds = 10

$hGUI = GUICreate (http://www.autoitscript.com/autoit3/docs/functions/GUICreate.htm)("Test", 200, 100)
$vremya = StringFormat (http://www.autoitscript.com/autoit3/docs/functions/StringFormat.htm)("%02i:%02i", $minuts, $secunds)
$button = GUICtrlCreateButton (http://www.autoitscript.com/autoit3/docs/functions/GUICtrlCreateButton.htm)($vremya, 65, 35, 75, 25)
GUISetState (http://www.autoitscript.com/autoit3/docs/functions/GUISetState.htm)()
AdlibRegister("_TimerCount", 1000)

While (http://www.autoitscript.com/autoit3/docs/keywords.htm#While) 1
$msg = GUIGetMsg (http://www.autoitscript.com/autoit3/docs/functions/GUIGetMsg.htm)()
Switch (http://www.autoitscript.com/autoit3/docs/keywords.htm#Switch) $msg
Case (http://www.autoitscript.com/autoit3/docs/keywords.htm#Case) $GUI_EVENT_CLOSE
ExitLoop (http://www.autoitscript.com/autoit3/docs/keywords.htm#ExitLoop)
Case (http://www.autoitscript.com/autoit3/docs/keywords.htm#Case) $button
AdlibUnRegister()
GUICtrlSetData (http://www.autoitscript.com/autoit3/docs/functions/GUICtrlSetData.htm)($button, "00:00")
_MyFunc()
EndSwitch (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndSwitch)
WEnd (http://www.autoitscript.com/autoit3/docs/keywords.htm#WEnd)

Func (http://www.autoitscript.com/autoit3/docs/keywords.htm#Func) _TimerCount()
$secunds -= 1
GUICtrlSetData (http://www.autoitscript.com/autoit3/docs/functions/GUICtrlSetData.htm)($button, StringFormat (http://www.autoitscript.com/autoit3/docs/functions/StringFormat.htm)("%02i:%02i", $minuts, $secunds))

If (http://www.autoitscript.com/autoit3/docs/keywords.htm#If) $secunds = 0 Then (http://www.autoitscript.com/autoit3/docs/keywords.htm#Then)
If (http://www.autoitscript.com/autoit3/docs/keywords.htm#If) $minuts >= 1 Then (http://www.autoitscript.com/autoit3/docs/keywords.htm#Then)
$minuts -= 1
$secunds = 60
Else (http://www.autoitscript.com/autoit3/docs/keywords.htm#Else)
If (http://www.autoitscript.com/autoit3/docs/keywords.htm#If) $secunds = 0 And (http://www.autoitscript.com/autoit3/docs/keywords.htm#And) $minuts = 0 Then (http://www.autoitscript.com/autoit3/docs/keywords.htm#Then)
AdlibUnRegister()
_MyFunc()
EndIf (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndIf)
EndIf (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndIf)
EndIf (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndIf)
EndFunc (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndFunc) ;==>_TimerCount

Func (http://www.autoitscript.com/autoit3/docs/keywords.htm#Func) _MyFunc()
MsgBox (http://www.autoitscript.com/autoit3/docs/functions/MsgBox.htm)(0, "_MyFunc", "Кнопка нажата")
EndFunc (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndFunc) ;==>_MyFunc

Creat0R
22-04-2010, 13:30
В таком случае обычно в цикле самой функции опрашивают событие нажатой кнопки, или ещё лучше, изменить режим обработки событии и проверять значение некой глобальной переменной:

#include <GuiConstantsEx.au3>

Global (http://www.autoitscript.com/autoit3/docs/keywords.htm#Global) $iStop = False (http://www.autoitscript.com/autoit3/docs/keywords.htm#False)

$hGUI = GUICreate (http://www.autoitscript.com/autoit3/docs/functions/GUICreate.htm)("Test", 200, 100)
$nStop_Button = GUICtrlCreateButton (http://www.autoitscript.com/autoit3/docs/functions/GUICtrlCreateButton.htm)("Stop _Timer()", 65, 35, 75, 25)

GUISetState (http://www.autoitscript.com/autoit3/docs/functions/GUISetState.htm)()

_Timer()

While (http://www.autoitscript.com/autoit3/docs/keywords.htm#While) 1
$msg = GUIGetMsg (http://www.autoitscript.com/autoit3/docs/functions/GUIGetMsg.htm)()
Switch (http://www.autoitscript.com/autoit3/docs/keywords.htm#Switch) $msg
Case (http://www.autoitscript.com/autoit3/docs/keywords.htm#Case) $GUI_EVENT_CLOSE
ExitLoop (http://www.autoitscript.com/autoit3/docs/keywords.htm#ExitLoop)
EndSwitch (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndSwitch)
WEnd (http://www.autoitscript.com/autoit3/docs/keywords.htm#WEnd)

Func (http://www.autoitscript.com/autoit3/docs/keywords.htm#Func) _Timer()
$iOld_Opt = Opt (http://www.autoitscript.com/autoit3/docs/functions/Opt.htm)("GUIOnEventMode", 1)

GUICtrlSetOnEvent (http://www.autoitscript.com/autoit3/docs/functions/GUICtrlSetOnEvent.htm)($nStop_Button, "_Main_Events")
GUISetOnEvent (http://www.autoitscript.com/autoit3/docs/functions/GUISetOnEvent.htm)($GUI_EVENT_CLOSE, "_Main_Events")

$paz = 10

For (http://www.autoitscript.com/autoit3/docs/keywords.htm#For) $Start = $paz To (http://www.autoitscript.com/autoit3/docs/keywords.htm#To) 0 Step (http://www.autoitscript.com/autoit3/docs/keywords.htm#Step) -1
;~ Select
;~ Case $Start >= 7
;~ GUICtrlSetColor($Label1, 0x00FF00)
;~ GUICtrlSetData($Label1, $Start)
;~ Case $Start > 3
;~ GUICtrlSetColor($Label1, 0xFF8000)
;~ GUICtrlSetData($Label1, $Start)
;~ Case $Start >= 0
;~ GUICtrlSetColor($Label1, 0xFF0000)
;~ GUICtrlSetData($Label1, $Start)
;~ EndSelect

If (http://www.autoitscript.com/autoit3/docs/keywords.htm#If) $iStop Then (http://www.autoitscript.com/autoit3/docs/keywords.htm#Then)
MsgBox (http://www.autoitscript.com/autoit3/docs/functions/MsgBox.htm)(64, 'Title', '_Timer() Stopped.')
$iStop = False (http://www.autoitscript.com/autoit3/docs/keywords.htm#False)
ExitLoop (http://www.autoitscript.com/autoit3/docs/keywords.htm#ExitLoop)
EndIf (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndIf)

Sleep (http://www.autoitscript.com/autoit3/docs/functions/Sleep.htm)(1000)
Next (http://www.autoitscript.com/autoit3/docs/keywords.htm#Next)

Opt (http://www.autoitscript.com/autoit3/docs/functions/Opt.htm)("GUIOnEventMode", $iOld_Opt)

Return (http://www.autoitscript.com/autoit3/docs/keywords.htm#Return)
EndFunc (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndFunc)

Func (http://www.autoitscript.com/autoit3/docs/keywords.htm#Func) _Main_Events()
Switch (http://www.autoitscript.com/autoit3/docs/keywords.htm#Switch) @GUI_CtrlId (http://www.autoitscript.com/autoit3/docs/macros.htm#@GUI_CtrlId)
Case (http://www.autoitscript.com/autoit3/docs/keywords.htm#Case) $GUI_EVENT_CLOSE
Exit (http://www.autoitscript.com/autoit3/docs/keywords.htm#Exit)
Case (http://www.autoitscript.com/autoit3/docs/keywords.htm#Case) $nStop_Button
$iStop = True (http://www.autoitscript.com/autoit3/docs/keywords.htm#True)
EndSwitch (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndSwitch)
EndFunc (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndFunc)

Ну или сразу использовать этот режим:

#include <GuiConstantsEx.au3>

Opt (http://www.autoitscript.com/autoit3/docs/functions/Opt.htm)("GUIOnEventMode", 1)

Global (http://www.autoitscript.com/autoit3/docs/keywords.htm#Global) $iStop = False (http://www.autoitscript.com/autoit3/docs/keywords.htm#False)

$hGUI = GUICreate (http://www.autoitscript.com/autoit3/docs/functions/GUICreate.htm)("Test", 200, 100)
$nStop_Button = GUICtrlCreateButton (http://www.autoitscript.com/autoit3/docs/functions/GUICtrlCreateButton.htm)("Stop _Timer()", 65, 35, 75, 25)

GUICtrlSetOnEvent (http://www.autoitscript.com/autoit3/docs/functions/GUICtrlSetOnEvent.htm)($nStop_Button, "_Main_Events")
GUISetOnEvent (http://www.autoitscript.com/autoit3/docs/functions/GUISetOnEvent.htm)($GUI_EVENT_CLOSE, "_Main_Events")

GUISetState (http://www.autoitscript.com/autoit3/docs/functions/GUISetState.htm)()

_Timer()

While (http://www.autoitscript.com/autoit3/docs/keywords.htm#While) 1
Sleep (http://www.autoitscript.com/autoit3/docs/functions/Sleep.htm)(10)
WEnd (http://www.autoitscript.com/autoit3/docs/keywords.htm#WEnd)

Func (http://www.autoitscript.com/autoit3/docs/keywords.htm#Func) _Timer()
$paz = 10

$iStop = False (http://www.autoitscript.com/autoit3/docs/keywords.htm#False)

For (http://www.autoitscript.com/autoit3/docs/keywords.htm#For) $Start = $paz To (http://www.autoitscript.com/autoit3/docs/keywords.htm#To) 0 Step (http://www.autoitscript.com/autoit3/docs/keywords.htm#Step) -1
;......

If (http://www.autoitscript.com/autoit3/docs/keywords.htm#If) $iStop Then (http://www.autoitscript.com/autoit3/docs/keywords.htm#Then)
MsgBox (http://www.autoitscript.com/autoit3/docs/functions/MsgBox.htm)(64, 'Title', '_Timer() Stopped.')
$iStop = False (http://www.autoitscript.com/autoit3/docs/keywords.htm#False)
ExitLoop (http://www.autoitscript.com/autoit3/docs/keywords.htm#ExitLoop)
EndIf (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndIf)

Sleep (http://www.autoitscript.com/autoit3/docs/functions/Sleep.htm)(1000)
Next (http://www.autoitscript.com/autoit3/docs/keywords.htm#Next)

Return (http://www.autoitscript.com/autoit3/docs/keywords.htm#Return)
EndFunc (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndFunc)

Func (http://www.autoitscript.com/autoit3/docs/keywords.htm#Func) _Main_Events()
Switch (http://www.autoitscript.com/autoit3/docs/keywords.htm#Switch) @GUI_CtrlId (http://www.autoitscript.com/autoit3/docs/macros.htm#@GUI_CtrlId)
Case (http://www.autoitscript.com/autoit3/docs/keywords.htm#Case) $GUI_EVENT_CLOSE
Exit (http://www.autoitscript.com/autoit3/docs/keywords.htm#Exit)
Case (http://www.autoitscript.com/autoit3/docs/keywords.htm#Case) $nStop_Button
$iStop = True (http://www.autoitscript.com/autoit3/docs/keywords.htm#True)
EndSwitch (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndSwitch)
EndFunc (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndFunc)

Medic84
22-04-2010, 13:45
Ну или сразу использовать этот режим: »
Да, совсем забыл про это :)
Но в больших программах это неудобно...

JaguarSP
22-04-2010, 18:39
Всем огромное спасибо, все сделал, все робит:-)))) Много нового подчерпнул для себя :-)




© OSzone.net 2001-2012