'Defines resources (graphics)
$RESOURCE COICO AS "COMPUTEROFF.ICO"
$RESOURCE StartP AS "START.BMP"
$RESOURCE StopP AS "STOP.BMP"
$RESOURCE MinP AS "MIN.BMP"
$RESOURCE CloseP AS "CLOS.BMP"
$RESOURCE WatchP As "TIME.BMP"
$RESOURCE SWatch As "STIME.BMP"
$RESOURCE QuesP As "QUES.BMP"
$RESOURCE BoxP As "BOX.BMP"
$RESOURCE LWatch As "TIMEL.BMP"
$RESOURCE MaxP As "MAX.BMP"
$RESOURCE MHB As "MHB.BMP"

'Declares the Windows shutdown function of USER32.DLL for use with Rapid Q
DECLARE FUNCTION ExitWindows LIB "USER32.DLL" ALIAS "ExitWindowsEx" (uFlags AS LONG, dwReserved AS LONG) AS LONG

'Declares the Windows function which loads files in the default viewer for use with Rapid Q
DECLARE SUB ShellExecute LIB "SHELL32" ALIAS "ShellExecuteA" (HWnd AS LONG, lpOperation AS STRING, lpFile AS STRING, lpParameters AS STRING, lpDirectory AS STRING, nShowCmd AS LONG)

'Allocates space for form and form components
DIM CurTime AS QLabel, Time1 As QLabel, Time2 As QLabel, Colon1 As QLabel, Colon2 As QLabel, Colon3 As QLabel, Colon4 As QLabel, HelpT As QLabel
DIM SecT AS QTimer, CurTim As QTimer
DIM Main AS QFORM
DIM SecD AS QEDIT, MinD AS QEDIT, HourD as QEDIT, SecAD AS QEDIT, MinAD AS QEDIT, HourAD as QEDIT
DIM CloseBut as QImage, MinBut as QImage, Button As QImage, MaxBut As QIMage, BoxBut As QImage, QuesBut As QImage, MHBUT as QImage
DIM Watch As QImage
DIM HelpE As QOvalBtn

SUB MHWeb  'MicroHof logo button
 MHBut.Transparent = 0
 ShellExecute 0, "Open", "http://www.public.iastate.edu/~microhof/", "", "", 1
END SUB

SUB DoneHelp
 Main.AutoScroll = 0
 CurTime.Visible = 1: Time1.Visible = 1: Time2.Visible = 1: Colon1.Visible = 1: Colon2.Visible = 1: Colon3.Visible = 1: Colon4.Visible = 1: HelpT.Visible = 0
 SecD.Visible = 1: MinD.Visible = 1: HourD.Visible = 1: SecAD.Visible = 1: MinAD.Visible = 1: HourAD.Visible = 1
 CloseBut.Visible = 1: MinBut.Visible = 1: Button.Visible = 1: MaxBut.Visible = 1: BoxBut.Visible = 1: QuesBut.Visible = 1: MHBUT.Visible = 1: Watch.Visible = 1: HelpE.Visible = 0
 Button.BMPHandle = StopP
END SUB

