Jump to content

Simple Stopwatch


Recommended Posts

Hi,

I wanted to share my very simple but quite handy stopwatch that I use every day for all sorts of stuff.

Screen:

stopwatch_screenc253c.png

Features:

  • Pause function
  • Reset function
  • Multiple instances possible

 

Code:

Spoiler
#NoTrayIcon
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#AutoIt3Wrapper_Icon=D:\Bilder\icons\alarm.ico ;Icon is attached to forum post, change to correct path
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Stopwatch", 219, 87, 192, 124, -1, $WS_EX_TOPMOST)
$b_start = GUICtrlCreateButton("Start", 8, 56, 59, 25)
$b_pause = GUICtrlCreateButton("Pause", 80, 56, 59, 25)
$b_reset = GUICtrlCreateButton("Reset", 152, 56, 59, 25)
$l_timer = GUICtrlCreateLabel("00:00:00", 0, 16, 219, 33, $SS_CENTER)
GUICtrlSetFont(-1, 18, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x000000)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Global $state = "Idle", $timer, $render_timer, $saved_time = 0

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $b_start
            start_timer()
        Case $b_pause
            pause_timer()
        Case $b_reset
            reset_timer()
    EndSwitch
    If $state == "Running" And TimerDiff($render_timer) >= 40 Then renderTime()
WEnd

Func start_timer()
    If $state == "Idle" Then
        Global $timer = TimerInit(), $state = "Running", $render_timer = TimerInit()
    EndIf
EndFunc

Func pause_timer()
    If $state == "Running" Then
        $state = "Paused"
        $saved_time = TimerDiff($timer) + $saved_time
        GUICtrlSetData($b_pause, "Resume")
    ElseIf $state == "Paused" Then
        $state = "Running"
        $timer = TimerInit()
        GUICtrlSetData($b_pause, "Pause")
    EndIf
EndFunc

Func reset_timer()
    Global $timer = "", $state = "Idle", $render_timer = "", $saved_time = 0
    GUICtrlSetData($l_timer, "00:00:00")
    GUICtrlSetData($b_pause, "Pause")
EndFunc

Func renderTime()
    Local $diff = TimerDiff($timer) + $saved_time
    Local $sec = Int(Mod($diff/1000, 60))
    Local $min = Int(Mod($diff/60000, 60))
    Local $hour = Int($diff/3600000)
    If $sec < 10 Then $sec = "0"&$sec
    If $min < 10 Then $min = "0"&$min
    If $hour < 10 Then $hour = "0"&$hour
    GUICtrlSetData($l_timer, $hour&":"&$min&":"&$sec)
    $render_timer = TimerInit()
EndFunc

 

 

Enjoy!

alarm.ico

Link to comment
Share on other sites

Pretty cool.  I have a couple of recommendations, features to offer.

The reset button: it's been a while since I used a real stopwatch, but I recall that reset would zero the time, but not affect the running state of the stopwatch (think lap timer).  I guess my point is, IMHO, it is not necessary to stop the timer when the reset button is pressed.  (If reset button pressed while running, restart timer; if reset button pressed while stopped, zero time)

Lap timer: you can probably infer from my previous remark.  Perhaps, collect timer snapshots with each "lap" and display them all when watch is stopped.  Just an idea.

Milliseconds: It would be neat to see milliseconds included in the display.


Also, another comment.  In your reset and start timer function, you reset your globals by re-declaring them as globals.  That's not a very good practice from what I understand.  I can see that it is easier to set them on a single line rather than set one per line, but it can lead to issues. 
 

Anyways, just wanted to offer some things to think about and possible coding challenges.  

Link to comment
Share on other sites

Hi @DarkFingers1337, this stopwatch is written as a "finished product", nice toy :), anyway, you should keep separate the background logic and the application gui so that the "stopwatch" can be used and reused even in other contexts.
Here is a very similar stopwatch I wrote some time ago where the logic is written as a standalone function, and the gui is a separate "section" that make use of the reusable standalone "stopwatch engine".

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

Very cool example @Chimp, yes my stopwatch is intended to be a finished product. I have it running most of the time to measure work time etc. I actually have it running right now!

When I started working on this app, I just needed something quickly to measure time and as I wasn't satisfied with any stopwatches out there I decided to make my own. Cool Timer was a good app I used a lot but new versions ruined it for me.

Edited by DarkFingers1337
Link to comment
Share on other sites

  • 3 years later...

@Chimp and @DarkFingers1337 nice work. Very similar in my opinion.

I have been looking for a "plugin" timer like this I could use. Due to the design I opted for @Chimp's designed, but my Child Gui broke :(

I think a proper UDF can be written (a small one :))  that contains all these.  

Very inspiring. Thank you

 

Skysnake

Why is the snake in the sky?

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...