Home | Blog | Software | Reviews and Features | Forum | Help | Donate | About us
topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • December 09, 2016, 03:19:25 PM
  • Proudly celebrating 10 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Simple Counter Utility Wanted  (Read 4163 times)

AndyM

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 616
    • View Profile
    • Donate to Member
Simple Counter Utility Wanted
« on: September 06, 2011, 03:56:59 PM »
Would someone please make me a simple Counter Program in AHK?

Two reasons:  I could use the utility and it would also serve well as an example of some of the AHK code I'd like to be more familiar with.  The GUI creation and counting subroutines would save me a lot of trial and error working the code out for myself.

There would be a simple small (~100 pixel tall x ~200 pixels wide) dialog box with:

   - Two buttons: Increment counter by 1, Decrement counter by 1.
   - Counter display (this could even be on one of the buttons)
  
The other feature would be that the counter would automatically increment by 1 each hour on the hour.  I could manually adjust the counter with the buttons for a start value, which would be incremented by 1 on the next hour (at the top of the hour, not an hour after I start).

That's all I'd need, but if you wanted to teach me one other thing, it would be to make the dialog box, buttons, counter, etc in different colors if this can be done in AHK.  You can pick the colors.

Thanks in advance to any takers!

Andy

skwire

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 4,668
    • View Profile
    • Donate to Member
Re: Simple Counter Utility Wanted
« Reply #1 on: September 07, 2011, 09:39:25 PM »
Here's a basic example (no colours):

Code: Autohotkey [Select]
  1. ; -------------------
  2. ; Set up environment.
  3. ; -------------------
  4. SetWorkingDir, %A_ScriptDir%
  5.  
  6. ; -------------------------
  7. ; Set any needed variables.
  8. ; -------------------------
  9. myCounter := 0
  10. Allow_Counter_Update := True
  11.  
  12. ; -------------
  13. ; Build out GUI
  14. ; -------------
  15. Gui, Margin, 3, 5
  16. Gui, Font, s30
  17. Gui, Add, Text  , xm     ym     w200 h100 0x1200 Center vCounter_txt          , % myCounter
  18. Gui, Add, Button, xm     ym+105 w95  h25                vPlus_btn    gOnButton, +1
  19. Gui, Add, Button, xm+105 ym+105 w95  h25                vMinus_btn   gOnButton, -1
  20.  
  21. SetTimer, OnHour, 1000 ; Fire every second.
  22.  
  23. Return ; End of auto-execute section.
  24.  
  25.  
  26. {
  27.     ExitApp
  28. }
  29. Return
  30.  
  31.  
  32. OnButton:
  33. {
  34.     If ( A_GuiControl = "Plus_btn" )
  35.     {
  36.         DoCounter( "Up" )
  37.     }
  38.     Else If ( A_GuiControl = "Minus_btn" )
  39.     {
  40.         DoCounter( "Down" )
  41.     }
  42. }
  43. Return
  44.  
  45.  
  46. OnHour:
  47. {
  48.     FormatTime, myNow, A_Now, HHmm
  49.     If ( Mod( myNow, 100 ) = 0 ) ; We're at the top of an hour.
  50.     {
  51.         If ( Allow_Counter_Update = True )
  52.         {
  53.             ; Increase counter by one and then don't allow any more
  54.             ; increases until the minute is up.  Otherwise, the
  55.             ; counter would increase by one every second.
  56.             DoCounter( "Up" )
  57.             Allow_Counter_Update := False
  58.         }
  59.     }
  60.    
  61.     ; We are no longer in a top-of-the-hour minute so allow
  62.     ; updates again for the next top-of-the-hour minute.
  63.     If ( Mod( myNow, 100 ) != 0 )
  64.     {
  65.         Allow_Counter_Update := True
  66.     }
  67. }
  68. Return
  69.  
  70.  
  71. DoCounter( sWay )
  72. {
  73.     Global
  74.    
  75.     If ( sWay = "Up" )
  76.     {
  77.         myCounter++
  78.     }
  79.     Else If ( sWay = "Down" )
  80.     {
  81.         myCounter--
  82.     }
  83.     GuiControl, Text, Counter_txt, % myCounter
  84. }