SUB Help  'Online help system button
 QuesBut.Transparent = 0
 If NOT Main.Height = 250 Then
  Main.Width = 250
  Main.Height = 250
  Watch.BMPHandle = WatchP
  Button.Top = 3: Button.Left = 140
  MHBut.Top = 226: MHBut.Left = 8
  CurTime.Top = 229: CurTime.Left = 150
  Main.Center
 End if
 CurTime.Visible = 0: Time1.Visible = 0: Time2.Visible = 0: Colon1.Visible = 0: Colon2.Visible = 0: Colon3.Visible = 0: Colon4.Visible = 0: HelpT.Visible = 1
 SecT.Enabled = 0: SecD.Visible = 0: MinD.Visible = 0: HourD.Visible = 0: SecAD.Visible = 0: MinAD.Visible = 0: HourAD.Visible = 0
 CloseBut.Visible = 0: MinBut.Visible = 0: Button.Visible = 0: MaxBut.Visible = 0: BoxBut.Visible = 0: QuesBut.Visible = 0: MHBUT.Visible = 0: Watch.Visible = 0: HelpE.Visible = 1
 Button.BMPHandle = StartP
 Main.AutoScroll = 1
 HelpE.Caption = "Exit Help": HelpE.Left = 65: HelpE.Top = 835: HelpE.Parent = Main: HelpE.OnClick = DoneHelp
 HelpT.Parent = Main: HelpT.Top = 0: HelpT.Left = 0: HelpT.Width = 200
 HelpT.Caption = " ComputerOff Help"+CHR$(13)
 HelpT.Caption = HelpT.Caption + " ----------------------------"+CHR$(13)
 HelpT.Caption = HelpT.Caption + " (You may exit help using the button below.)"+CHR$(13)+CHR$(13)
 HelpT.Caption = HelpT.Caption + " Purpose:"+CHR$(13)
 HelpT.Caption = HelpT.Caption + "    ComputerOff is a program designed to give"+CHR$(13)
 HelpT.Caption = HelpT.Caption + " computer users control over their computer that"+CHR$(13)
 HelpT.Caption = HelpT.Caption + " they've had over their TV for years.  You can"+CHR$(13)
 HelpT.Caption = HelpT.Caption + " now enter a time, click a button, and after"+CHR$(13)
 HelpT.Caption = HelpT.Caption + " that amount of time your computer will"+CHR$(13)
 HelpT.Caption = HelpT.Caption + " shutdown."+CHR$(13)+CHR$(13)
 HelpT.Caption = HelpT.Caption + " Basic Use:"+CHR$(13)
 HelpT.Caption = HelpT.Caption + "    The basic use of the program is as simple"+CHR$(13)
 HelpT.Caption = HelpT.Caption + " as the program concept.  Enter a time in the"+CHR$(13)
 HelpT.Caption = HelpT.Caption + " first set of text boxes.  The exact time"+CHR$(13)
 HelpT.Caption = HelpT.Caption + " shutdown will occur will be calculated and"+CHR$(13)
 HelpT.Caption = HelpT.Caption + " displayed in the second set of text boxes."+CHR$(13)
 HelpT.Caption = HelpT.Caption + " When you want to start the countdown, click on"+CHR$(13)
 HelpT.Caption = HelpT.Caption + " the red light located at the top right of the"+CHR$(13)
 HelpT.Caption = HelpT.Caption + " ComputerOff window.  The light will turn green"+CHR$(13)
 HelpT.Caption = HelpT.Caption + " and ComputerOff should begin the countdown."+CHR$(13)
 HelpT.Caption = HelpT.Caption + " When countdown is complete, Windows will"+CHR$(13)
 HelpT.Caption = HelpT.Caption + " shutdown.  If your computer has advanced"+CHR$(13)
 HelpT.Caption = HelpT.Caption + " power management (APM) it will power off"+CHR$(13)
 HelpT.Caption = HelpT.Caption + " after Windows has shutdown."+CHR$(13)+CHR$(13)
 HelpT.Caption = HelpT.Caption + " Other Features:"+CHR$(13)+CHR$(13)
 HelpT.Caption = HelpT.Caption + "    ComputerOff has a custom designed interface."+CHR$(13)
 HelpT.Caption = HelpT.Caption + " As such, it has many more buttons in the title"+CHR$(13)
 HelpT.Caption = HelpT.Caption + " bar than do most other programs.  A description"+CHR$(13)
 HelpT.Caption = HelpT.Caption + " of these buttons' functions can be found here."+CHR$(13)
 HelpT.Caption = HelpT.Caption + "    The first four buttons (from the right) are"+CHR$(13)
 HelpT.Caption = HelpT.Caption + " in the same order and have the same function"+CHR$(13)
 HelpT.Caption = HelpT.Caption + " as most other programs, they just look different."+CHR$(13)
 HelpT.Caption = HelpT.Caption + " The X button closes the program; the square"+CHR$(13)
 HelpT.Caption = HelpT.Caption + " restores the form; the dash minimizes the form"+CHR$(13)
 HelpT.Caption = HelpT.Caption + " (actually, it 'rolls' up); the question mark"+CHR$(13)
 HelpT.Caption = HelpT.Caption + " takes you to this screen."+CHR$(13)
 HelpT.Caption = HelpT.Caption + "    The next two buttons are unique to"+CHR$(13)
 HelpT.Caption = HelpT.Caption + " ComputerOff.  The square with the dot in the"+CHR$(13)
 HelpT.Caption = HelpT.Caption + " middle minimizes the form to a small square"+CHR$(13)
 HelpT.Caption = HelpT.Caption + " in the upper left corner of the screen.  To"+CHR$(13)
 HelpT.Caption = HelpT.Caption + " restore an ComputerOff form at any time,"+CHR$(13)
 HelpT.Caption = HelpT.Caption + " all you have to do is click on it.  The red/"+CHR$(13)
 HelpT.Caption = HelpT.Caption + " green button will start/stop the countdown."+CHR$(13)+CHR$(13)
 HelpT.Caption = HelpT.Caption + " For more help, go to:"+CHR$(13)
 HelpT.Caption = HelpT.Caption + "    http://microhof.hypermart.net"+CHR$(13)
 HelpT.Caption = HelpT.Caption + " Or, click on the logo in the lower left hand"+CHR$(13)
 HelpT.Caption = HelpT.Caption + " corner of the screen."+CHR$(13)+CHR$(13)
 HelpT.Caption = HelpT.Caption + " Or, email jgartin@mailandnews.com"+CHR$(13)+CHR$(13)
 HelpT.Caption = HelpT.Caption + " Oh, yeah.  You can't move the ComputerOff"+CHR$(13)
 HelpT.Caption = HelpT.Caption + " window around the screen, so stop trying."+CHR$(13)
 HelpT.Caption = HelpT.Caption + " I decided not to go to the trouble of "+CHR$(13)
 HelpT.Caption = HelpT.Caption + " programming the routines.  Also, the timer"+CHR$(13)
 HelpT.Caption = HelpT.Caption + " stops when you enter the help screen, so "+CHR$(13)
 HelpT.Caption = HelpT.Caption + " you don't have to worry about your computer"+CHR$(13)
 HelpT.Caption = HelpT.Caption + " suddenly shutting off right now.  Just don't"+CHR$(13)
 HelpT.Caption = HelpT.Caption + " forget to turn the timer back on when you're"+CHR$(13)
 HelpT.Caption = HelpT.Caption + " ready."
