找回密码
 加入
搜索
查看: 4774|回复: 2

[AU3基础] 定时器,在到达定时事件时重新定时后不执行了

[复制链接]
发表于 2014-4-4 23:05:34 | 显示全部楼层 |阅读模式
本帖最后由 aqtd 于 2014-4-4 23:07 编辑

一个定时器,在到了设定的时间时,出现一个30秒倒计时窗口,当计时窗口出现时我想重新定时。可是到了重新定时的时间了就不执行了
#NoTrayIcon
#Region ;**** 参数创建于 ACNWrapper_GUI ****
#PRE_Icon=D:\AutoIt3.3.9.0\Aut2Exe\Icons\64x64(98).ico
#PRE_Outfile=定时关机哭.exe
#EndRegion ;**** 参数创建于 ACNWrapper_GUI ****
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Timers.au3>
;TraySetClick(3)
Opt("TrayMenuMode", 3)
Opt("TrayOnEventMode",1)
Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ### Form=  
$g_szVersion = "shutdown 1.0.0.1"
If WinExists($g_szVersion) Then
        MsgBox(32, "", "别点了!程序已经运行了。")
        Exit ; 此脚本已经运行了
EndIf
AutoItWinSetTitle($g_szVersion)
;==========创建设置窗口
    $Form1 = GUICreate("简单定时关机器",  279, 139, -1, -1,BitOR($ws_caption,$ws_sysmenu));只有关闭按钮窗口
        GUICtrlSetOnEvent($GUI_EVENT_CLOSE ,"_form1")
    $Group1 = GUICtrlCreateGroup("时间设定", 8, 15, 257, 73)
    $Label2 = GUICtrlCreateLabel("指定关机时间:", 24, 46, 88, 17)
    $Combo1 = GUICtrlCreateCombo("", 112, 44, 49, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
    GUICtrlSetData(-1, "00|01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19|20|21|22|23", "16")
    $Label3 = GUICtrlCreateLabel("时", 165, 46, 16, 17)
    $Combo2 = GUICtrlCreateCombo("", 188, 44, 49, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
    GUICtrlSetData(-1, "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", "00")
    $Label4 = GUICtrlCreateLabel("分",242, 46, 16, 17)
    GUICtrlCreateGroup("",  -99, -99, 1, 1)
    $Button1 = GUICtrlCreateButton("保   存", 160, 96, 97, 33)
    GUICtrlSetOnEvent($Button1, "_form1")

;========创建计时窗口
       $Form2 = GUICreate("关机倒计时", 467, 211, -1, -1,$ws_caption);无控制按钮窗口
           $Label22 = GUICtrlCreateLabel("30",120,32,30,25)
       GUICtrlSetFont(-1, 18, 400, 0, "楷体_GB2312")
       $Label21 = GUICtrlCreateLabel(" 秒后将关闭电脑!", 148, 32, 260, 25)
       GUICtrlSetFont(-1, 18, 400, 0, "楷体_GB2312")
      $Progress21 = GUICtrlCreateProgress(32, 88, 385, 22)
      $Button21 = GUICtrlCreateButton("立即关闭", 112, 141, 113, 28)
     GUICtrlSetOnEvent($Button21, "_form2")
     $Button22 = GUICtrlCreateButton("取消(&X)", 264, 141, 113, 28)
     GUICtrlSetOnEvent($Button22, "_form2")

;======托盘
$cssj = TrayCreateItem("重新设定时间")
TrayItemSetOnEvent($cssj, "traymsg")
TrayCreateItem("")
$quit = TrayCreateItem("退  出")
TrayItemSetOnEvent($quit, "traymsg")
TraySetState()



;=========检查配置文件是否存在
 If Not FileExists(@MyDocumentsDir & "\shutdown.ini") Then
         
        
         GUISetState(@SW_SHOW,$Form1)
        
 EndIf
  Global $h_hour=IniRead(@MyDocumentsDir& "\shutdown.ini" ,"timer","HH","23")
        Global $m_min = IniRead(@MyDocumentsDir &"\shutdown.ini" ,"timer","MM","59")
While 1
     checktime()
    Sleep(1000)
WEnd



 
Func checktime()
        If ( @HOUR = $h_hour And @MIN = $m_min ) Then
                 GUISetState(@SW_SHOW,$Form2)
                WinSetOnTop("关机倒计时","",1)
                Global $time = 30
        
        AdlibRegister("_timer", 1000)
                $timer1 = _Timer_Init()
        While 1
                        $nMsg = GUIGetMsg()
            Switch $nMsg
                Case $GUI_EVENT_CLOSE, $Button22
                       AdlibUnRegister("_timer")
                                           GUIDelete($Form2)
                         
                Case $Button21
                        main()
            EndSwitch
            If $time <= 0 Then
                AdlibUnRegister("_timer")
                                
                main()
            EndIf
        ;If _Timer_GetIdleTime() <= _Timer_Diff($timer1) Then AdlibUnRegister("_timer") ;检测键盘鼠标是否移动
        WEnd
    EndIf
EndFunc



Func _timer()  ;计时窗口的秒数和进度条
        $time -= 1
        GUICtrlSetData($Label22, $time )
        GUICtrlSetData($Progress21,100- $time*100 / 30)
        If $time <= 0 Then AdlibUnRegister("_timer")
EndFunc   

Func main()  ;关机
       
                shutdown(4+8)
                Exit
EndFunc   ;==>main

Func traymsg() ;托盘事件
        
         Switch @TRAY_ID
                Case $cssj  ;重设时间
                                        If WinGetState("关机倒计时") = 7 Then
                                AdlibUnRegister("_timer")
                                                GUISetState(@SW_HIDE,$Form2)
                                        EndIf
                        
                        GUISetState(@SW_SHOW,$Form1)
                                        
                Case $quit  ;退出
                     If WinGetState("关机倒计时") = 7 Then
                                AdlibUnRegister("_timer")
                                        EndIf
                              Exit
                         EndSwitch
 EndFunc

Func _form1() ;设置窗口的事件
        While 1 ;窗体状态控制
       ; $nMsg = GUIGetMsg()
        Switch @GUI_CtrlId
                 Case $GUI_EVENT_CLOSE
                      GUISetState(@SW_HIDE,$Form1)
                Case $Button1           ;保存
                     $h_hour = GUICtrlRead ($Combo1)
                                 $m_min = GUICtrlRead ($Combo2)        
                                     IniWrite(@MyDocumentsDir & "\shutdown.ini" ,"timer","HH",$h_hour)
                                     IniWrite(@MyDocumentsDir & "\shutdown.ini" ,"timer","MM",$m_min)
                                     GUISetState(@SW_HIDE,$Form1)
          
        EndSwitch
         ExitLoop                         
    WEnd
EndFunc

Func _form2() ;计时窗口的事件
    While 1 ;窗体状态控制
        ;$nMsg = GUIGetMsg()
        Switch @GUI_CtrlId
                
                Case $Button21  ;立即关闭
                     main()
                 Case $Button22 ;取消
                     AdlibUnRegister("_timer")
                    GUIDelete($Form2)        
      
                         EndSwitch
                         ExitLoop
                                
    WEnd
     
  EndFunc

           
        
发表于 2014-4-5 06:25:43 | 显示全部楼层
不会,帮你顶,等高手来解答。。
发表于 2014-4-5 18:25:06 | 显示全部楼层
看了两三分钟,没能看完。是东拼西凑的代码?编码风格前后不一致啊。

发现两个问题。
1,为了迅速响应程序,主循环里的延迟应该小于100。
2,程序起始 Opt("GUIOnEventMode", 1),但检测定时函数里为什么又直接 GUIGetMsg()??
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-5 20:49 , Processed in 0.079156 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表