一:变量和常量的定义

Dim定义变量

Dim $a=1,$b=2,$c="Hello"

Const定义常量

Const $a=100,$b=200,$d="World"

二:逻辑控制

判断语句

1.If...ElseIf...Else...EndIf
if $a>0 Then
    MsgBox(0,"信息框","大于0")
ElseIf $a=0 Then
    MsgBox(0,"信息框","=0")
Else
    MsgBox(0,"信息框","<0")
    EndIf
2.Select...Case...EndSelect

如果表达式为 true, 则执行从下一行开始, 直到下一个 Case 或 EndSelect 为止的所有语句.如果有多个 Case 表达式成立,则只执行第一个 Case 语句

Dim $a=InputBox("输入","分数")
Select
    Case $a>=90 And $a<100
        MsgBox(0,"信息框","A")
    Case $a>=60 And $a<90
        MsgBox(0,"信息框","B")
    Case $a>=0 And $a<60
        MsgBox(0,"信息框","C")
EndSelect
3.Switch...Case...EndSwitch

Switch <表达式>
Case <值> [To <值>] [,<值> [To <值>] ...]
语句 1
...
[Case <值> [To <值>] [,<值> [To <值>] ...]
语句 2
...]
[Case Else
语句 N
...]
EndSwitch

Switch @HOUR
    Case 6 To 11
        $sMsg = "Good Morning"
    Case 12 To 17
        $sMsg = "Good Afternoon"
    Case 18 To 21
        $sMsg = "Good Evening"
    Case Else
        $sMsg = "What are you still doing up?"
EndSwitch

+=
加法赋值. 例如 $vVar += 1 (添加 1 到变量 $vVar)

循环语句

4.While...WEnd

While <表达式>
语句
...
WEnd
如果表达式为 true(条件存在), 则连续执行 Wend 循环语句, 直到表达式为 false(条件消失).

Dim $a=InputBox("输入","次数")
While $a<5
    MsgBox(0,"信息框",$a)
    $a=$a+1
WEnd
5.For...To...Step...Next

For <变量> = <开始> To <停止> [Step <步进值>]
语句
...
Next

For $i = 1 To 5 
    MsgBox(0, "", "Count down!" & @CRLF & $i)
Next

三:自定义函数

Func...Return...EndFunc

Func functioname ( [Const] [ByRef] $param1, ..., [Const] [ByRef] $paramN, $optionalpar1 = value, ...)
...
[Return [value]]
EndFunc

xiaoxi()
Func xiaoxi()
    MsgBox(0,"消息","消息函数")
EndFunc
Dim $a=1,$b=2,$c

$c=jisuan($a,$b)
MsgBox(0,"消息",$c)
Func jisuan($x,$y)
    $z=$x+$y
    Return $z
EndFunc

四:软件自动安装

1.普通安装

Run运行程序
WinWait等待窗口界面
WinActivate 激活窗口
ControlClick鼠标点击

TIM安装代码示例

Run("TIM3.4.5.22071.exe")
WinWait("腾讯TIM安装向导","立即安装")
Sleep(1000)
WinActivate("腾讯TIM安装向导","立即安装")
Sleep(1000)
ControlClick("腾讯TIM安装向导","立即安装","Button4")
ControlClick("腾讯TIM安装向导","立即安装","Button10")
Sleep(1000)
ControlClick("腾讯TIM安装向导","立即安装","Button3")
Exit

2.类名为高级模式

发送鼠标点击命令到指定控件
ControlClick ( "标题", "文本", 控件ID, 按钮 = "left", 点击次数 = 1 , X 坐标 , Y 坐标)
设置控件文本.
ControlSetText ( "标题", "文本", 控件ID, "新文本" , 标志 = 0 ) 发送鼠标点击命令到指定控件
好压安装示例

Run("haozip_v5.0.exe")
Local $pid=WinWait("2345好压v5.0安装")
WinActivate($pid,"2345好压v5.0安装")
Sleep(1000)
ControlClick($pid,"2345好压v5.0安装","[class:ATL:10044B50]","left",1,340,333)
Sleep(1000)
ControlSetText($pid,"2345好压v5.0安装","[class:Edit]","D:\HaoZip")
Sleep(1000)
ControlClick($pid,"2345好压v5.0安装","[class:ATL:10044B50]","left",1,305,210)
Exit

3.消息机制安装

获取窗口内部句柄.

WinGetHandle ( "标题" [, "文本"] )

include <SendMessage.au3>

_SendMessage
YY安装示例

#include <SendMessage.au3>
Run("YYSetup-7.0.0.6-zh-CN.exe")
WinWait("YY7 安装")
WinActivate("YY7 安装") ;返回窗口句柄
$pid=WinGetHandle("YY7 安装") ;获取窗口内部句柄
_SendMessage($pid,513,"",0x012600F1)
_SendMessage($pid,514,"",0x012600F1)
Sleep(5000)
_SendMessage($pid,513,"",0x01F0093)
_SendMessage($pid,514,"",0x01F0093)
_SendMessage($pid,513,"",0x012E0086)
_SendMessage($pid,514,"",0x012E0086)
_SendMessage($pid,513,"",0x014C0085)
_SendMessage($pid,514,"",0x014C0085)
_SendMessage($pid,513,"",0x012B0193)
_SendMessage($pid,514,"",0x012B0193)
_SendMessage($pid,513,"",0x010C0175)
_SendMessage($pid,514,"",0x010C0175)
Sleep(5000)
_SendMessage($pid,513,"",0x016D011D)
_SendMessage($pid,514,"",0x016D011D)
Exit

4.句柄鼠标点击安装

最后修改:2023 年 03 月 09 日
如果觉得我的文章对你有用,请随意赞赏