« Last Edit: September 07, 2011, 10:19:27 PM by skwire »

AndyM

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 616
    • View Profile
    • Donate to Member
Re: Simple Counter Utility Wanted
« Reply #2 on: September 07, 2011, 10:07:16 PM »
Ah skwire, that's just perfect, exactly what I was looking for.  And the comments are quite helpful.

Thank you!!!   :Thmbsup:

Andy

skwire

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 4,668
    • View Profile
    • Donate to Member
Re: Simple Counter Utility Wanted
« Reply #3 on: September 07, 2011, 10:17:16 PM »
You're most welcome.  Glad I could help.   :)

skwire

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 4,668
    • View Profile
    • Donate to Member
Re: Simple Counter Utility Wanted
« Reply #4 on: September 07, 2011, 10:20:01 PM »
BTW, I just made a change to the code that I meant to make before I posted it.  Please refresh and re-grab the code.  Sorry about that.

Winkie

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 82
    • View Profile
    • Donate to Member
Re: Simple Counter Utility Wanted
« Reply #5 on: September 09, 2011, 03:36:03 PM »
Of cource I can't beat skwire ;D, but this is how I like it:

Screenshot 20110909220838.png
Code: Autohotkey [Select]
  1.  
  2. iMyCount := 0
  3. bAllowUpdate := True
  4.  
  5. Gui, 1:+LabelMainGui
  6. Gui, 1:Margin, 5, 5
  7. Gui, 1:Font, s30
  8. Gui, 1:Add, Text, xm w200 h100 0x1200 Center Section vCounterCtrl, % iMyCount
  9. Gui, 1:Add, Button, ys w75 h25 Section vInput_btn gInputValue, Set
  10. Gui, 1:Font, w700
  11. Gui, 1:Add, Button, xs ys+45 w75 h25 gCountUp, +1
  12. Gui, 1:Add, Button, xs w75 h25 gCountDown, -1
  13. Gui, 1:Add, Checkbox, xm w138 h25 Checked vbDoHourCount gToggleHourCount, Count up on the hour
  14. Gui, 1:Add, Checkbox, x+5 w138 h25 vbAllowNegatives, Allow negative values
  15. Gui, 1:Show, Center, CountAM
  16.  
  17. SetTimer, HourCount, 1000
  18. Return
  19.  
  20. MainGuiClose:
  21. MainGuiEscape:
  22. {
  23.         ExitApp
  24. }
  25. Return
  26.  
  27. CountUp:
  28. {
  29.         iMyCount++
  30.         FormatAndUpdate( iMyCount )
  31. }
  32. Return
  33.  
  34. CountDown:
  35. {
  36.         Gui, 1:Submit, NoHide
  37.         If Not bAllowNegatives
  38.         {
  39.                 If iMyCount
  40.                         iMyCount--
  41.         }
  42.         Else
  43.                 iMyCount--
  44.         FormatAndUpdate( iMyCount )
  45. }
  46. Return
  47.  
  48. HourCount:
  49. {
  50.         FormatTime, iMyNow, A_Now, HHmm
  51.         If ( Mod( iMyNow, 100 ) = 0 ) ; We're at the top of an hour.
  52.         {
  53.                 If ( bAllowUpdate = True )
  54.                 {
  55.                         ; Increase counter by one and then don't allow any more increases until this minute is up.
  56.                         Gosub, CountUp
  57.                         bAllowUpdate := False
  58.                 }
  59.         }
  60.         Else
  61.         {
  62.                 bAllowUpdate := True
  63.         }
  64. }
  65. Return
  66.  
  67. ToggleHourCount:
  68. {
  69.         Gui, 1:Submit, NoHide
  70.         If bDoHourCount
  71.                 SetTimer, HourCount, 1000
  72.         Else
  73.                 SetTimer, HourCount, Off
  74. }
  75. Return
  76.  
  77. InputValue:
  78. {
  79.         Gui, 1:+Disabled
  80.         Gui, 2:+LabelInputGui +Owner1
  81.         Gui, 2:Margin, 12, 12
  82.         Gui, 2:Add, Edit, xm w80 r1 Number
  83.         Gui, 2:Add, UpDown, Range-32767-32767 viMyInput
  84.         Gui, 2:Add, Button, x+15 w80 Default gNewInput, OK
  85.         Gui, 2:Show, Center, Enter a value
  86. }
  87. Return
  88.  
  89. InputGuiClose:
  90. InputGuiEscape:
  91. {
  92.         Gui, 1:-Disabled
  93.         Gui, 2:Destroy
  94. }
  95. Return
  96.  
  97. NewInput:
  98. {
  99.         Gui, 2:Submit, NoHide
  100.         Gui, 2:+OwnDialogs
  101.         If Not iMyInput
  102.         {
  103.                 MsgBox, 36, Please confirm, Do you really want the value to be blank?
  104.                 IfMsgBox No
  105.                         Return
  106.         }
  107.         Gui, 1:-Disabled
  108.         Gui, 2:Destroy
  109.         Gui, 1:Default
  110.         FormatAndUpdate( iMyInput )
  111.         iMyCount := iMyInput
  112.         iMyInput :=
  113. }
  114. Return
  115.  
  116. ; FormatInteger -- by Winkie
  117. FormatAndUpdate( Int )
  118. {
  119.         vVar := Int
  120.         If vVar < 0
  121.         {
  122.                 vMinus := "-"
  123.                 StringTrimLeft, vVar, vVar, 1
  124.         }
  125.         Loop
  126.         {
  127.                 If vVar > 999
  128.                 {
  129.                         StringLen, vLen, vVar
  130.                         vTrim := vLen - 3
  131.                         StringTrimLeft, vPart, vVar, % vTrim
  132.                         vOut := "." vPart vOut
  133.                         StringTrimRight, vVar, vVar, 3
  134.                 }
  135.                 Else
  136.                         Break
  137.         }
  138.         Gui, 1:Default
  139.         GuiControl, Text, CounterCtrl, % vMinus vVar vOut
  140. }
