PayCalc: Difference between revisions

From AutoIt Wiki
Jump to navigation Jump to search
(Initial version)
 
m (+Category:Samples)
 
Line 1: Line 1:
[[Category:Samples]]
<b>Script:</b> payCalc
<b>Script:</b> payCalc



Latest revision as of 14:38, 14 November 2012

Script: payCalc

Author: Jon Dunham

Description: Simple GUI example to calculate hourly pay in real-time.

Notes: "my code is a dog's code" - R. Beef Kazenzakis


#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

#region GUI setup
; create main window/form/frame
$formPC = GUICreate("payCalc", 251, 136, 346, 590)

; create labels. it's generally not necessary to set the returned handle
;  to a variable on static label controls you know you won't be interacting with
GUICtrlCreateLabel("Time:", 8, 8, 30, 17)
GUICtrlCreateLabel("Total pay:", 8, 32, 51, 17)
; create read-only status boxes
$tTime = GUICtrlCreateInput("", 72, 8, 121, 21, BitOR($ES_AUTOHSCROLL,$ES_READONLY))
$tTotal = GUICtrlCreateInput("", 72, 32, 121, 21, BitOR($ES_AUTOHSCROLL,$ES_READONLY))

; create buttons. use $BS_DEFPUSHBUTTON constant on Start to make it the default button for when {ENTER} is pressed.
;  the ampersand character specifies the alt-key shortcut to use
$btnStart = GUICtrlCreateButton("&Start", 8, 104, 75, 25, $BS_DEFPUSHBUTTON)
$btnStop = GUICtrlCreateButton("S&top", 88, 104, 75, 25)
$btnReset = GUICtrlCreateButton("&Reset", 168, 104, 75, 25)

; rate label and input controls
GUICtrlCreateLabel("Rate ($/hr):", 8, 72, 59, 17)
$inRate = GUICtrlCreateInput("0.00", 72, 72, 57, 21)

; finally, show the GUI
GUISetState(@SW_SHOW)
#EndRegion

; init global var to keep track of the last timestamp if "Stop" is clicked
Global $lastTime = 0

While 1
	; main/idle GUI switch statement
	Switch GUIGetMsg()
	Case $GUI_EVENT_CLOSE
		GUIDelete()
		Exit
	Case $btnStart
		; enter calc function when Start is clicked
		payCalc()
	Case $btnReset
		; zero out gui controls and whatnot
		GUICtrlSetData($tTime, "")
		GUICtrlSetData($tTotal, "")
		$lastTime = 0
	EndSwitch
	
	Sleep(10)
WEnd

Func payCalc()
	; start the timer
	$timer = TimerInit()
	; get the rate of pay to multiply based on the update interval, which in this case is 100 milliseconds or 1/10th second
	$calcRate = Number(GUICtrlRead($inRate))/60/600
	
	While 1
		; check GUI for events
		Switch GUIGetMsg()
		Case $GUI_EVENT_CLOSE
			GUIDelete()
			Exit
		Case $btnStop
			; when Stop button is clicked, set that global lastTime var to the current timestamp plus itself (+=) if we return to this function
			$lastTime += TimerDiff($timer)
			WinSetTitle($formPC, "", "payCalc")
			; return to main loop
			Return
		EndSwitch
		
		; update the time edit box, adding a millisecond display 'cause it's fun to watch numbers... i guess
		GUICtrlSetData($tTime, timeFormat(TimerDiff($timer)+$lastTime) & " [" & Round(Mod(TimerDiff($timer), 1000), 0) & "]")

		; multiply the adjusted rate by how much time has passed in ms/10 and set edit control, window title and traytip
		$totalPay = Round($calcRate * ((TimerDiff($timer)+$lastTime)/100), 2)
		WinSetTitle($formPC, "", "$" & $totalPay & " [payCalc]")
		TraySetToolTip("$" & $totalPay & @CRLF & timeFormat(TimerDiff($timer)+$lastTime))
		GUICtrlSetData($tTotal, "$" & $totalPay)
		
		; sleep before looping
		Sleep(100)
	WEnd
EndFunc

func timeFormat($ms, $sepChar = ":")
	; custom function to make a nice HH:MM:SS display from a given time in milliseconds
	dim $sec, $min, $hr
	dim $s = round($ms/1000, 0 )

	$sec = mod($s, 60)
	if $sec < 10 then $sec = "0" & $sec
	$min = Floor(mod($s/60, 60))
	if $min < 10 then $min = "0" & $min
	$hr = Floor($s/3600)
	if $hr < 10 then $hr = "0" & $hr

	Return $hr & $sepChar & $min & $sepChar & $sec
EndFunc ; <== timeFormat