本帖最后由 xiehuahere 于 2011-3-13 12:09 编辑
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
|