Date "Countdown" class

This class allows the developer to quickly place a countdown control in a user interface. It is publicly derived from the CGCColorStatic class to provide the ability to control the background and text colors, the font utilized, the border style and more.

The CGCCountdownCtrl class provides the method Set() to set the starting point for the countdown control.


BOOL Set(const UINT Days = 0,	
         const UINT Hours = 1,
	 const UINT Minutes = 0,	
	 const UINT Seconds = 0,	
	 const BOOL TerminateAtZero = TRUE,
	 const UINT Style = CD_DAYS,
	 void (*fn_pointer)(UINT CtrlID) = NULL);	
The Set() method accepts the following parameters:
  • Days The number of days from which to start the countdown.
  • Hours The number of hours from which to start the countdown.
  • Minutes The number of minutes from which to start the countdown.
  • Seconds The number of seconds from which to start the countdown.
  • Another parameter, TerminateAtZero, indicates whether the countdown should terminate at 0 or continue with a negative countdown. There are some conditions controlling the effect of this parameter to be discussed later.

    The parameter Style, is used to control the way that the countdown control is presented. The styles that may be specified are as follows:

    CD_DAYS - Display days, hours, minutes and seconds as DD:HH:MM:SS

    Sample Image

    CD_HOURS - Display hours, minutes and seconds as HH:MM:SS

    Sample Image

    CD_MINUTES - Display minutes and seconds as MM:SS

    Sample Image

    CD_SECONDS - Display seconds only as SS.

    Sample Image

    The final parameter to Set(), allows the client to specify a procedure (return type void) to be called when zero is reached in the countdown. The procedure should accept a single parameter of type UINT that will be passed the control's id.

    If the client wishes to perform a 2 day, 12 hour and 30 minute countdown, and to stop when 0 is reached, a call similar to the following would be made:

    
    Set(2,12,30,0,TRUE,NULL);
    

    By design, the CGCCountdownCtrl class only allows negative countdown when the specified style is CD_DAYS to allow, essentially, an infinite negative countdown. However, if this is desired, ensure that the control is wide enough to display a large number of days.

    When specifying any other styles, the CGCCountdownCtrl class is designed to terminate at zero. That is, specifying the CD_HOURS, CD_MINUTES or CD_SECONDS style implies TRUE for the TerminateAtZero parameter.

    To start the countdown, the client should call the method

    
    BOOL Start();
    
    To stop the countdown, the client should call the method
    
    void Stop();	
    
    When calling Stop(), the countdown is halted at the point in the countdown at which Stop() was called. The countdown may be resumed at the point at which is was stopped by calling Start() again.

    The CGCCountdownCtrl class provides the method

    
    BOOL EndCountAtZero(const BOOL bZero = TRUE);
    
    to allow the client to change the setting of the TerminateAtZero parameter specified when Set() was called at any time during execution of the countdown or while the countdown is stopped.

    The CGCCountdownCtrl class overrides the OnTimer() handler to update the control's display. Its timer uses a private ID of CNTDWN_UPDATE_TIMER_ID. This ID is defined as WM_APP+2 because the CGCColorStatic class utilizes a timer ID of WM_APP+1. By having a separate ID for the control's display update timer, the client is able to combine flashing with the update of the countdown display. If a new class is derived from the CGCCountdownCtrl class that must also override the OnTimer() handler, the developer should define that class' timer id to be WM_APP+3 or greater. The class's OnTimer() handler should also call the CGCCountdownCtrl class' OnTimer() if needed.

    HOW TO USE CGCCountdownCtrl

  • 1. Add the CGCCountdownCtrl class to your project.
  • 2. Add the CGCColorStatic class to your project.
  • 3. Place a static text control using the Resource Editor.
  • 4. For best results, set the "Align Text" style to "Center" and check the "Center vertically" style.
  • 5. Use ClassWizard to create a member variable for the control of type CGCCountdownCtrl.
  • 6. Call the Start() method to start the countdown running.
  • Downloads

    Download demo application - 100 Kb
    Download demo source - 38 Kb


    Comments

    • One suggestion

      Posted by Legacy on 04/11/2001 12:00am

      Originally posted by: Pen

      Dear George:
      
      

      Thank you very much for the good control. There is one
      suggestion:


      The Stop() function may cause a unpredictable problem.

      If I call Stop() and then call Start() "immediately"
      (or within the 1000ms interval), The Timer set by Start()
      might be killed because of the m_bStopFlag Flag in the
      OnTimer function.

      Therefore the timer will never start until you call another Start().

      To solve this problem, I rewrite the Stop() function as following:


      void CGCCountdownCtrl::Stop()
      {
      // Kill the timer.
      KillTimer(CNTDWN_UPDATE_TIMER_ID);

      m_bStopFlag = FALSE; // Reset Stop Flag.
      m_bTimerFlag = FALSE; // Resent Timer Flag.
      }

      There I call KillTimer Immediately and set m_bStopFlag to false.
      Therefore, the later timer set by Start() won't be killed before it
      can work correctly in the OnTimer function.

      Reply
    Leave a Comment
    • Your email address will not be published. All fields are required.

    Top White Papers and Webcasts

    • Live Event Date: May 11, 2015 @ 1:00 p.m. ET / 10:00 a.m. PT One of the languages that have always been supported with the Intel® RealSense™ SDK (Software Developer Kit) is JavaScript, specifically so that web-enabled apps could be created. Come hear from Intel Expert Bob Duffy as he reviews his own little "space shooting" game where the orientation of your face controls the aiming reticle to help teach developers how to write apps and games in JavaScript that can use facial and gesture …

    • You may not realize the complexity you could be creating when developing mobile apps – many companies don't initially. You could be doubling your IT costs for development and delivery; not to mention, risking sales, productivity and brand satisfaction. Read this IBM-commissioned Forrester Study to understand the key cost drivers of mobile app delivery -- for both customer-facing and enterprise applications. Find out how you could lower costs and increase success with the right strategy and investment.

    Most Popular Programming Stories

    More for Developers

    RSS Feeds

    Thanks for your registration, follow us on our social networks to keep up-to-date