Jump to content

Countdown timer


kiendp
 Share

Recommended Posts

Hi, I'm a newbie in autoIT. I'm trying to create a countdown timer and get some problems.

First, I'm finding for a way to pause count script when press "Pause". I tried several ways but nothing had worked.

Second, I want to hide the main form to system tray. If the timer is pause, it works well, but when counting down, it only minimizes to taskbar.

Below is my script. Could anyone help me? Sorry for my English grammar

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)
Opt("TrayOnEventMode", 1)
Global $cIdTrayRestore
Global $cIdTrayExit = TrayCreateItem("Exit")
Opt("TrayMenuMode", 1)
Global $hWnd = GUICreate("Time to die", 215, 103, 4, 591)
Global $hour = GUICtrlCreateInput("00", 24, 8, 38, 40, BitOR($GUI_SS_DEFAULT_INPUT, $ES_CENTER, $ES_NUMBER))
GUICtrlSetFont(-1, 18, 800, 0, "Segoe UI Semibold")
GUICtrlSetColor(-1, 0x000000)
Global $min = GUICtrlCreateInput("00", 88, 8, 38, 40, BitOR($GUI_SS_DEFAULT_INPUT, $ES_CENTER, $ES_NUMBER))
GUICtrlSetFont(-1, 18, 800, 0, "Segoe UI Semibold")
GUICtrlSetColor(-1, 0x000000)
Global $second = GUICtrlCreateInput("00", 152, 8, 38, 40, BitOR($GUI_SS_DEFAULT_INPUT, $ES_CENTER, $ES_NUMBER))
GUICtrlSetFont(-1, 18, 800, 0, "Segoe UI Semibold")
GUICtrlSetColor(-1, 0x000000)
Global $btnRun = GUICtrlCreateButton("Run", 48, 64, 51, 25)
GUICtrlSetFont(-1, 11, 800, 0, "Segoe UI Semibold")
GUICtrlSetColor(-1, 0x000000)
Global $btnCancel = GUICtrlCreateButton("Cancel", 120, 64, 51, 25)
GUICtrlSetFont(-1, 11, 800, 0, "Segoe UI Semibold")
GUICtrlSetColor(-1, 0x000000)
GUICtrlCreateLabel(":", 72, 8, 10, 34)
GUICtrlSetFont(-1, 16, 800, 0, "Segoe UI")
GUICtrlSetColor(-1, 0x000000)
GUICtrlCreateLabel(":", 136, 8, 10, 34)
GUICtrlSetFont(-1, 16, 800, 0, "Segoe UI")
GUICtrlSetColor(-1, 0x000000)
GUISetState(@SW_SHOW)

#Region ======= thêm
GUISetOnEvent($GUI_EVENT_CLOSE, "exitGUI")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "hideToTray")
TrayItemSetOnEvent($cIdTrayExit, "exitGUI")
GUICtrlSetOnEvent($btnRun, "calcTime")
GUICtrlSetOnEvent($btnCancel, "resetGUI")
#EndRegion ======= thêm

While 1
    Global $countClick = 0
    If GUIGetMsg() = $btnRun Then $countClick += 1
    Sleep(10)
WEnd
Func calcTime()
    GUICtrlSetStyle($hour, BitOR($ES_READONLY, $ES_CENTER))
    GUICtrlSetStyle($min, BitOR($ES_READONLY, $ES_CENTER))
    GUICtrlSetStyle($second, BitOR($ES_READONLY, $ES_CENTER))
    Local $_hour = GUICtrlRead($hour), $_min = GUICtrlRead($min), $_sec = GUICtrlRead($second)
    If (($_hour > 24 Or $_hour == "") Or ($_min > 59 Or $_min == "") Or ($_sec > 59 Or $_sec == "")) Then
        MsgBox(16, "", "Wrong time format")
        GUICtrlSetStyle($hour, BitOR($GUI_SS_DEFAULT_INPUT, $ES_NUMBER, $ES_CENTER))
        GUICtrlSetStyle($min, BitOR($GUI_SS_DEFAULT_INPUT, $ES_NUMBER, $ES_CENTER))
        GUICtrlSetStyle($second, BitOR($GUI_SS_DEFAULT_INPUT, $ES_NUMBER, $ES_CENTER))
    Else
        GUICtrlSetData($btnRun, "Stop")
        Local $timeRun = ($_hour * 3600 + $_min * 60 + $_sec)
        While $timeRun > 0
            AdlibRegister("_GUIGetMsg", 40)
            $timeRun -= 1
            $_hour = Int($timeRun / 3600)
            $_min = Int($timeRun / 60 - $_hour * 60)
            $_sec = Int($timeRun - $_hour * 3600 - $_min * 60)
            GUICtrlSetData($hour, StringFormat("%02u", $_hour))
            GUICtrlSetData($min, StringFormat("%02u", $_min))
            GUICtrlSetData($second, StringFormat("%02u", $_sec))
            Sleep(990)
            If $countClick = 2 Then ExitLoop
            AdlibUnRegister("_GUIGetMsg")
        WEnd
        GUICtrlSetData($btnRun, "Run")
        If (GUICtrlRead($hour) = 0 And GUICtrlRead($min) = 0 And GUICtrlRead($second) = 0) Then
            MsgBox(64, "", "Time's out", 3)
            GUICtrlSetStyle($hour, BitOR($GUI_SS_DEFAULT_INPUT, $ES_NUMBER, $ES_CENTER))
            GUICtrlSetStyle($min, BitOR($GUI_SS_DEFAULT_INPUT, $ES_NUMBER, $ES_CENTER))
            GUICtrlSetStyle($second, BitOR($GUI_SS_DEFAULT_INPUT, $ES_NUMBER, $ES_CENTER))
        EndIf
    EndIf
EndFunc   ;==>calcTime
Func resetGUI()
    GUICtrlSetData($hour, "00")
    GUICtrlSetData($min, "00")
    GUICtrlSetData($second, "00")
EndFunc   ;==>resetGUI
Func hideToTray()
    $cIdTrayRestore = TrayCreateItem("Restore")
    TrayItemSetOnEvent($cIdTrayRestore, "restoreGUI")
    WinSetState($hWnd, "", @SW_HIDE)
EndFunc   ;==>hideToTray
Func exitGUI()
    Exit
EndFunc   ;==>exitGUI
Func restoreGUI()
;~  If $cIdTrayRestore <> 0 Then
    WinSetState($hWnd, "", @SW_RESTORE)
    TrayItemDelete($cIdTrayRestore)
    $cIdTrayRestore = 0
;~  EndIf
EndFunc   ;==>restoreGUI
Func _GUIGetMsg()
    GUISetOnEvent($GUI_EVENT_CLOSE, "exitGUI")
    GUISetOnEvent($GUI_EVENT_MINIMIZE, "hideToTray")
    TrayItemSetOnEvent($cIdTrayExit, "exitGUI")
EndFunc   ;==>_GUIGetMsg

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...