END SUB

SUB CloseSubNotMinimize  'Make the close button flash before exiting
 CloseBut.Transparent = 0
END SUB

SUB MinimizeSubNotClose  'Minimize button (resizes form to a window shade)
 MinBut.Transparent = 0
 If NOT Main.Height = 43 Then
  Main.Height = 43
  Watch.BMPHandle = LWatch
  CurTime.Top = 22
  MHBut.Top = 19
 End if
END SUB

SUB Box  'Other minimize button (resizes form to a 20x20 box)
 BoxBut.Transparent = 0
 If NOT Main.Height = 20 Then
  Main.Top = 0
  Main.Left = 0
  Main.Width = 20
  Main.Height = 20
  Watch.BMPHandle = SWatch
  Button.Top = 5: Button.Left = 5
 End if
END SUB

SUB Max  'Maximize button (restores form to 250x250)  Actually if you click anywhere on the form where there isn't a button it maximizes
 MaxBut.Transparent = 0
 If NOT Main.Height = 250 Then
  Main.Width = 250
  Main.Height = 250
  Watch.BMPHandle = WatchP
  Button.Top = 3: Button.Left = 140
  MHBut.Top = 226: MHBut.Left = 8
  CurTime.Top = 229: CurTime.Left = 150
  Main.Center
 End if
