AutoIt queues events and only actions them after you return from a running function.
也就是说,onEvent模式下,AutoIt以消息队列的形式处理事件。主体本身是个循环,若函数中又包含有一个循环,当且仅当事件处理函数返回,回到主体循环中,才能接着处理下一个事件。
具体建议去看看这个WiKi:
http://www.autoitscript.com/wiki/Interrupting_a_running_function
我这里也给个小例子吧:
用GUIRegisterMsg()拦截点击button时发送的消息,也就是会被AutoIt放入其消息队列中等待处理的消息,交给自定义的MY_WM_COMMAND函数处理,在该函数中设置标志位,使得前一事件的处理函数中的循环退出,这样AutoIt就能接着处理我们想要它处理的消息了。#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiEdit.au3>
Opt("GUIOnEventMode", 1)
; Declare a flag
$fInterrupt = 0
#Region ### START Koda GUI section ###
$Form1 = GUICreate("Form1", 252, 327, 192, 124, $WS_CAPTION)
$Edit1 = GUICtrlCreateEdit("", 8, 8, 233, 257, BitOR($ES_AUTOVSCROLL,$ES_AUTOHSCROLL,$ES_READONLY,$ES_WANTRETURN,$WS_HSCROLL,$WS_VSCROLL))
GUICtrlSetData(-1, "This is the default text.")
$Button1 = GUICtrlCreateButton("Start", 16, 280, 113, 33)
GUICtrlSetOnEvent(-1, "FillEvent")
$Button2 = GUICtrlCreateButton("Exit", 168, 280, 65, 33)
GUICtrlSetOnEvent(-1, "ExitEvent")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
; Intercept Windows command messages with out own handler
GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND")
While 1
Sleep(10)
WEnd
Func FillEvent()
GUICtrlSetState($Button1, $GUI_DISABLE)
GUICtrlSetData($Edit1, "")
While 1
_GUICtrlEdit_AppendText($Edit1, "看我成功退出吧, oh yeah~~" & @CRLF)
If $fInterrupt <> 0 Then
$fInterrupt = 0
Return
EndIf
Sleep(60)
WEnd
GUICtrlSetState($Button1, $GUI_ENABLE)
EndFunc ;==>ConnectEvent
Func ExitEvent()
Exit 0
EndFunc ;==>ExitEvent
Func MY_WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
; Button2 was pressed so set the flag
If BitAND($wParam, 0x0000FFFF) =$Button2 Then $fInterrupt = 1
Return $GUI_RUNDEFMSG
EndFunc ;==>MY_WM_COMMAND 目前用的7楼的例子,还算好用,谢谢各位! 第二按钮跳出循环.
开关按钮跳出循环:
3mile 发表于 2011-3-12 11:47 http://www.autoitx.com/images/common/back.gif
很不错的方法。。 au3是单线程的,貌似不是那么简单。关注中。。。 看了下代码,几个例子应该都是执行完一次循环,然后通过检测FLAG判断是否退出循环。如果循环的内容比较复杂,如代码很长,中途是没办法退出循环的,得等此次执行完再行判断。等待大家共同探讨。 对7楼的代码有漏洞,改了一下:#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Local $Flg = False
Local $t = 0
Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 318, 120, 468, 171)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")
$Button1 = GUICtrlCreateButton("开始循环", 8, 84, 57, 25)
GUICtrlSetOnEvent(-1, "Button1Click")
$Button4 = GUICtrlCreateButton("结束循环", 238, 84, 57, 25)
GUICtrlSetOnEvent(-1, "Button2Click")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
If $Flg = True Then
Ab()
Else
Sleep(200)
EndIf
WEnd
Func Ab()
While 1
ToolTip("正在循环")
Sleep(100)
If $Flg = False Then
ExitLoop
ToolTip("")
EndIf
WEnd
EndFunc ;==>Ab
Func Button1Click()
$Flg = True
EndFunc ;==>Button1Click
Func Button2Click()
$Flg = False
ToolTip("")
msgbox(0,0,"你按了结束循环")
EndFunc ;==>Button2Click
Func Form1Close()
Exit
EndFunc ;==>Form1Close
页:
1
[2]