很多autoit的用户(包括一些老用户)都认为OnEvent模式是简单的把GUIGetMsg 替换成了GUICtrlSetOnEvent,因此也产生了OnEvent模式不适合复杂GUI的误解。本文将具体阐述OnEvent模式的高级应用和使用技巧。
一、OnEvent模式的优势
OnEvent模式和MessageLoop模式最大的区别在于一个是主动,一个是被动。前者只有产生了消息才会执行函数,而后者则是不断地获取直到得到消息,这样CPU占用上OnEvent模式有很大优势。
另外,OnEvent模式能在运行脚本的同时获取GUI事件消息,这样我们就能把非常耗时的代码放在主循环里运行,而在其他的自定义函数里处理GUI消息。
比如下面这段代码,程序一直在主循环里更新控件内容,与此同时程序还能响应GUI消息:#include <GuiConstants.au3>
GUICreate("MyGUI", 372, 88, -1, -1, BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS))
$Progress_1 = GUICtrlCreateProgress(10, 10, 370, 20)
$Button_2 = GUICtrlCreateButton("start", 10, 40, 60, 30)
$Button_3 = GUICtrlCreateButton("pause", 80, 40, 60, 30)
$Button_4 = GUICtrlCreateButton("exit", 150, 40, 70, 30)
Opt("GUIOnEventMode", 1)
Dim $start = 0, $ostart
GUISetState()
GUISetOnEvent($GUI_EVENT_CLOSE, "gui")
GUICtrlSetOnEvent($Button_2, "gui")
GUICtrlSetOnEvent($Button_3, "gui")
GUICtrlSetOnEvent($Button_4, "gui")
While 1
If $start >= 0 Then
Sleep(50)
$start += 1
If $start > 100 Then $start = 0
If $start >= 0 Then
$ostart = $start
GUICtrlSetData($Progress_1, $start)
Else
ContinueLoop
EndIf
EndIf
WEnd
Exit
Func gui()
Switch @GUI_CtrlId
Case $GUI_EVENT_CLOSE, $Button_4
Exit
Case $Button_2
$start = 0
Case $Button_3
If $start = -100 Then
$start = $ostart
Else
$start = -100
EndIf
EndSwitch
EndFunc ;==>gui
如果用MessageLoop模式来实现相同的效果,不但代码复杂,对GUI消息的响应也相当迟钝。也就是说MessageLoop模式只能在一个时间里做一件事,而OnEvent模式则能在做同样的事的同时抽出手去处理GUI事件。
所以,对于一些操作非常耗时或者GUI非常复杂的脚本,我们应首选OnEvent模式。
二、宏的妙用
许多人都有一个误解,认为一个事件只能注册一个函数,因此一些人把脚本写完之后代码里到处都是Func 、EndFunc。实际上,autoit本就提供了@GUI_CTRLID、@GUI_WINHANDLE、@GUI_CTRLHANDLE这三个宏,合理利用宏能大大简化代码
事实上,帮助文件里就有过说明:
注意:使用同一个函数来响应多种事件是完全合法的,记住要灵活使用 @GUI_CTRLID 这个宏。比如说,您可以注册所有系统事件到同一个函数。
下面是一个例子:#include <GuiConstants.au3>
GuiCreate("MyGUI", 340, 60,-1, -1 , BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS))
$Button_1 = GuiCtrlCreateButton("Button1", 10, 10, 100, 40)
$Button_2 = GuiCtrlCreateButton("Button2", 120, 10, 100, 40)
$Button_3 = GuiCtrlCreateButton("Button3", 230, 10, 100, 40)
Opt("GUIOnEventMode", 1)
GUISetState()
GUISetOnEvent($GUI_EVENT_CLOSE, "gui")
GUICtrlSetOnEvent($Button_2, "gui")
GUICtrlSetOnEvent($Button_3, "gui")
GUICtrlSetOnEvent($Button_1, "gui")
While 1
;;;
WEnd
Exit
Func gui()
Switch @GUI_CtrlId
Case $GUI_EVENT_CLOSE
Exit
Case $Button_2
MsgBox(0,"","按下了Button_2")
Case $Button_3
MsgBox(0,"","按下了Button_3")
Case $Button_1
MsgBox(0,"","按下了Button_1")
EndSwitch
EndFunc ;==>gui
在上面的例子里,我将全部的事件都注册到一个函数里,然后根据@GUI_CTRLID来判断具体的事件,这样代码就简洁多了。
而在关闭多窗口时@GUI_WINHANDLE则会起到很大作用,下面有个例子:#include <GuiConstants.au3>
Opt("GUIOnEventMode", 1)
$main = GuiCreate("MyGUI", 340, 60,-1, -1 , BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS))
GUISetOnEvent($GUI_EVENT_CLOSE, "gui")
$Button_1 = GuiCtrlCreateButton("Button1", 10, 10, 100, 40)
$Button_2 = GuiCtrlCreateButton("Button2", 120, 10, 100, 40)
$Button_3 = GuiCtrlCreateButton("Button3", 230, 10, 100, 40)
GUISetState()
GuiCreate("子窗口1", 140, 60,10,100,-1,-1,$main)
GUISetOnEvent($GUI_EVENT_CLOSE, "gui")
GUISetState()
GuiCreate("子窗口2", 140, 60,160,100,-1,-1,$main)
GUISetOnEvent($GUI_EVENT_CLOSE, "gui")
GUISetState()
GuiCreate("子窗口3", 140, 60,300,100,-1,-1,$main)
GUISetOnEvent($GUI_EVENT_CLOSE, "gui")
GUISetState()
GUISetOnEvent($GUI_EVENT_CLOSE, "gui")
GUICtrlSetOnEvent($Button_2, "gui")
GUICtrlSetOnEvent($Button_3, "gui")
GUICtrlSetOnEvent($Button_1, "gui")
While 1
;;;
WEnd
Exit
Func gui()
Switch @GUI_CtrlId
Case $GUI_EVENT_CLOSE
Switch @GUI_WINHANDLE
Case $main
Exit
Case Else
MsgBox(0,"","关闭窗口:"&WinGetTitle(@GUI_WINHANDLE))
GUIDelete(@GUI_WINHANDLE)
EndSwitch
Case $Button_2
MsgBox(0,"","按下了Button_2")
Case $Button_3
MsgBox(0,"","按下了Button_3")
Case $Button_1
MsgBox(0,"","按下了Button_1")
EndSwitch
EndFunc ;==>gui
我们可以看到,主窗口关闭时程序会直接退出,而其余窗口关闭时则会删掉关闭的窗口。这里有个技巧,就是对于非主窗口,我们可以在Case Else的情况下用GUIDelete(@GUI_WinHandle)直接处理窗口,而不需要为每个窗口都写一个关闭函数。
三、多窗口操作技巧
建立多窗口时两种选择,一是在脚本开头就建立好全部窗口,然后把多余的窗口隐藏起来,在需要时再将隐藏的窗口显示出来。二是只建立主窗口,在需要时才建立子窗口。前一种选择会多占用内存,但响应速度非常快,适合使用多个非常复杂的窗口的脚本;而后一种选择明显要节约内存,但临时建立窗口会花费一些时间,适合一些子窗口简单的脚本。
至于帮助文件里提到的GUISwitch函数,实际上很少会用到。
下面分别是两种选择的范例:
隐藏子窗口型:#include <GuiConstants.au3>
Opt("GUIOnEventMode", 1)
$main = GuiCreate("MyGUI", 340, 60,-1, -1 , BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS))
GUISetOnEvent($GUI_EVENT_CLOSE, "gui")
$Button_1 = GuiCtrlCreateButton("显示窗口1", 10, 10, 100, 40)
$Button_2 = GuiCtrlCreateButton("显示窗口2", 120, 10, 100, 40)
$Button_3 = GuiCtrlCreateButton("显示窗口3", 230, 10, 100, 40)
GUISetState()
$s1 = GuiCreate("子窗口1", 140, 60,10,100,-1,-1,$main)
GUISetOnEvent($GUI_EVENT_CLOSE, "gui")
GUISetState(@SW_HIDE)
$s2 = GuiCreate("子窗口2", 140, 60,160,100,-1,-1,$main)
GUISetOnEvent($GUI_EVENT_CLOSE, "gui")
GUISetState(@SW_HIDE)
$s3 = GuiCreate("子窗口3", 140, 60,300,100,-1,-1,$main)
GUISetOnEvent($GUI_EVENT_CLOSE, "gui")
GUISetState(@SW_HIDE)
GUISetOnEvent($GUI_EVENT_CLOSE, "gui")
GUICtrlSetOnEvent($Button_2, "gui")
GUICtrlSetOnEvent($Button_3, "gui")
GUICtrlSetOnEvent($Button_1, "gui")
While 1
;;;
WEnd
Exit
Func gui()
Switch @GUI_CtrlId
Case $GUI_EVENT_CLOSE
Switch @GUI_WINHANDLE
Case $main
Exit
Case Else
GUISetState(@SW_HIDE,@GUI_WINHANDLE)
EndSwitch
Case $Button_2
GUISetState(@SW_SHOW,$s2)
Case $Button_3
GUISetState(@SW_SHOW,$s3)
Case $Button_1
GUISetState(@SW_SHOW,$s1)
EndSwitch
EndFunc ;==>gui
删除子窗口型:#include <GuiConstants.au3>
Opt("GUIOnEventMode", 1)
$main = GuiCreate("MyGUI", 340, 60,-1, -1 , BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS))
GUISetOnEvent($GUI_EVENT_CLOSE, "gui")
$Button_1 = GuiCtrlCreateButton("显示窗口1", 10, 10, 100, 40)
$Button_2 = GuiCtrlCreateButton("显示窗口2", 120, 10, 100, 40)
$Button_3 = GuiCtrlCreateButton("显示窗口3", 230, 10, 100, 40)
GUISetState()
GUISetOnEvent($GUI_EVENT_CLOSE, "gui")
GUICtrlSetOnEvent($Button_2, "gui")
GUICtrlSetOnEvent($Button_3, "gui")
GUICtrlSetOnEvent($Button_1, "gui")
While 1
;;;
WEnd
Exit
Func gui()
Switch @GUI_CtrlId
Case $GUI_EVENT_CLOSE
Switch @GUI_WINHANDLE
Case $main
Exit
Case Else
GUIDelete(@GUI_WINHANDLE)
EndSwitch
Case $Button_2
if WinExists("子窗口2") = 0 Then
GuiCreate("子窗口2", 140, 60,10,100,-1,-1,$main)
GUISetState()
GUISetOnEvent($GUI_EVENT_CLOSE, "gui")
EndIf
Case $Button_3
if WinExists("子窗口3") = 0 Then
GuiCreate("子窗口3", 140, 60,160,100,-1,-1,$main)
GUISetState()
GUISetOnEvent($GUI_EVENT_CLOSE, "gui")
EndIf
Case $Button_1
if WinExists("子窗口1") = 0 Then
GuiCreate("子窗口1", 140, 60,300,100,-1,-1,$main)
GUISetState()
GUISetOnEvent($GUI_EVENT_CLOSE, "gui")
EndIf
EndSwitch
EndFunc ;==>gui
四、一些技巧
除去上面几节提到的一些,OnEvent模式下还有几项实用的技巧:
1.可控下载
在下载时,我们可以把下载函数放到主循环里进行,再根据某个变量的变化来停止、开始下载。而我们可以根据不同的GUI事件来控制这个变量。下面是一个例子:#include <GuiConstants.au3>
Opt("GUIOnEventMode", 1)
Dim $start,$stop
GuiCreate("MyGUI", 392, 84,-1, -1 , BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS))
$Input_1 = GuiCtrlCreateInput("http://www.geocities.com/z8519312/ComicsDown-203.zip", 10, 10, 280, 30)
$Button_2 = GuiCtrlCreateButton("下载", 300, 10, 80, 30)
$Progress_3 = GuiCtrlCreateProgress(10, 50, 370, 20)
GUISetOnEvent($GUI_EVENT_CLOSE, "gui")
GUICtrlSetOnEvent($Button_2,"gui")
GuiSetState()
While 1
If $start=1 Then
GUICtrlSetData($Button_2,"停止")
$url = GUICtrlRead($Input_1)
$size = InetGetSize($url)
InetGet ($url, @TempDir&"\d.tem",0,1)
Do
Sleep(500)
GUICtrlSetData($Progress_3,@InetGetBytesRead /$size*100)
If $stop = 1 Then
InetGet ("abort")
ExitLoop
EndIf
Until @InetGetActive = 0
GUICtrlSetData($Button_2,"下载")
$stop = 0
$start = 0
EndIf
WEnd
Exit
Func gui()
Switch @GUI_CtrlId
Case $GUI_EVENT_CLOSE
Exit
Case $Button_2
If GUICtrlRead($Button_2) = "下载" Then
$start =1
Else
$stop = 1
EndIf
EndSwitch
EndFunc
2.用数组储存控件ID
有时候我们需要注册的大量控件,如果是每一个控件都用GUICtrlSetOnEvent来注册,代码量会非常大,这时用数组就会省事得多。下面是个例子:Dim $Button[3]
$Button[0] = GuiCtrlCreateButton("Button1", 10, 10, 100, 40)
$Button[1] = GuiCtrlCreateButton("Button2", 120, 10, 100, 40)
$Button[2] = GuiCtrlCreateButton("Button3", 230, 10, 100, 40)
For $i =0 To 2
GUICtrlSetOnEvent($Button[$i], "gui")
Next
这里有个问题,绝大多数人都使用KODA来建立窗口(其实我自己用的是GUIBuilder),而KODA不能以数组来命名控件ID,只能用button_1、button_2之类的名称来命名控件ID,这时我们就可以用Eval函数来解决:$Button_1 = GuiCtrlCreateButton("Button1", 10, 10, 100, 40)
$Button_2 = GuiCtrlCreateButton("Button2", 120, 10, 100, 40)
$Button_3 = GuiCtrlCreateButton("Button3", 230, 10, 100, 40)
For $i =0 To 2
GUICtrlSetOnEvent(Eval("Button_"&String($i)), "gui")
Next
——END—— |