Stopwatch problem Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Pilu
Posts: 84
Joined: 22 Jun 2018, 01:13

Stopwatch problem

20 May 2020, 04:58

Hi,

I try to crate a stopwatch, but the i have a problem with the FormatTime. if i set hh:mm:ss and start the stopwatch th hour start from 12.

Code: Select all

#NoEnv
Gui, -Caption
Gui, +Sysmenu +ToolWindow
SetBatchLines, -1
SetWinDelay, -1

ElapsedTime := A_YYYY                                 ; initialize ElapsedTime, it will be treated as YYYY0101000000
BalanceTime := 0  
Gui, +AlwaysOnTop +HwndGuiHwnd
Gui, Add, Picture, x0 y5 w160 h525, Buttons\bg.png






;Gui, Font, cff3366
Gui, Font, c00ffff



Gui, Add , text, x5 y30 +BackgroundTrans,BÁRKÓD:
;Gui, Add , text, x120 y30 +BackgroundTrans Hidden vREISS,REIS
Gui, Add , DDL, x5 y50 +BackgroundTrans, ATTO|CHAP|VALAMIs
Gui, Add, Picture, x0 y70 +BackgroundTrans, Buttons\v.png
Gui, Add , text, x5 y90 +BackgroundTrans, FOLYAMAT
Gui, Add, DDL, x5 y110 +BackgroundTrans, VALAMI|EZT CSINálom|BLABLA
Gui, Add, Picture, x0 y130 +BackgroundTrans, Buttons\v.png
Gui, Add , text, x5 y150 +BackgroundTrans, Perc
Gui, Add, Edit, x5 y170 +BackgroundTrans,
Gui, Add, Picture, x0 y190 +BackgroundTrans, Buttons\v.png
;Gui, Add , text, x5 y125 +BackgroundTrans Hidden gCZTO vCZTO,CZTO
Gui, Font, c00ffff
Gui, Font, S16 Bold, 
Gui, Add , text, x5 y220 +BackgroundTrans vDisplayTime, 00:00:00
Gui, Font,,
Gui, Font, Bold, 
Gui, Add, Button, x5 y250 w50 hp vBtnStartStop gStartStop Default, Start ;`nStop
Gui, Add, Button, x+0 y250 w50 hp vBtnReset gReset, Reset
Gui, Add, Button, x+0 y250 w50 hp vBtnRögzít gRögzít, Rögzít




Gui, Show, x0 y-5 w160 h525
Gosub, StartStop
;SetTimer, PressTheKey, 60000
SetTimer, check, 10
MouseMove, 10, 10 
Return



check:
SetTimer, check, Off
WinGet, hwnd, ID, A
WinGetPos,x,y,w,h,ahk_id %GuiHwnd%
MouseGetPos,mx,my
if (mx <= 0 && my <= y+h)
	gosub ShowWindow
if (hwnd <> GuiHwnd or ((mx > w) or (my > y+h))) {
	gosub HideWindow
}   
SetTimer, check, 10
return

HideWindow:
WinGetPos,x,y,w,h,ahk_id %GuiHwnd%
while (x+w > 0 ) {
	WinMove, ahk_id %GuiHwnd%,,% x-10
	WinGetPos,x,y,w,h,ahk_id %GuiHwnd%
	Sleep, 10
}
return

ShowWindow:
WinGetPos,x,y,w,h,ahk_id %GuiHwnd%
while (x < 0 ) {
	WinMove, ahk_id %GuiHwnd%,,% x+10
	WinGetPos,x,y,w,h,ahk_id %GuiHwnd%
	Sleep, 10
}
WinActivate, ahk_id %GuiHwnd%,
return


StartStop:
GuiControlGet, BtnStartStop                        ; get the caption of the button
If (BtnStartStop = "Start"){ ;`nStop") {                ; if it is "Start`nStop"
	StartTickCount := A_TickCount - BalanceTime     ; initialize StartTickCount
	SetTimer, StopWatch, 10                         ; start the timer called every 10 ms to be accurate
	GuiControl, , BtnStartStop, Stop                ; set the caption to Stop
} Else {                                           ; it should be "Stop"
	BalanceTime := A_TickCount - StartTickCount     ; memorize the balance time not counted yet
	SetTimer, StopWatch, Off                        ; stop the timer
                       ; stop the timer
	GuiControl, , BtnStartStop, Start ;`nStop         ; set the caption to "Start`nStop"
}       ; set the caption to "Start`nStop"

Return
; BtnReset label
; BtnReset label
Reset:
SetTimer, StopWatch, Off                           ; stop the timer
GuiControl, , BtnStartStop, Start ;`nStop            ; set the caption of BtnStartStop to "Start`nStop"
GuiControl, , DisplayTime, 00:00:00                   ; initialize DisplayTime
ElapsedTime := A_YYYY                              ; initialize ElapsedTime, it will be treated as YYYY0101000000
BalanceTime := 0                                   ; initialize BalanceTime
GuiControl, Focus, BtnStartStop                    ; set the focus on BtnStartStop
GuiControl, -Default, BtnReset                     ; remove the BS_DEFPUSHBUTTON (default) style
GuiControl, +Default, BtnStartStop                 ; set the BS_DEFPUSHBUTTON (default) style
Return

StopWatch:
If ((A_TickCount - StartTickCount) >= 1000) {      ; if current tickcount - StartTickCount >= 1000 (i.e. 1 second)
	StartTickCount += 1000                          ; add 1000 ms (1 second) to StartTickCount
	ElapsedTime += 1, S                             ; add 1 second to ElapsedTime
	FormatTime, DisplayTime, %ElapsedTime%, hh:mm:ss ; format ElapsedTime to mm:ss
	GuiControl, , DisplayTime, %DisplayTime%        ; update DisplayTime
}
Return

Rögzít:
return



$1::

return
GuiEscape:
GuiClose:
ExitApp

Rohwedder
Posts: 7669
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Stopwatch problem  Topic is solved

20 May 2020, 09:00

Hallo,
only a try.
Replace hh:mm:ss by HH:mm:ss or mm:ss
User avatar
boiler
Posts: 17034
Joined: 21 Dec 2014, 02:44

Re: Stopwatch problem

20 May 2020, 09:26

You could just have the variable ElapsedTime contain the number of msec rather than trick it into containing a date and formatting that, then you could display the elapsed time using the function below:

Code: Select all

; demonstrate with example of 8 hrs, 5 min, and 23+ sec in msec:
ElapsedTime := 8 * 3600000 + 5 * 60000 + 23150
MsgBox, % ElapsedTimeDisplay(ElapsedTime)
return

ElapsedTimeDisplay(msec) {
	hr := msec // 3600000
	min := (msec - hr * 3600000) // 60000
	Sec := (msec - hr * 3600000 - min * 60000) // 1000
	return hr ":" (min < 10 ? "0" : "") . min ":" (sec < 10 ? "0" : "") . sec
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], jollyjoe and 319 guests