Jump to content

My Stupid Code


Recommended Posts

Hey , I attempted to make an alarm, I know it looks rough and is more bulky then it odd to be but I rather sort of stick to this kind of method if it can be shorten great but I don't really want it changed much I would like to continue to use @Hour and @Min.

Can somebody confirm that I got the correct usage for @Hour though in my "If $Hour Then Hour = X ElseIf..."

[Edit]

A little bit confused because help file said @Hour = 00-23 instead of 01-24

but a problem however is it is 9 PM so it returns @hour = 21 instead of 20 thus 12 AM wouldn't be 23 :mellow:

I am pretty sure 12 AM is @hour = 00 but 12 PM is...or do I have that backwards :P

#include <Misc.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

_Singleton(@ScriptName)

If HotKeySet("{ESC}" , "_Exit") = 0 Then
    MsgBox(0, "Error registering hotkey" , "The program will now exit.")
    Exit
EndIf

$Alarm = GUICreate("" , 318 , 242 , Default , Default , $WS_POPUP, $WS_EX_TOPMOST)
$Laura_Set = GUICtrlCreatePic(@ScriptDir & "\Lor.bmp" , 0 , 0 , 318 , 242)
$Hour_Set = GUICtrlCreateCombo("1", 0, 0, 50, 20 , $CBS_DROPDOWNLIST)
$Min_Set = GUICtrlCreateCombo("00", 50, 0, 50, 20 , $WS_VSCROLL + $CBS_DROPDOWNLIST)
$State_Set = GUICtrlCreateCombo("AM" , 100 , 0 , 50 , 20 , $CBS_DROPDOWNLIST)
$Set = GUICtrlCreateButton("Set" , 150 , 0 , 50 , 20)
$Exit = GUICtrlCreateButton("X", 298, 222, 20, 20)
GUICtrlSetState($Laura_Set , $GUI_DISABLE)
GUICtrlSetData($State_Set , "PM")
GUICtrlSetData($Hour_Set , "2|3|4|5|6|7|8|9|10|11|12")
GUICtrlSetData($Min_Set , "1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59|")
GUISetState(@SW_SHOW)

While 1
    $Msg = GUIGetMsg()
    Switch $Msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Set
            _Alarm_Set(GUICtrlRead($Hour_Set) , GUICtrlRead($Min_Set) ,GUICtrlRead($State_Set))
        Case $Exit
            Exit
    EndSwitch
WEnd

Func _Alarm_Set($Hour , $Min , $State)
    GUIDelete($Alarm)

    ToolTip("Alarm is Set for " & $Hour & ":" & $Min & " " & $State & @CRLF & @CRLF & "Push Escape (ESC) to Exit"  , 0 , 0 , "Alarm")

    If $State = "AM" Then
        If $Hour = 1 Then
            $Hour = 01
        ElseIf $Hour = 2 Then
            $Hour = 02
        ElseIf $Hour = 3 Then
            $Hour = 03
        ElseIf $Hour = 4 Then
            $Hour = 04
        ElseIf $Hour = 5 Then
            $Hour = 05
        ElseIf $Hour = 6 Then
            $Hour = 06
        ElseIf $Hour = 7 Then
            $Hour = 07
        ElseIf $Hour = 8 Then
            $Hour = 08
        ElseIf $Hour = 9 Then
            $Hour = 09
        ElseIf $Hour = 10 Then
            $Hour = 10
        ElseIf $Hour = 11 Then
            $Hour = 11
        ElseIf $Hour = 12 Then
            $Hour = 00
        Else
            Exit
        EndIf
    ElseIf $State = "PM" Then
        If $Hour = 1 Then
            $Hour = 13
        ElseIf $Hour = 2 Then
            $Hour = 14
        ElseIf $Hour = 3 Then
            $Hour = 15
        ElseIf $Hour = 4 Then
            $Hour = 16
        ElseIf $Hour = 5 Then
            $Hour = 17
        ElseIf $Hour = 6 Then
            $Hour = 18
        ElseIf $Hour = 7 Then
            $Hour = 19
        ElseIf $Hour = 8 Then
            $Hour = 20
        ElseIf $Hour = 9 Then
            $Hour = 21
        ElseIf $Hour = 10 Then
            $Hour = 22
        ElseIf $Hour = 11 Then
            $Hour = 23
        ElseIf $Hour = 12 Then
            $Hour = 24
        Else
            Exit
        EndIf
    EndIf