« Last Edit: September 13, 2011, 01:54:14 PM by Winkie, Reason: Un-hotlinked image »

skwire

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 4,668
    • View Profile
    • Donate to Member
Re: Simple Counter Utility Wanted
« Reply #6 on: September 09, 2011, 03:48:45 PM »
Nice job, Winkie.  Well done.   :Thmbsup:

AndyM

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 616
    • View Profile
    • Donate to Member
Re: Simple Counter Utility Wanted
« Reply #7 on: September 09, 2011, 04:15:00 PM »
This is what I ended up with.


Counter.png

skwire

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 4,668
    • View Profile
    • Donate to Member
Re: Simple Counter Utility Wanted
« Reply #8 on: September 09, 2011, 06:05:44 PM »
Nice, AndyM.  What are you using it for?

AndyM

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 616
    • View Profile
    • Donate to Member
Re: Simple Counter Utility Wanted
« Reply #9 on: September 09, 2011, 06:22:19 PM »
Counting  ;D

app103

  • That scary taskbar girl
  • Global Moderator
  • Joined in 2006
  • *****
  • Posts: 5,666
    • View Profile
    • App's Apps
    • Read more about this member.
    • Donate to Member
Re: Simple Counter Utility Wanted
« Reply #10 on: September 09, 2011, 08:18:48 PM »
It's almost similar to what you wanted, without the automatic incrementing, and without the deincrementing, and you need to have a target goal, it has a progress bar, it saves data to the registry (so it isn't portable)...and it's not written in AHK: AnotherOneDone (something I released for GOE challenge)