[已解决]如何做含有倒计时功能的程序?请高手帮忙改改代码
本帖最后由 hnfeng 于 2014-2-12 16:52 编辑XT800 和 TeamViewer 都是远程控制软件,我常常用于在家里控制公司的电脑。但是这两个软件都有个问题,就是有时连接失败,此时需要重新启动服务才行,但是公司电脑旁没有人,无法操作。于是我想搞个自动重启这两个软件的服务,得到目的。
因为初学AU3,如果是一个按钮对应一个功能的程序还好对付,但是需要倒计时、中断倒计时 等,脑袋快要爆了,不知道怎么解决,请高手帮忙修改一下。
主要想实现的是:
1、按“立即重启服务”后就重启被选中的服务,即 Case $Button2 里面的内容。
2、按“开始延时”后,从 $Slider1 得到数值(小时),然后倒计时。同时按钮变成“停止延时”。
如果延时到了,就执行重启服务的操作。然后继续下一轮倒计时。
如果延时没到按下了“停止延时”,就还原到初始状态
3、退出按钮 随时按均可退出#RequireAdmin
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <ServiceControl.au3>
#include <SliderConstants.au3>
_Main()
Func _Main()
$Form1 = GUICreate("Form1", 282, 276, 424, 287)
$Checkbox1 = GUICtrlCreateCheckbox("XT800", 64, 12, 57, 33)
If Not _ServiceExists("", "XT800Service") Then GUICtrlSetState(-1, $GUI_DISABLE)
$Checkbox2 = GUICtrlCreateCheckbox("TeamViewer9", 144, 16, 97, 25)
If Not _ServiceExists("", "TeamViewer9") Then GUICtrlSetState(-1, $GUI_DISABLE)
$Button1 = GUICtrlCreateButton("开始延时", 16, 128, 89, 33)
$Button2 = GUICtrlCreateButton("立即重启服务", 116, 128, 89, 33)
$Button3 = GUICtrlCreateButton("退出", 216, 128, 49, 33)
$Label3 = GUICtrlCreateLabel("00:00:00", 18, 192, 244, 64, $SS_CENTER)
GUICtrlSetFont(-1, 36, 400, 0, "Tahoma")
GUICtrlSetResizing(-1, $GUI_DOCKWIDTH)
$Group1 = GUICtrlCreateGroup(" 延时 ", 8, 48, 265, 57)
$Slider1 = GUICtrlCreateSlider(16, 64, 249, 25, $TBS_TOOLTIPS)
GUICtrlSetLimit(-1, 24, 0)
GUICtrlSetData(-1, 1)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)
Do
Local $_SliderTime = GUICtrlRead($Slider1)
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button1
GUICtrlSetData($Button1, "停止延时")
Local $iTime = $_SliderTime * 3600
For $i = 0 To $iTime
Sleep(500)
$m = GUIGetMsg()
If $m = -3 Then ExitLoop
If $m = $Button1 Then
GUICtrlSetData($Button1, "开始延时")
Else
Local $H = StringRight("0" & Int($iTime / 3600), 2)
Local $m = StringRight("0" & Int(($iTime - $H * 3600) / 60), 2)
Local $S = StringRight("0" & Int($iTime - $H * 3600 - $m * 60), 2)
GUICtrlSetData($Label3, $H & ":" & $m & ":" & $S)
EndIf
Next
Case $Button2
If GUICtrlRead($Checkbox1) = 1 Then
Local $iServ = "XT800Service"
Sleep(2000)
_StopService("", $iServ)
Sleep(2000)
_StartService("", $iServ)
EndIf
If GUICtrlRead($Checkbox2) = 1 Then
Local $iServ = "TeamViewer9"
Sleep(2000)
_StopService("", $iServ)
Sleep(2000)
_StartService("", $iServ)
EndIf
Case $Button3
Exit
EndSwitch
Until $nMsg = $GUI_EVENT_CLOSE
EndFunc
#RequireAdmin
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <ServiceControl.au3>
#include <SliderConstants.au3>
$Form1 = GUICreate("Form1", 282, 276, 424, 287)
$Checkbox1 = GUICtrlCreateCheckbox("XT800", 64, 12, 57, 33)
If Not _ServiceExists("", "XT800Service") Then
GUICtrlSetState(-1, $GUI_DISABLE)
Else
GUICtrlSetState(-1, $GUI_CHECKED)
EndIf
$Checkbox2 = GUICtrlCreateCheckbox("TeamViewer9", 144, 16, 97, 25)
If Not _ServiceExists("", "TeamViewer9") Then
GUICtrlSetState(-1, $GUI_DISABLE)
Else
GUICtrlSetState(-1, $GUI_CHECKED)
EndIf
$Button1 = GUICtrlCreateButton("开始延时", 16, 128, 89, 33)
$Button2 = GUICtrlCreateButton("立即重启服务", 116, 128, 89, 33)
$Button3 = GUICtrlCreateButton("退出", 216, 128, 49, 33)
$Label3 = GUICtrlCreateLabel("00:00:00", 18, 192, 244, 64, $SS_CENTER)
GUICtrlSetFont(-1, 36, 400, 0, "Tahoma")
GUICtrlSetResizing(-1, $GUI_DOCKWIDTH)
$Group1 = GUICtrlCreateGroup(" 延时 ", 8, 48, 265, 57)
$Slider1 = GUICtrlCreateSlider(16, 64, 249, 25, $TBS_TOOLTIPS)
GUICtrlSetLimit(-1, 24, 0)
GUICtrlSetData(-1, 1)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)
_Main()
Func _Main()
Do
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button1
Global $iTime = GUICtrlRead($Slider1)
GUICtrlSetData($Label3, StringFormat("%02d:%02d:%02d\n", $iTime, 00, 00))
If GUICtrlRead($Button1) = "开始延时" Then
GUICtrlSetData($Button1, "停止延时")
AdlibRegister('timer', 1000)
Else
GUICtrlSetData($Button1, "开始延时")
AdlibUnRegister('timer')
EndIf
Case $Button2
reset()
Case $Button3
Exit
EndSwitch
Until $nMsg = $GUI_EVENT_CLOSE
EndFunc ;==>_Main
Func timer()
$l = Number(StringLeft(GUICtrlRead($Label3), 2))
$m = Number(StringMid(GUICtrlRead($Label3), 4, 2))
$n = Number(StringRight(StringTrimRight(GUICtrlRead($Label3), 1), 2))
;倒计时
If $n > 0 Then
$n -= 1
Else
$n = 59
If $m <> 0 Then
$m -= 1
Else
$m = 59
If $l <> 0 Then
$l -= 1
Else
reset()
If $iTime < 1 Then
$l = 0
Else
$l = $iTime - 1
EndIf
EndIf
EndIf
EndIf
GUICtrlSetData($Label3, StringFormat("%02d:%02d:%02d\n", $l, $m, $n))
EndFunc ;==>timer
Func reset()
If GUICtrlRead($Checkbox1) = 1 Then
Local $iServ = "XT800Service"
Sleep(2000)
_StopService("", $iServ)
Sleep(2000)
_StartService("", $iServ)
EndIf
If GUICtrlRead($Checkbox2) = 1 Then
Local $iServ = "TeamViewer9"
Sleep(2000)
_StopService("", $iServ)
Sleep(2000)
_StartService("", $iServ)
EndIf
EndFunc ;==>reset
希望楼主完全做好后能共享你的作品。我也用的 TeamViewer,经常连不上远端,特别是突然断线了更是需要对方帮助重启,麻烦的很。 回复 2# zldfsz
太感谢了,多谢多谢
{:face (316):} 回复 3# imlzr
2楼代码已经能用了,你编译一下就可用 学习哈,我也有这个问题 都想省事,连论坛里现成的代码都不去好好找找,我靠 本帖最后由 imlzr 于 2014-2-12 17:28 编辑
能确认有TV9的服务,如下图:
编译时报了两个错,我没有安装XT800,我就把相关语句直接屏了的。 本帖最后由 hnfeng 于 2014-2-12 17:46 编辑
缺少 ServiceControl.au3
可安装其他版本的AU3,或者这里下载:
http://www.autoitx.com/thread-43520-1-1.html
另外,如果你没有安装XT800,它的显示是灰色的 那个帖子提供的是Services.au3,我把文件名改成ServiceControl.au3,这样对吗?
页:
[1]