Using Tix_Now and friends as a stopwatch?

Started by Quin, May 08, 2025, 07:50:52 PM

Previous topic - Next topic

MrBcx

#2
I'm guessing my edited version below is a bit closer to what you're going for.

I had to change the gui control arrangement because I am not blind and I needed
to see what was being displayed.   ;)

Code that I've added include a trailing comment: ' Added by MrBcx

HTH


$No_Borland
$Noini
$No_LCCWin
$No_VKKeys
Gui "SwatchWndClass", Pixels

Enum
    IDC_START_PAUSE = 101
    IDC_RESET
    IDC_TIME_LABEL
    IDC_TIME_FIELD
    IDT_UPDATE_TIMER = 200
End Enum

Global As Control MainForm, StartPauseButton, ResetButton, TimeLabel, TimeField
Global Running As Bool
Global Elapsed#, Start#
Global tix_started As Bool   ' Added by MrBcx

Sub Formload
    MainForm = Bcx_Form("Swatch", 0, 0, 400, 200)
    StartPauseButton = Bcx_Button("&Start", MainForm, IDC_START_PAUSE, 5, 5, 50, 50)
    ResetButton = Bcx_Button("&Reset", MainForm, IDC_RESET, 5, 100, 50, 50)
    EnableWindow(ResetButton, False)
    TimeLabel = Bcx_Label("Time &elapsed", MainForm, IDC_TIME_LABEL, 100, 5, 230, 30)
    TimeField = Bcx_Input(GetElapsed$(), MainForm, IDC_TIME_FIELD, 100, 35, 230, 40, WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_LEFT | ES_READONLY)
    Center(MainForm)
    Show(MainForm)
    SetFocus(StartPauseButton)
End Sub



Begin Events
    Select Case Cbmsg
        Case WM_SETFOCUS
        Global As HWND hPrevFocus, hFocusedCtrl
        If hPrevFocus Then
            SetFocus(hPrevFocus)
            hPrevFocus = 0
        End If
        Exit Function
        Case WM_KILLFOCUS
        hFocusedCtrl = GetFocus()
        Exit Function
        Case WM_ACTIVATEAPP
        If Cbwparam = 0 Then
            hFocusedCtrl = GetFocus()
        Else
            If hFocusedCtrl Then
                hPrevFocus = hFocusedCtrl
                SetFocus(hFocusedCtrl)
                hFocusedCtrl = NULL
            End If
        End If
        Exit Function
        Case WM_QUIT, WM_CLOSE
        PostQuitMessage(0)
        Exit Function

        Case WM_COMMAND
        Select Case Cbctl
            Case IDC_START_PAUSE
            If Running Then
                Elapsed# = Elapsed# + (Tix_Now - Start#)
                KillTimer(MainForm, IDT_UPDATE_TIMER)
                SetWindowText(StartPauseButton, "&Resume")
            Else
                If tix_started = False Then      ' Added by MrBcx
                    Tix_Start                    ' Added by MrBcx
                    tix_started = True           ' Added by MrBcx
                End If                           ' Added by MrBcx

                Start# = Tix_Now
                SetWindowText(StartPauseButton, "&Pause")
                SetTimer(MainForm, IDT_UPDATE_TIMER, 1000, NULL)
            End If
            Running = Not Running
            EnableWindow(ResetButton, True)


            Case IDC_RESET
            Elapsed# = 0
            KillTimer(MainForm, IDT_UPDATE_TIMER)     ' Added by MrBcx
            EnableWindow(ResetButton, False)          ' Added by MrBcx
            SetWindowText(StartPauseButton, "&Start")
            SetWindowText(TimeField, "")              ' Added by MrBcx
            UpdateWindow(TimeField)                   ' Added by MrBcx
            SetFocus(StartPauseButton)
            Running = False
        End Select

        Case WM_TIMER
        If Running Then
            'MrBcx says --  Do not change Elapsed# here!
            SetWindowText(TimeField, GetElapsed$())
        End If

    End Select
End Events


Function GetElapsed$()
    Local Total#, Seconds, Minutes, Hours, Days, Res$
    If Running Then
        Total# = Elapsed# + (Tix_Now - Start#)
    Else
        Total# = Elapsed#
    End If
    If Total# < 1000 Then Function = "0 seconds"
    Seconds = Total# / 1000
    Minutes = Seconds / 60
    Seconds = Seconds % 60
    Hours = Minutes / 60
    Minutes = Minutes % 60
    Days = Hours / 24
    Hours = Hours % 24
    Days = Round(Days, 2)
    If Days > 0 Then Res$ += Str$(Days, 1) + " " + Iif$(Days = 1, "day", "days") + ", "
    If Hours > 0 Then Res$ += Str$(Hours , 1) + " " + Iif$(Hours = 1, "hour", "hours") + ", "
    If Minutes > 0 Then Res$ += Str$(Minutes, 1) + " " + Iif$(Minutes = 1, "minute", "minutes") + ", "
    If Seconds > 0 Then Res$ += Str$(Seconds, 1) + " " + Iif$(Seconds = 1, "second", "seconds") + ", "
    Function = Left$(Res$, Len(Res$) - 2)
