Mein erstes kleines Project: Stoppuhr

  • Hallo,

    ich habe heute mein erstes, kleines richtiges Projekt erstellt. :D
    Ist zwar nicht so gut und sind noch Bugs drin, aber ich
    krieg es auch überhaupt gar nicht hin Ordnung in meinen Code zu bekommen.

    Naja wie findet ihr es?
    Ihr könnt auch die F1-F4 Tasten zum Starten/Pausieren benutzen! :D

    Spoiler anzeigen
    [autoit]

    #include <GUIConstants.au3>
    #include <Date.au3>
    #include <WindowsConstants.au3>

    [/autoit] [autoit][/autoit] [autoit]

    hotkeyset("{F1}","_startfirsth")
    hotkeyset("{F2}","_startsecondh")
    hotkeyset("{F3}","_startthirdh")
    hotkeyset("{F4}","_startallh")
    _declare()
    #Region ### START Koda GUI section ### Form=
    $Form1 = GUICreate("Stoppuhr", 777, 212, 193, 293)
    $Group1 = GUICtrlCreateGroup("Stoppuhr1", 16, 24, 225, 113)
    $Zeit1 = GUICtrlCreateLabel("000.00.00.000", 20, 65, 210, 41)
    GUICtrlSetFont(-1, 24, 400, 0, "MS Sans Serif")
    GUICtrlCreateGroup("", -99, -99, 1, 1)
    $Group2 = GUICtrlCreateGroup("Stoppuhr2", 272, 24, 225, 113)
    $Zeit2 = GUICtrlCreateLabel("000.00.00.000", 276, 65, 210, 41)
    GUICtrlSetFont(-1, 24, 400, 0, "MS Sans Serif")
    GUICtrlCreateGroup("", -99, -99, 1, 1)
    $Group3 = GUICtrlCreateGroup("Stoppuhr3", 528, 24, 225, 113)
    $Zeit3 = GUICtrlCreateLabel("000.00.00.000 ", 532, 65, 210, 41)
    GUICtrlSetFont(-1, 24, 400, 0, "MS Sans Serif")
    GUICtrlCreateGroup("", -99, -99, 1, 1)
    $Start1 = GUICtrlCreateButton("Start", 16, 144, 97, 33)
    $Pause1 = GUICtrlCreateButton("Pause", 144, 144, 97, 33)
    $Start2 = GUICtrlCreateButton("Start", 272, 144, 97, 33)
    $Pause2 = GUICtrlCreateButton("Pause", 400, 144, 97, 33)
    $Start3 = GUICtrlCreateButton("Start", 528, 144, 97, 33)
    $Pause3 = GUICtrlCreateButton("Pause", 656, 144, 97, 33)
    $Menu = GuiCtrlCreateMenu("Settings")
    $MenuColor = GuiCtrlCreateMenuItem("Color",$Menu)
    $Menuabout = GuiCtrlCreateMenuItem("About",$Menu)
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###

    [/autoit] [autoit][/autoit] [autoit]

    $Form2 = GUICreate("Form2", 517, 171, 336, 218)
    $Label1 = GUICtrlCreateLabel("Start Buttons:", 64, 32, 68, 17)
    $Label2 = GUICtrlCreateLabel("Pause Buttons", 64, 56, 73, 17)
    $Label3 = GUICtrlCreateLabel("Uhr Schrift:", 64, 80, 57, 17)
    $Label4 = GUICtrlCreateLabel("Uhr Hintergrund:", 64, 104, 82, 17)
    $Label5 = GUICtrlCreateLabel("Group Farbe:", 64, 128, 66, 17)
    $sc = GUICtrlCreateInput("0x00FF00", 160, 32, 121, 21)
    $pc= GUICtrlCreateInput("0xFF0000", 160, 56, 121, 21)
    $usc = GUICtrlCreateInput("0xFFFF00", 160, 80, 121, 21)
    $uhc = GUICtrlCreateInput("0x000000", 160, 104, 121, 21)
    $gc = GUICtrlCreateInput("0xFFFFFF", 160, 128, 121, 21)
    $Back = GUICtrlCreateButton("Speichern und Zurück", 312, 32, 185, 121)
    #EndRegion ### END Koda GUI section ###

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    _colors()
    While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    $end=msgbox(36,"Stoppuhr beenden","Programm wirklich beenden??")
    If $end = 6 then
    Exit
    endif
    Case $Start1
    _startfirst()

    Case $Start2
    _startsecond()

    Case $Start3
    _startthird()

    Case $Pause1
    _pauseone()
    Case $Pause2
    _pausetwo()
    Case $Pause3
    _pausethree()
    Case $Form1
    case $MenuColor
    GUISetState(@SW_DISABLE,$Form1)
    GUISetState(@SW_SHOW,$Form2)
    Case $back
    GUISetState(@SW_ENABLE,$Form1)
    GUISetState(@SW_HIDE,$Form2)
    _colors()
    Case $MenuAbout
    msgbox(64,"About","Programm: Stoppuhr"&@CRLF&"Version: 1.0"&@CRLF&"Erstelldatum: 3.7.2012"&@CRLF&"Autor: ThomasTT"&@CRLF&@CRLF&"$§%/!(Icelog)!\%§$")
    EndSwitch
    _zaehler()
    _pausedisable()
    sleep(10)
    WEnd

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    func _start1()
    Global $timer1=Timerinit()
    Global $s1=True
    endfunc
    func _start2()
    Global $timer2=Timerinit()
    Global $s2=True
    endfunc
    func _start3()
    Global $timer3=Timerinit()
    Global $s3=True
    endfunc

    [/autoit] [autoit][/autoit] [autoit]

    func _zaehler()
    If $s1=true Then
    global $time1=(Timerdiff($Timer1)) + $pausedTime1
    $msecm=mod($time1,1000)
    $msec1=Floor($msecm)
    _TicksToTime($time1, $hour1,$min1,$sec1)
    If $hour1< 10 Then $hour1 = "00" & $hour1
    If $hour1>10 AND $hour1<100 then $hour1 = "0" & $hour1
    If $min1 < 10 Then $min1 = "0" & $min1
    If $sec1 < 10 Then $sec1 = "0" & $sec1

    [/autoit] [autoit][/autoit] [autoit]

    guictrlsetdata($Zeit1, $hour1&"."&$min1&"."&$sec1&"."&$msec1)
    endif


    If $s2=true Then
    global $time2=(Timerdiff($Timer2)) + $pausedTime2
    $msecm=mod($time2,1000)
    $msec2=Floor($msecm)
    _TicksToTime($time2, $hour2,$min2,$sec2)
    If $hour2< 10 Then $hour2 = "00" & $hour2
    If $hour2>10 AND $hour2<100 then $hour2 = "0" & $hour2
    If $min2 < 10 Then $min2 = "0" & $min2
    If $sec2 < 10 Then $sec2 = "0" & $sec2

    [/autoit] [autoit][/autoit] [autoit]

    guictrlsetdata($Zeit2, $hour2&"."&$min2&"."&$sec2&"."&$msec2)
    endif


    If $s3=true Then
    Global $time3=(Timerdiff($Timer3)) + $pausedTime3
    $msecm=mod($time3,1000)
    $msec3=Floor($msecm)
    _TicksToTime($time3, $hour3,$min3,$sec3)
    If $hour3< 10 Then $hour3= "00" & $hour3
    If $hour3>10 AND $hour3<100 then $hour3 = "0" & $hour1
    If $min3 < 10 Then $min3= "0" & $min3
    If $sec3 < 10 Then $sec3 = "0" & $sec3

    [/autoit] [autoit][/autoit] [autoit]

    guictrlsetdata($Zeit3, $hour3&"."&$min3&"."&$sec3&"."&$msec3)
    endif
    endfunc

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    func _declare()
    Global $s1=False
    Global $s2=False
    Global $s3=False
    Global $hour1=00
    Global $hour2=00
    Global $hour3=00
    Global $sec1=00
    Global $sec2=00
    Global $sec3=00
    Global $min1=00
    Global $min2=00
    Global $min3=00
    Global $msec1=00
    Global $msec2=00
    Global $msec3=00
    Global $pausedTime1=0
    Global $pausedTime2=0
    Global $pausedTime3=0
    Global $Time1=0
    Global $Time2=0
    Global $Time3=0
    Global $sc = 0x00FF00
    Global $pc= 0xFF0000
    Global $usc = 0xFFFF00
    Global $uhc = 0x000000
    Global $gc = 0xFFFFFF
    endfunc

    [/autoit] [autoit][/autoit] [autoit]

    func _colors()
    guisetbkcolor(0x000000,$Form1)
    guictrlsetbkcolor($Pause1,guictrlread($pc))
    guictrlsetbkcolor($Pause2,guictrlread($pc))
    guictrlsetbkcolor($Pause3,guictrlread($pc))
    guictrlsetbkcolor($Start1,guictrlread($sc))
    guictrlsetbkcolor($Start2,guictrlread($sc))
    guictrlsetbkcolor($Start3,guictrlread($sc))
    guictrlsetbkcolor($Zeit1,guictrlread($uhc))
    guictrlsetbkcolor($Zeit2,guictrlread($uhc))
    guictrlsetbkcolor($Zeit3,guictrlread($uhc))
    guictrlsetcolor($Zeit1,guictrlread($usc))
    guictrlsetcolor($Zeit2,guictrlread($usc))
    guictrlsetcolor($Zeit3,guictrlread($usc))
    guictrlsetcolor($group1,guictrlread($gc))
    guictrlsetcolor($group2,guictrlread($gc))
    guictrlsetcolor($group3,guictrlread($gc))
    endfunc

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    func _startfirst()
    If guictrlread($start1) = "Start" then
    guictrlsetdata($start1, "Reset")
    guictrlsetdata($Pause1,"Pause")
    _start1()
    Else
    guictrlsetdata($start1, "Start")
    Global $s1=false
    guictrlsetdata($Zeit1, "000.00.00.000")
    Global $time1=0
    Global $pausedTime1=0
    guictrlsetdata($Pause3, "Pause")
    endif
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    func _startsecond()
    If guictrlread($start2) = "Start" then
    guictrlsetdata($start2, "Reset")
    guictrlsetdata($Pause2,"Pause")
    _start2()
    Else
    guictrlsetdata($start2, "Start")
    Global $s2=false
    guictrlsetdata($Zeit2, "000.00.00.000")
    Global $time2=0
    Global $pausedTime2=0
    guictrlsetdata($Pause3, "Pause")
    endif
    endfunc

    [/autoit] [autoit][/autoit] [autoit]

    func _startthird()
    If guictrlread($start3) = "Start" then
    guictrlsetdata($start3, "Reset")
    guictrlsetdata($Pause3,"Pause")
    _start3()
    Else
    guictrlsetdata($start3, "Start")
    Global $s3=false
    guictrlsetdata($Zeit3, "000.00.00.000")
    Global $time3=0
    Global $pausedTime3=0
    guictrlsetdata($Pause3, "Pause")
    endif
    EndFunc

    func _pauseone()
    If guictrlread($Pause1) = "Pause" then
    Global $s1=false
    guictrlsetdata($Pause1, "Fortsetzen")
    $pausedTime1=$Time1

    else
    _start1()
    guictrlsetdata($Pause1, "Pause")
    endif
    endfunc

    [/autoit] [autoit][/autoit] [autoit]

    func _pausetwo()
    If guictrlread($Pause2) = "Pause" then
    Global $s2=false
    guictrlsetdata($Pause2, "Fortsetzen")
    $pausedTime2=$Time2

    else
    _start2()
    guictrlsetdata($Pause2, "Pause")
    endif
    endfunc

    [/autoit] [autoit][/autoit] [autoit]

    func _pausethree()
    If guictrlread($Pause3) = "Pause" then
    Global $s3=false
    guictrlsetdata($Pause3, "Fortsetzen")
    $pausedTime3=$Time3
    else
    _start3()
    guictrlsetdata($Pause3, "Pause")
    endif
    endfunc

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    func _startallh()
    _startfirsth()
    _startsecondh()
    _startthirdh()
    endfunc

    [/autoit] [autoit][/autoit] [autoit]

    func _pausedisable()
    If guictrlread($Zeit1)= "000.00.00.000" then
    guictrlsetstate($Pause1, $GUI_DISABLE)
    else
    guictrlsetstate($Pause1, $GUI_ENABLE)
    endif

    If guictrlread($Zeit2)= "000.00.00.000" then
    guictrlsetstate($Pause2, $GUI_DISABLE)
    else
    guictrlsetstate($Pause2, $GUI_ENABLE)
    endif

    If guictrlread($Zeit3)= "000.00.00.000" then
    guictrlsetstate($Pause3, $GUI_DISABLE)
    else
    guictrlsetstate($Pause3, $GUI_ENABLE)
    endif
    sleep(25)
    endfunc

    [/autoit] [autoit][/autoit] [autoit]

    func _startfirsth()
    If guictrlread($start1) = "Start" then
    guictrlsetdata($start1, "Reset")
    guictrlsetdata($Pause1,"Pause")
    _start1()
    Else
    _pauseone()
    endif
    endfunc

    [/autoit] [autoit][/autoit] [autoit]

    func _startsecondh()
    If guictrlread($start2) = "Start" then
    guictrlsetdata($start2, "Reset")
    guictrlsetdata($Pause2,"Pause")
    _start2()
    Else
    _pausetwo()
    endif
    endfunc

    [/autoit] [autoit][/autoit] [autoit]

    func _Startthirdh()
    If guictrlread($start3) = "Start" then
    guictrlsetdata($start3, "Reset")
    guictrlsetdata($Pause3,"Pause")
    _start3()
    Else
    _pausethree()
    endif
    endfunc

    [/autoit] [autoit][/autoit] [autoit][/autoit]
  • Ich finde es schon ganz schön für den Anfang!

    Allerdings könntest du den Code viel kürzer machen, indem du bspw. Parameter nutzt.
    Du kannst also alle Funktionen, die sich in ihrer Struktur/Aufgabe ähneln (oder gleich sind), zu einer einzigen machen.
    Geht z.B. bei:
    _startfirsth()
    _startsecondh()
    _startthirdh()
    _pauseone()
    _pausetwo()
    _pausethree()