;MsgBox(0 , @HOUR , $Hour)
    If $Min = 00 Then
        Do
        Sleep(500)
        MouseMove(Random(0 , 5) , Random(0 , 5))
    Until @HOUR = $Hour
    _Beep()
    Else
        Do
        Sleep(500)
        MouseMove(Random(0 , 5) , Random(0 , 5))
    Until @HOUR = $Hour And @MIN >= $Min
    _Beep()
    EndIf
EndFunc

Func _Beep()
While 1
    Sleep(250)
    Beep(Random(100 , 500) , Random(100, 500))
WEnd
EndFunc

Func _Exit()
    Exit
EndFunc
Edited by SkellySoul
Link to comment
Share on other sites

Here is brain fodder for you. I recently had to convert a time project of mine to a 12 hour clock display, instead of a 24 display (which I prefer, over am/pm..), and I came up with this 'little helper'. Anyway, I think much of your code can be replaced with something similar, and so then made much shorter.

Switch @HOUR
    Case 0
        $vLittleHand = 12
        $sAmPm = "AM"
    Case 13 To 23
        $vLittleHand = @HOUR - 12
        $sAmPm = "PM"
    Case Else
        $vLittleHand = @HOUR
        $sAmPm = "AM"
EndSwitch
ConsoleWrite($vLittleHand & ":" & @MIN & " " & $sAmPm & @LF)
;MsgBox(4096, "", $vLittleHand & ":" & @MIN & " " & $sAmPm)

- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Link to comment
Share on other sites

Here is brain fodder for you. I recently had to convert a time project of mine to a 12 hour clock display, instead of a 24 display (which I prefer, over am/pm..), and I came up with this 'little helper'. Anyway, I think much of your code can be replaced with something similar, and so then made much shorter.

Switch @HOUR
    Case 0
        $vLittleHand = 12
        $sAmPm = "AM"
    Case 13 To 23
        $vLittleHand = @HOUR - 12
        $sAmPm = "PM"
    Case Else
        $vLittleHand = @HOUR
        $sAmPm = "AM"
EndSwitch
ConsoleWrite($vLittleHand & ":" & @MIN & " " & $sAmPm & @LF)
;MsgBox(4096, "", $vLittleHand & ":" & @MIN & " " & $sAmPm)

Thanks Very much :mellow:

This seems to work and looks to be correct.

#include <Misc.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

FileInstall("Lor.bmp" , "Lor.bmp")

BlockInput(1)
If FileExists(@HomeDrive & "\Lor.bmp") Then
    FileDelete(@ScriptDir & "\Lor.bmp")
Else
    FileMove(@ScriptDir & "\Lor.bmp" , @HomeDrive & "\Lor.bmp")
EndIf
BlockInput(0)

_Singleton(@ScriptName)

If HotKeySet("{ESC}" , "_Exit") = 0 Then
    MsgBox(0, "Error registering hotkey" , "The program will now exit.")
    Exit
EndIf

$Alarm = GUICreate("" , 318 , 242 , Default , Default , $WS_POPUP, $WS_EX_TOPMOST)
$Laura_Set = GUICtrlCreatePic(@HomeDrive & "\Lor.bmp" , 0 , 0 , 318 , 242)
$Hour_Set = GUICtrlCreateCombo("1", 0, 0, 50, 20 , $CBS_DROPDOWNLIST)
$Min_Set = GUICtrlCreateCombo("00", 50, 0, 50, 20 , $WS_VSCROLL + $CBS_DROPDOWNLIST)
$State_Set = GUICtrlCreateCombo("AM" , 100 , 0 , 50 , 20 , $CBS_DROPDOWNLIST)
$Set = GUICtrlCreateButton("Set" , 150 , 0 , 50 , 20)
$Exit = GUICtrlCreateButton("X", 298, 222, 20, 20)
GUICtrlSetState($Laura_Set , $GUI_DISABLE)
GUICtrlSetData($State_Set , "PM")
GUICtrlSetData($Hour_Set , "2|3|4|5|6|7|8|9|10|11|12")
GUICtrlSetData($Min_Set , "01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59|")
GUISetState(@SW_SHOW)

While 1
    $Msg = GUIGetMsg()
    Switch $Msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Set
            _Alarm_Set(GUICtrlRead($Hour_Set) , GUICtrlRead($Min_Set) ,GUICtrlRead($State_Set))
        Case $Exit
            Exit
    EndSwitch
WEnd