End Function


Quin

I have this little GUI stopwatch program I was hoping to publish to the user contributions section once its done. It almost fully works, accept when trying to resume from a paused state, the timer resets. My brain is telling me this should be stupid simple, but all 4 people from my house are going to 4 different places, two of them being in two different countries, from the 6th to the 10th, so I'm more than a little scatterbrained to say the least and not doing my best coding between airport runs, packing and dealing with emotional family members.
$No_Borland
$Noini
$No_LCCWin
$No_VKKeys
Gui "SwatchWndClass", Pixels

Enum
    IDC_START_PAUSE = 101
    IDC_RESET
    IDC_TIME_LABEL
    IDC_TIME_FIELD
    IDT_UPDATE_TIMER = 200
End Enum

Global As Control MainForm, StartPauseButton, ResetButton, TimeLabel, TimeField
Global Running As Bool
Global Elapsed#, Start#

Sub Formload
    MainForm = Bcx_Form("Swatch")
    StartPauseButton = Bcx_Button("&Start", MainForm, IDC_START_PAUSE, 5, 5, 50, 50)
    ResetButton = Bcx_Button("&Reset", MainForm, IDC_RESET, 5, 100, 50, 50)
    EnableWindow(ResetButton, False)
    TimeLabel = Bcx_Label("Time &elapsed", MainForm, IDC_TIME_LABEL, 200, 5, 30, 5)
    TimeField = Bcx_Input(GetElapsed$(), MainForm, IDC_TIME_FIELD, 200, 15, 200, 40, WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_LEFT | ES_READONLY)
    Center(MainForm)
    Show(MainForm)
    SetFocus(StartPauseButton)
End Sub

Begin Events
    Select Case Cbmsg
        Case WM_SETFOCUS
        Global As HWND hPrevFocus, hFocusedCtrl
        If hPrevFocus Then
            SetFocus(hPrevFocus)
            hPrevFocus = 0
        End If
        Exit Function
        Case WM_KILLFOCUS
        hFocusedCtrl = GetFocus()
        Exit Function
        Case WM_ACTIVATEAPP
        If Cbwparam = 0 Then
            hFocusedCtrl = GetFocus()
        Else
            If hFocusedCtrl Then
                hPrevFocus = hFocusedCtrl
                SetFocus(hFocusedCtrl)
                hFocusedCtrl = NULL
            End If
        End If
        Exit Function
        Case WM_QUIT, WM_CLOSE
        PostQuitMessage(0)
        Exit Function
        Case WM_COMMAND
        Select Case Cbctl
            Case IDC_START_PAUSE
            If Running Then
                Elapsed# = Elapsed# + (Tix_Now - Start#)
                KillTimer(MainForm, IDT_UPDATE_TIMER)
                SetWindowText(StartPauseButton, "&Resume")
            Else
                Tix_Start
                Start# = Tix_Now
                SetWindowText(StartPauseButton, "&Pause")
                SetTimer(MainForm, IDT_UPDATE_TIMER, 1000, NULL)
            End If
            Running = Not Running
            EnableWindow(ResetButton, True)
            Case IDC_RESET
            Elapsed# = 0
            EnableWindow(ResetButton, False)
            SetWindowText(StartPauseButton, "&Start")
            SetFocus(StartPauseButton)
        End Select
        Case WM_TIMER
        If Running Then
            Elapsed# = Tix_Now
            SetWindowText(TimeField, GetElapsed$())
        End If
    End Select
End Events

Function GetElapsed$()
    Local Total#, Seconds, Minutes, Hours, Days, Res$
    If Running Then
        Total# = Elapsed# + (Tix_Now - Start#)
    Else
        Total# = Elapsed#
    End If
    If Total# < 1000 Then Function = "0 seconds"
    Seconds = Total# / 1000
    Minutes = Seconds / 60
    Seconds = Seconds % 60
    Hours = Minutes / 60
    Minutes = Minutes % 60
    Days = Hours / 24
    Hours = Hours % 24
    Days = Round(Days, 2)
    If Days > 0 Then Res$ += Str$(Days, 1) + " " + Iif$(Days = 1, "day", "days") + ", "
    If Hours > 0 Then Res$ += Str$(Hours , 1) + " " + Iif$(Hours = 1, "hour", "hours") + ", "
    If Minutes > 0 Then Res$ += Str$(Minutes, 1) + " " + Iif$(Minutes = 1, "minute", "minutes") + ", "
    If Seconds > 0 Then Res$ += Str$(Seconds, 1) + " " + Iif$(Seconds = 1, "second", "seconds") + ", "
    Function = Left$(Res$, Len(Res$) - 2)
End Function