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: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

    • A modern mobile IT strategy is no longer an option, it is an absolute business necessity. Today's most productive employees are not tied to a desk, an office, or a location. They are mobile. And your company's IT strategy has to be ready to support them with easy, reliable, 24/7 access to the business information they need, from anywhere in the world, across a broad range of communication devices. Here's how some of the nation's most progressive corporations are meeting the many needs of their mobile workers …

    Most Popular Programming Stories

    More for Developers

    Latest Developer Headlines

    RSS Feeds