Page 2 of 2

Re: "uptime" for Windows

Posted: Fri Mar 14, 2025 11:47 am
by Axolotl
:D

Newly Revised "uptime" for Windows

Posted: Fri Aug 29, 2025 9:01 pm
by Randy Walker
I added the ability to perform a reboot or shutdown with optional countdown.

Code: Select all

Result = ExamineDesktops()
_x = DesktopWidth(0)/2
_y = DesktopHeight(0)/2
;- Enumerations / DataSections
; Windows
Enumeration
  #Window_0
EndEnumeration
;
; Gadgets
Enumeration
  #Instructions
  #Reboot
  #PowerDown
  #StringIN
  #Start
  #CANCEL
EndEnumeration
;
Global Type$, parameters$, s$
Define.l Event, EventWindow, EventGadget, EventType, EventMenu
Import "" ; --- from Kernel32.dll ---
  GetTickCount64()  ; Modified (3/13/2035) to include this Import sugested by Azolotl
  ;https://www.purebasic.fr/english/viewtopic.php?p=637424#p637424
EndImport 
trigger.q = (GetTickCount64() & $FFFFFFFFFFFFFFFF)/1000
hours.d = trigger / 3600  ;193.92388886213303
totalSeconds.d = hours * 3600
uptm$ = StrD(totalSeconds)  
days.d = Int(totalSeconds / 86400)
remainingSeconds.d = totalSeconds - (days * 86400)
hours.d = Int(remainingSeconds / 3600)
remainingSeconds.d = remainingSeconds - (hours * 3600)
minutes.d = Int(remainingSeconds / 60)
seconds.d = Round(remainingSeconds - (minutes * 60), #PB_Round_Nearest)
date.q = Date()
Boot.q = date-trigger
START$ = FormatDate("%hh:%ii:%ss   %mm:%dd:%yyyy",Boot)
s$ = uptm$ + " Total seconds   "+Chr(10)+StrD(days) + " days   "+Chr(10) +StrD(hours) + " hours   "+Chr(10) +StrD(minutes) + " Minutes   "+Chr(10) +StrD(seconds)+ " seconds"+Chr(10)+Chr(10)+"Last boot at:"+Chr(10)+START$+Chr(10)+" ---------------------------------------------------"
;
Procedure OpenWindow_Window_0()
  If OpenWindow(#Window_0, _x, _y, 190, 292, "Shutdown/Countdown", #PB_Window_SystemMenu | #PB_Window_ScreenCentered |#PB_Window_SizeGadget|#PB_Window_TitleBar)
    If LoadFont(1, "Arial", 10)
      ;SetGadgetFont(#PB_Default, FontID(1))
      TextGadget(#Instructions, 10, 15, 160, 180, s$+Chr(10)+"Select action and give hh:mm:ss for your countdown")
      OptionGadget(#Reboot, 15, 195, 80, 20, "Reboot")
      OptionGadget(#PowerDown, 15, 215, 80, 15, "PowerDown")
      StringGadget(#StringIN, 10, 230, 85, 25, "00:00:00")
      ButtonGadget(#Start, 10, 260, 85, 20, "START")
      ButtonGadget(#CANCEL, 95, 260, 90, 20, "CANCEL")
      ;Set Reboot as Default selection:
      SetGadgetState(#Reboot, 1)
      SetActiveGadget(#Reboot)
      Type$ = "Reboot"
      parameters$ = "/r /t "
    EndIf
  EndIf
EndProcedure

OpenWindow_Window_0()
SetActiveGadget(#Start)
;- Event loop
Repeat
  Event = WaitWindowEvent()
  delay$ = GetGadgetText(#StringIN)
  hh = Val(StringField(delay$,1,":"))
  mm = Val(StringField(delay$,2,":"))
  ss = Val(StringField(delay$,3,":"))
  delay = hh*3600 + mm*60 + ss
  s$ = Str(delay)
  EventType = EventGadget()
  EventGadget = EventGadget()
  Select Event
    Case #WM_CHAR
      key = EventwParam()
      Select key
        Case 27 ;ESCAPE
          Debug "ESCAPE key pressed"
          Event = #PB_Event_CloseWindow
      EndSelect
      ; ///////////////////
    Case #PB_Event_Gadget
      Select EventType
        Case #Reboot
          Debug "hit Reboot"
          Type$ = "Reboot"
          parameters$ = "/r /t "
        Case #PowerDown
          Type$ = "PowerDown"
          parameters$ = "/s /t "
        Case #Start
          Debug "hit Start"
          parameters$ +s$
          Debug parameters$
          If Type$ <> ""
            Select MessageRequester("   Confirm "+Type$+" Time","System will "+Type$+" after " +s$+ " seconds", #MB_OKCANCEL|#MB_SYSTEMMODAL)
              Case #IDOK
                Exe=RunProgram("shutdown",parameters$,"C:\Windows\System32",#PB_Program_Hide|#PB_Program_Open|#PB_Program_Read|#PB_Program_Error)
                Event = #PB_Event_CloseWindow
              Case #IDCANCEL
                Event = #PB_Event_CloseWindow
            EndSelect
          EndIf
        Case #CANCEL
          Debug "hit Cancel"
          Event = #PB_Event_CloseWindow
      EndSelect
      ; ////////////////////////
  EndSelect
Until Event = #PB_Event_CloseWindow
End