Func _Alarm_Set($Hour , $Min , $State)
    GUIDelete($Alarm)

    ToolTip("Alarm is Set for " & $Hour & ":" & $Min & " " & $State & @CRLF & @CRLF & "Push Escape (ESC) to Exit"  , 0 , 0 , "Alarm")

    Switch @Hour
    Case 0
        $Get_Hour = 12
        $Get_State = "AM"
    Case 13 To 23
        $Get_Hour = @Hour - 12
        $Get_State = "PM"
    Case Else
        $Get_Hour = @Hour
        $Get_State = "AM"
    EndSwitch

    If $Min = 00 Then
    Do
        Sleep(500)
        MouseMove(Random(0 , 5) , Random(0 , 5))
    Until $Get_Hour = $Hour And $Get_State = $State
    _Beep()
    Else
    Do
        Sleep(500)
        MouseMove(Random(0 , 5) , Random(0 , 5))
    Until $Get_Hour = $Hour And @MIN >= $Min And $Get_State = $State
    _Beep()
    EndIf
EndFunc

Func _Beep()
While 1
    Sleep(250)
    Beep(Random(100 , 500) , Random(100, 500))
WEnd
EndFunc

Func _Exit()
    Exit
EndFunc
Edited by SkellySoul
Link to comment
Share on other sites

You'll never get a "12 PM" out of that code.

I'm not sure if there is a sneakier (shorter) way of doing it than:

$vLittleHand = @HOUR
Switch $vLittleHand
    Case 0 
        $vLittleHand = 12
        $sAmPm = "AM"
    Case 1 to 11 
        $sAmPm = "AM"
    Case 12 
        $sAmPm = "PM"
    Case 13 To 23
        $vLittleHand -= 12
        $sAmPm = "PM"
EndSwitch

Edit: Maybe this...

$vLittleHand = @HOUR
If Not Mod($vLittleHand, 12) Then $vLittleHand += 12 ; If 0 or 12 then add 12
If $vLittleHand > 12 Then
    $vLittleHand -= 12
    $sAmPm = "PM"
Else
    $sAmPm = "AM"
EndIf
Edited by Spiff59
Link to comment
Share on other sites

Thanks Very much :mellow:

You bet. Hope it helped or nudged or something..

This part of your code

If $Min = 00 Then
    Do
        Sleep(500)
        MouseMove(Random(0 , 5) , Random(0 , 5))
    Until $Get_Hour = $Hour And $Get_State = $State
    _Beep()
    Else
    Do
        Sleep(500)
        MouseMove(Random(0 , 5) , Random(0 , 5))
    Until $Get_Hour = $Hour And @MIN >= $Min And $Get_State = $State
    _Beep()
    EndIf

Can be shortened/changed to

Do
        Sleep(100)
        MouseMove(Random(0 , 5) , Random(0 , 5))
    Until $Get_Hour = $Hour And @MIN = $Min And $Get_State = $State
    _Beep()

- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Link to comment
Share on other sites

You'll never get a "12 PM" out of that code.

I'm not sure if there is a sneakier (shorter) way of doing it than:

$vLittleHand = @HOUR
Switch $vLittleHand
    Case 0 
        $vLittleHand = 12
        $sAmPm = "AM"
    Case 1 to 11 
        $sAmPm = "AM"
    Case 12 
        $sAmPm = "PM"
    Case 13 To 23
        $vLittleHand -= 12
        $sAmPm = "PM"
EndSwitch

Edit: Maybe this...

$vLittleHand = @HOUR
If Not Mod($vLittleHand, 12) Then $vLittleHand += 12 ; If 0 or 12 then add 12
If $vLittleHand > 12 Then
    $vLittleHand -= 12
    $sAmPm = "PM"
Else
    $sAmPm = "AM"
EndIf

Thanks for pointing that out , I tested and your right, however that code crashed on me.

I don't understand this code or how it works.

:mellow:

Link to comment
Share on other sites

Well, our requirments are:

1. The first 12 values returned by @HOUR (0-11) are all "AM", *BUT* only a zero needs to be bumped up by 12

and

2. The last 12 values (12-23) are all "PM", *BUT* only values 13-23 need to be decremented by 12

So, the second example makes life easier by handling the exceptions of 0 and 12 up front. Look at Mod() in the helpfile, it simple tells me if there is any remainder after dividing the hour by 12, if there is none, we add 12 to hour, converting 0 into 12, and 12 into 24. Now, the remaining test doesn't have to have exceptions for the "*BUT*" conditions listed above. We're now working with values of 1-24 and can set *ALL* of the first 12 values to AM, and with *ALL* of the final 12 values we can subtract 12 and assign them as PM.

There is no reason any of that code should cause a crash.

Edit: I did do a boolean compare on the result from the Mod() function by saying "If Not Mod($vLittleHand, 12)". That could cause some confusion. It would function exactly the same had I gone with "If Mod($vLittleHand, 12) = 0".

Edited by Spiff59
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...