END SUB

SUB CountButton  'Red\green button that starts\stops the countdown
 Button.Transparent = 0
 IF SecT.Enabled = 1 THEN
  Button.BMPHandle = StopP  'Changes button graphic
  SecT.Enabled = 0
  EXIT SUB
 END IF
 IF SecT.Enabled = 0 THEN
   Button.BMPHandle = StartP  'Changes button graphic
  SecT.Enabled = 1
  EXIT SUB
 END IF
END SUB

SUB UPDTIM  'Updates the current time display and turns buttons back to normal after click  (Also closes form when close button has been pressed)
 TIMEVar$ = TIME$
 SecAD.Text = RIGHT$(TIMEVar$, Len(TIMEVar$)-Instr(Instr(TIMEVar$, ":")+1, TIMEVar$, ":"))
 HourAD.Text = LEFT$(TIMEVar$, Instr(TIMEVar$, ":")-1)
 MinAD.Text = MID$(TIMEVar$, Instr(TIMEVar$, ":")+1, Instr(Instr(TIMEVar$, ":")+1, TIMEVar$, ":")-(Instr(TIMEVar$, ":")+1))
 SecAD.Text = STR$(Val(SecAD.Text)+Val(SecD.Text))
 MinAD.Text = STR$(Val(MinAD.Text)+Val(MinD.Text))
 HourAD.Text = STR$(Val(HourAD.Text)+Val(HourD.Text))
 IF MinBut.Transparent = 0 Then
  MinBut.Transparent = 1
 End if
 IF MHBut.Transparent = 0 Then
  MHBut.Transparent = 1
 End if
 IF CloseBut.Transparent = 0 Then
  Main.Close  'Closes the form if close button was pressed
 End if
 IF Button.Transparent = 0 Then
  Button.Transparent = 1
 End if
 IF MaxBut.Transparent = 0 Then
  MaxBut.Transparent = 1
 End if
 IF BoxBut.Transparent = 0 Then
  BoxBut.Transparent = 1
 End if
 IF QuesBut.Transparent = 0 Then
  QuesBut.Transparent = 1
 End if
 CurTime.Caption = TIME$  'Updates the time display
END SUB

SUB Count  'Does countdown to shutdown and shuts down when timer reaches 0:0:0
 IF Val(SecD.Text) <= 0 AND Val(MinD.Text) <= 0 AND Val(HourD.Text) <= 0 AND SecT.Enabled = 1 THEN
  ExitWindows 1, 0   'Code that shuts down Windows
 END IF
 IF Val(SecD.Text) > 0 THEN  'Subtracts 1 from seconds every second
  SecT.Interval = 1000                        ssss
  SecD.Text = Str$(Val(SecD.Text) - 1)
 END IF
 IF VAL(MinD.Text) = 0 AND VAL(HourD.Text) > 0 THEN  'If minutes = 0 then add 60 to minutes and take one from hours
  HourD.Text = Str$(Val(HourD.Text) - 1)
  MinD.Text = Str$(Val(MinD.Text) + 60)
 END IF
 IF VAL(SecD.Text) = 0 AND VAL(MinD.Text) > 0 THEN  'If seconds = 0 then add 59 to seconds and take one from minutes
  MinD.Text = Str$(Val(MinD.Text) - 1)
  SecD.Text = Str$(Val(SecD.Text) + 59)
 END IF
END SUB

'Form, button, image, and other component definitions
Main.CAPTION = "ComputerOff": Main.ICOHandle = COICO: Main.WIDTH = 250: Main.HEIGHT = 250: Main.CENTER: Main.BorderStyle = bsNone: Main.Color = &H777777

SecD.Parent = Main: SecD.Top = 70: SecD.Left = 160: SecD.Width = 50: SecD.Text = Str$(0)
MinD.Parent = Main: MinD.Top = 70: MinD.Left = 100: MinD.Width = 50: MinD.Text = Str$(0)
HourD.Parent = Main: HourD.Top = 70: HourD.Left = 40: HourD.Width = 50: HourD.Text = Str$(0)

