DarkFingers1337 Posted April 14, 2017 Share Posted April 14, 2017 Hi, I wanted to share my very simple but quite handy stopwatch that I use every day for all sorts of stuff. Screen: Features: Pause function Reset function Multiple instances possible Code: Spoiler expandcollapse popup#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 Xandy, Skysnake and robertocm 3 Link to comment Share on other sites More sharing options...
spudw2k Posted April 14, 2017 Share Posted April 14, 2017 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. Spoiler Things I've Made: Always On Top Tool ◊ AU History ◊ Deck of Cards ◊ HideIt ◊ ICU ◊ Icon Freezer ◊ Ipod Ejector ◊ Junos Configuration Explorer ◊ Link Downloader ◊ MD5 Folder Enumerator ◊ PassGen ◊ Ping Tool ◊ Quick NIC ◊ Read OCR ◊ RemoteIT ◊ SchTasksGui ◊ SpyCam ◊ System Scan Report Tool ◊ System UpTime ◊ Transparency Machine ◊ VMWare ESX Builder Misc Code Snippets: ADODB Example ◊ CheckHover ◊ Detect SafeMode ◊ DynEnumArray ◊ GetNetStatData ◊ HashArray ◊ IsBetweenDates ◊ Local Admins ◊ Make Choice ◊ Recursive File List ◊ Remove Sizebox Style ◊ Retrieve PNPDeviceID ◊ Retrieve SysListView32 Contents ◊ Set IE Homepage ◊ Tickle Expired Password ◊ Transpose Array Projects: Drive Space Usage GUI ◊ LEDkIT ◊ Plasma_kIt ◊ Scan Engine Builder ◊ SpeeDBurner ◊ SubnetCalc Cool Stuff: AutoItObject UDF ◊ Extract Icon From Proc ◊ GuiCtrlFontRotate ◊ Hex Edit Funcs ◊ Run binary ◊ Service_UDF Link to comment Share on other sites More sharing options...
DarkFingers1337 Posted April 14, 2017 Author Share Posted April 14, 2017 Wow, somebody actually replied! The reset button change makes a lot of sense and will be added. I didn't encounter any issues with redeclaring globals inside functions yet but I will keep that in mind. Thanks for your feedback! Was hoping someone would chime in. Link to comment Share on other sites More sharing options...
Gianni Posted April 14, 2017 Share Posted April 14, 2017 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". Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt.... Link to comment Share on other sites More sharing options...
DarkFingers1337 Posted April 14, 2017 Author Share Posted April 14, 2017 (edited) 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 April 14, 2017 by DarkFingers1337 Link to comment Share on other sites More sharing options...
Skysnake Posted April 23, 2020 Share Posted April 23, 2020 @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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now