=======================定时关机精灵=======================
修改AutoIt的通知栏全局设置
Opt("TrayMenuMode", 1 + 2) Opt("TrayAutoPause", 0) ;点击图标时,脚本不暂停 ;创建快捷键,按下Ctrl+Alt+向下箭头则退出程序 HotKeySet("^!{DOWN}", "_HotKey")
创建GUI视图界面
Global $Form = GUICreate("定时关机精灵1.0", 308, 280, 192, 114) Const $shutdownOptionsWidth = 290 ;关机选项模块的宽 Const $shutdownOptionsHeight = 97 ;关机选项模块的高 Global $Group1 = GUICtrlCreateGroup("定时关机选项", 8, 8, $shutdownOptionsWidth, $shutdownOptionsHeight) Global $Label1 = GUICtrlCreateLabel("系统将在每天", 20, 32, 76, 17) Global $Combo1 = GUICtrlCreateCombo("", 93, 27, 40, 17) Global $Label2 = GUICtrlCreateLabel("时", 137, 32, 16, 17) Global $Combo2 = GUICtrlCreateCombo("", 153, 27, 40, 17) Global $Labe13 = GUICtrlCreateLabel("分执行下列操作", 200, 32, 88, 17) Global $Radio1 = GUICtrlCreateRadio("关机", 24, 70, 49, 17) Global $Radio2 = GUICtrlCreateRadio("重启", 96, 70, 57, 17) Global $Radio3 = GUICtrlCreateRadio("注销", 160, 70, 49, 17) Global $Radio4 = GUICtrlCreateRadio("锁定", 224, 70, 49, 17) Const $advancedOptionsWidth = 290 ;高级选项模块的宽 Const $advancedOptionsHeight = 121 ;高级选项模块的高 Const $advancedOptionsStartY = $shutdownOptionsHeight + 20 ;高级选项模块开始的Y轴位置 Global $Group3 = GUICtrlCreateGroup("高级选项", 8, $advancedOptionsStartY, $advancedOptionsWidth, $advancedOptionsHeight) Global $Checkbox1 = GUICtrlCreateCheckbox("启用自我保护,设置密码", 16, $advancedOptionsStartY + 20, 153, 17, $BS_AUTOCHECKBOX) Global $Input = GUICtrlCreateInput("", 173, $advancedOptionsStartY + 20, 80, 17, $ES_PASSWORD) Global $Checkbox2 = GUICtrlCreateCheckbox("程序运行时禁止修改系统时间。", 16, $advancedOptionsStartY + 42, 185, 17, $BS_AUTOCHECKBOX) Global $Checkbox3 = GUICtrlCreateCheckbox("每天8:30分自动与授时服务器校准时间。", 16, $advancedOptionsStartY + 64, 249, 17, $BS_AUTOCHECKBOX) Global $Checkbox4 = GUICtrlCreateCheckbox("当同时按下ctr1+a1t+UP键时,系统瞬间重启。", 16, $advancedOptionsStartY + 86, 257, 17, $BS_AUTOCHECKBOX) Global $Button1 = GUICtrlCreateButton("确定", 220, $advancedOptionsStartY + $advancedOptionsHeight + 10, 73, 25)
创建程序右键菜单
Global $ts1 = TrayCreateItem("程序设置") TrayCreateItem("") Global $ts2 = TrayCreateItem("系统休眠") Global $ts3 = TrayCreateItem("注销当前用户") Global $ts4 = TrayCreateItem("系统重启") Global $ts5 = TrayCreateItem("关闭系统") TrayCreateItem("") Global $ts0 = TrayCreateItem("退出程序")
创建用于选择时间的下拉框选项的两个常量
Const $set1 = "1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|00" Const $set2 = "00|01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59" ;程序写入注册表,开机自启动 RegWrite("HKLM\SOFTWARE\Microsoft\Windows\Currentversion\Run", "定时强制关机精灵", "REG_SZ", @ScriptFullPath) Global $hour,$minute,$pwd Global $type,$nettime,$hide,$lctime,$hotkey _main() Exit
以下是自定义的函数
;主入口函数 Func _main() _inird() GUICtrlSetData($Combo1, $set1, $hour) GUICtrlSetData($Combo2, $set2, $minute) While 1 If Number($hour) = @HOUR And Number($minute) = @MIN Then ExitLoop Local $_ts = TrayGetMsg() ;响应用户在右键菜单中的点击操作 Switch $_ts Case $ts0 _pdcheck("E") Case $ts1 _pdcheck("F") Case $ts2 ;休眠 _shutdown(5) Case $ts3 ;注销 _shutdown(3) Case $ts4 ;重启 _shutdown(2) Case $ts5 ;关机 _shutdown(1) EndSwitch WEnd ;退出循环则表示到了设置的时间,执行关机、重启等操作 _shutdown($type) EndFunc ;执行关机、重启等操作 Func _shutdown($type) For $step = 3 To 1 Step -1 TrayTip("警告", $step & "正在执行操作...", 0, 1) Sleep(1000) Next TrayTip("", "", 0) Switch $type Case 1 ;关机 Shutdown(1 + 4 + 8) Case 2 ;重启 Shutdown(2 + 4) Case 3 ;注销 Shutdown(0) Case 4 ;锁定 Send("#L") Case 5 ;休眠 Shutdown(64) EndSwitch Exit EndFunc Func _form() GUISetState(@SW_SHOW, $Form) While 1 Local $idMsg = GUIGetMsg() Local $gur1 = GUICtrlRead($Checkbox1) Local $gur2 = GUICtrlRead($Checkbox2) Local $gur3 = GUICtrlRead($Checkbox3) Local $gur4 = GUICtrlRead($Checkbox4) Switch $idMsg Case $Button1 ExitLoop Case $Radio1 $type = "1" Case $Radio2 $type = "2" Case $Radio3 $type = "3" Case $Radio4 $type = "4" Case $Checkbox1 If $gur1 = $GUI_CHECKED Then $hide = 1 GUICtrlSetstate($Input, $GUI_ENABLE) Else $hide = 0 GUICtrlSetState($Input, $GUI_DISABLE) EndIf Case $Checkbox2 If $gur2 = $GUI_CHECKED Then $lctime = 1 Else $lctime = 0 EndIf Case $Checkbox3 If $gur3 = $GUI_CHECKED Then $nettime = 1 Else $nettime =0 EndIf Case $Checkbox4 If $gur4 = $GUI_CHECKED Then $hotkey = 1 Else $hotkey = 0 EndIf Case $GUI_EVENT_CLOSE GUISetState(@SW_HIDE, $Form) _main() EndSwitch WEnd GUISetState(@SW_HIDE, $Form) _rfwin($type, $nettime, $hide, $lctime, $hotkey) _main() EndFunc ;将GUI的设置写入ini文件 Func _rfwin($type, $nettime, $hide, $lctime, $hotkey) Local $idMsg = GUIGetMsg() $hour = GUICtrlRead($Combo1) $minute = GUICtrlRead($Combo2) $pwd = GUICtrlRead($Input) Iniwrite(@ScriptDir & "\time.ini", "time", "hour", $hour) IniWrite(@ScriptDir & "\time.ini", "time", "minute", $minute) IniWrite(@ScriptDir & "\time.ini", "time", "type", $type) Iniwrite(@ScriptDir & "\time.ini", "config", "nettime", $nettime) Iniwrite(@ScriptDir & "\time.ini", "config", "hide", $hide) Iniwrite(@ScriptDir & "\time.ini", "config", "lctime", $lctime) Iniwrite(@ScriptDir & "\time.ini", "config", "hotkey", $hotkey) Iniwrite(@ScriptDir & "\time.ini", "config", "pwd", $pwd) EndFunc ;读取ini配置文件并表现在GUI上 Func _inird() ;将ini文件中的配置读取到变量中 $hour = IniRead(@ScriptDir & "\time.ini", "time","hour", "") $minute = IniRead(@ScriptDir & "\time.ini", "time", "minute", "") $type = IniRead(@ScriptDir & "\time.ini", "time", "type", "") $nettime = IniRead(@ScriptDir & "\time.ini", "config", "nettime", "") $hotkey = IniRead(@ScriptDir & "\time.ini", "config", "hotkey", "") $pwd = IniRead(@ScriptDir & "\time.ini", "config", "pwd", "") $hide = IniRead(@ScriptDir & "\time.ini", "config", "hide", "") $lctime = IniRead(@ScriptDir & "\time.ini", "config", "lctime", "") ;根据配置数据选中单选框 Switch $type Case 1 GUICtrlSetState($Radio1, $GUI CHECKED) Case 2 GUICtrlSetState($Radio2, $GUI_CHECKED) Case 3 GUICtrlSetState($Radio3, $GUI CHECKED) Case 4 GUICtrlSetState($Radio4, $GUI_CHECKED) EndSwitch If $nettime = 1 Then GUICtrlSetState($Checkbox3, $GUI_CHECKED) If $hotkey = 1 Then GUICtrlSetState($Checkbox4, $GUI_CHECKED) If $hide = 1 Then GUICtrlSetState($Checkbox1, $GUI_CHECKED) GUICtrlSetState($Input, $GUI_ENABLE) GUICtrlSetData($Input, $pwd) EndIf If $lctime = 1 Then GUICtrlsetstate($Checkbox2, $GUI_CHECKED) FileMove(@SystemDir & "\timedate.cpl", @ScriptDir & "\timedate.dll", 1) Else FileMove(@ScriptDir & "\timedate.dll", @SystemDir & "\timedate.cpl") EndIf Return EndFunc ;验证密码,实现退出或进入系统设置功能 Func _pdcheck($call) If $pwd <> "" And $hide = 1 Then Local $pdchk = GUICreate("验证密码", 240, 30) Local $idFile = GUICtrlCreateInput("", 10, 5, 150, 20, $ES_PASSWORD) GUICtrlSetState(-1, $GUI_DROPACCEPTED) Local $idBtn = GUICtrlCreateButton("Ok", 170, 5, 60, 20) GUISetState(@SW_SHOW) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE GUISetState(@SW_HIDE) _main() Case $idBtn If $pwd == GUICtrlRead($idFile) Then ExitLoop Else MsgBox(4096, "错误", "验证失败") EndIf EndSwitch WEnd EndIf GUISetState(@SW_HIDE) If $call = "F" Then _form() ElseIf $CALL = "E" Then FileMove(@ScriptDir & "\timedate.dll", @SystemDir & "\timedate.cpl") ;删除注册表中添加的开机自启动 RegDelete("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", "定时关机精灵") MsgBox(0, "", "成功退出程序") Exit EndIf EndFunc ;按下Ctrl+Alt+向下箭头按键后的回调函数 Func _HotKey() _pdcheck("E") EndFunc
|