下面是 AutoIt 提供的循环语句:
For...Next
#include <MsgBoxConstants.au3>
For $i = 5 To 1 Step -1
MsgBox($MB_SYSTEMMODAL, "", "倒计时!" & @CRLF & $i)
Next
MsgBox($MB_SYSTEMMODAL, "", "点火升空!")While...WEnd
; 设置终止脚本的热键.
HotKeySet("{ESC}", "_Terminate")
Example()
Func Example()
; 初始化本地变量.
Local $aMgp = 0
; 创建一个无限循环, 将始终为 1. 因此 True.
While 1
; 分配本地变量一个光标的坐标位置值 (数组).
$aMgp = MouseGetPos()
; 在光标附近显示其坐标位置的工具提示.
ToolTip("x 坐标: " & $aMgp[0] & ", y 坐标: " & $aMgp[1], $aMgp[0] + 10, $aMgp[1] + 10)
; 避免 CPU 使用率过高.
Sleep(50)
WEnd
EndFunc ;==>Example
Func _Terminate()
Exit
EndFunc ;==>_TerminateDo...Until
#include <MsgBoxConstants.au3>
Local $i = 0
Do
MsgBox($MB_SYSTEMMODAL, "", "$i 的当前值: " & $i) ; 显示 $i 的值.
$i = $i + 1 ; 或者也可以使用 $i += 1.
Until $i = 10 ; 增加 $i 的值, 直到它等于 10 (时结束循环).For...In...Next
#include <MsgBoxConstants.au3>
Example()
Func Example()
; 使用一个数组
Local $aArray[4]
$aArray[0] = "a"
$aArray[1] = 0
$aArray[2] = 1.3434
$aArray[3] = "测试"
Local $sString = ""
For $vElement In $aArray
$sString = $sString & $vElement & @CRLF
Next
MsgBox($MB_SYSTEMMODAL, "", "For..IN 枚举数组元素:" & @CRLF & "结果: " & @CRLF & $sString)
; 使用一个对象集合
Local $oShell = ObjCreate("shell.application")
Local $oShellWindows = $oShell.windows
If IsObj($oShellWindows) Then
$sString = ""
For $Window In $oShellWindows
$sString = $sString & $Window.LocationName & @CRLF
Next
MsgBox($MB_SYSTEMMODAL, "", "你有以下窗口被打开:" & @CRLF & $sString)
Else
MsgBox($MB_SYSTEMMODAL, "", "你没有打开的 shell 窗口.")
EndIf
EndFunc ;==>Example
上一篇:4.【Au3基础】条件语句