SecAD.Parent = Main: SecAD.Top = 130: SecAD.Left = 160: SecAD.Width = 50: SecAD.Text = Str$(0): SecAD.Enabled = 0
MinAD.Parent = Main: MinAD.Top = 130: MinAD.Left = 100: MinAD.Width = 50: MinAD.Text = Str$(0): MinAD.Enabled = 0
HourAD.Parent = Main: HourAD.Top = 130: HourAD.Left = 40: HourAD.Width = 50: HourAD.Text = Str$(0): HourAD.Enabled = 0

Colon1.Parent = Main: Colon1.Top = 74: Colon1.Left = 93: Colon1.Caption = ":"
Colon2.Parent = Main: Colon2.Top = 74: Colon2.Left = 153: Colon2.Caption = ":"
Colon3.Parent = Main: Colon3.Top = 134: Colon3.Left = 93: Colon3.Caption = ":"
Colon4.Parent = Main: Colon4.Top = 134: Colon4.Left = 153: Colon4.Caption = ":"

Watch.Parent = Main: Watch.BMPHandle = WatchP: Watch.Height = 250: Watch.Width = 250: Watch.Top = 0: Watch.Left = 0: Watch.OnClick = Max: Watch.Transparent = 1

Button.Parent = Main: Button.Top = 3: Button.Left = 140: Button.BMPHandle = StopP: Button.OnClick = CountButton: Button.Width = 11: Button.Height = 11: Button.Transparent = 1
CloseBut.Parent = Main: CloseBut.Top = 3: CloseBut.Left = 215: CloseBut.Width = 11: CloseBut.Height = 11: CloseBut.BMPHandle = CloseP: CloseBut.Transparent = 1: CloseBut.OnClick = CloseSubNotMinimize
MinBut.Parent = Main: MinBut.Top = 3: MinBut.Left = 185: MinBut.Width = 11: MinBut.Height = 11: MinBut.BMPHandle = MinP: MinBut.Transparent = 1: MinBut.OnClick = MinimizeSubNotClose
MaxBut.Parent = Main: MaxBut.Top = 3: MaxBut.Left = 200: MaxBut.Width = 11: MaxBut.Height = 11: MaxBut.BMPHandle = MaxP: MaxBut.Transparent = 1: MaxBut.OnClick = Max
BoxBut.Parent = Main: BoxBut.Top = 3: BoxBut.Left = 155: BoxBut.Width = 11: BoxBut.Height = 11: BoxBut.BMPHandle = BoxP: BoxBut.Transparent = 1: BoxBut.OnClick = Box
QuesBut.Parent = Main: QuesBut.Top = 3: QuesBut.Left = 170: QuesBut.Width = 11: QuesBut.Height = 11: QuesBut.BMPHandle = QuesP: QuesBut.Transparent = 1: QuesBut.OnClick = Help
MHBut.Parent = Main: MHBut.Top = 226: MHBut.Left = 8: MHBut.Width = 24: MHBut.Height = 17: MHBut.BMPHandle = MHB: MHBut.Transparent = 1: MHBut.OnClick = MHWeb

CurTime.Parent = Main: CurTime.Top = 229: CurTime.Left = 150: CurTime.Caption = TIME$
Time1.Parent = Main: Time1.Top = 50: Time1.Left = 10: Time1.Caption = "Time until shutdown:"
Time2.Parent = Main: Time2.Top = 110: Time2.Left = 10: Time2.Caption = "Shutdown will occur at:"

'Timer.Interval=1000 = Timer.Interval=1 second
SecT.Interval = 1000: SecT.OnTimer = Count: SecT.Enabled = 0
CurTim.Interval = 1000: CurTim.OnTimer = UPDTIM: CurTim.Enabled = 1

'Shows the form
Main.SHOWMODAL

