Click here to Skip to main content
12,508,185 members (53,194 online)
Click here to Skip to main content
Add your own
alternative version

Stats

131.8K views
3.6K downloads
40 bookmarked
Posted

A Time-line control using WTL

, 23 May 2003
Rate this:
Please Sign up or sign in to vote.
An alternative approach to selecting date ranges.
<!-- Article image -->

Screen shot

Introduction

CTimeLineCtrl provides an alternative way of selecting a regularly recurring date range.

Features

  • one-click date range selection at different granularities (eg week, month, year)
  • display today's date highlighted
  • year digits kept in view
  • display n 'date-range bands' in whatever ordering is required
  • scroll limits can be set

Using CTimeLineCtrl in a WTL Application

This control was developed using VS.NET, WTL 7, and the Feb 2003 Platform SDK. It has been tested under WindowsXP only.

To use CTimeLineCtrl in your application:

  1. Copy the following files to your application directory:
    • atlgdix.h (it's included in the zip files, but you can also download it here)
    • TimeLineScroll.h
    • TimeLineCtrl.cpp
    • TimeLineCtrl.h
    • TimeLineBand.cpp
    • TimeLineBand.h

    If you want to use the Year, Month, or Week bands, also add the following files:
    • TimeLineBandImpl.cpp
    • TimeLineBandImpl.h
       
  2. Add these files to your project.
  3. If you're not already using them, add <atlcomtime.h> and <atltypes.h> to your <stdafx.h> file.
  4. Insert #include "TimeLineCtrl.h" prior to the definition of the parent window class.
  5. Add a new member variable to the parent window class. The class will be CTimeLineCtrl - give it any name you desire.
  6. Add code at an appropriate place (eg OnCreate or OnInitDialog) to create the control window.
Here is an example of how to create the control. <!-- If there's a better way to handle code-indenting in HTML, please let me know! This is butt-ugly. -->
LRESULT
CMainDlg::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, 
    LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
    ...
    m_wndTimeLine.Create(m_hWnd, 0, 0, WS_CHILD | WS_VISIBLE | WS_HSCROLL,
        WS_EX_STATICEDGE, IDC_TIMELINE);
    m_wndTimeLine.AutoPosition(); // Position the control at the 
                                  // bottom of the parent window.
    ...
}

You can also add m_wndTimeLine.AutoPosition() to your OnSize handler to keep the control at the top or bottom of the parent window.

Implementation Details

This control is implemented using a combination of several classes:

CTimeLineScroll<T>

This is a mix-in class that handles the WM_HSCROLL message. If you need to manage scrolling through a range of dates, you can use this class on it's own. For example:

class CMyWindow :
    public CWindowImpl<CMyWindow>
    public CTimeLineScroll<CMyWindow>
{
    ...
    BEGIN_MSG_MAP(CMyWindow)
      ...
      CHAIN_MSG_MAP(CTimeLineScroll<CMyWindow>)
    END_MSG_MAP()
    ...
    // Handle the scroll request here.
    void DoScroll(int cxDelta);
};

CTimeLineBand

This is an abstract base class used by CTimeLineCtrl to handle date-range rendering and selection.

I've included implementations for year, month, and week ranges (ie CTimeLineYearBand, CTimeLineMonthBand, and CTimeLineWeekBand respectively). If you want to add your own custom date-range bands, these will hopefully give you a good place to start.

Note that in this design, these classes are internal implementation classes for use by CTimeLineCtrl. Users of CTimeLineCtrl don't need to be aware of their existence.

CTimeLineCtrl

This is a CWindowImpl-derived class that acts as a host for instances of CTimeLineBand-derived classes. It mixes in CTimeLineScroll to handled WM_HSCROLL, and delegates rendering requests to its collection of bands.

The collection of bands is kept in a std::deque. The order in which bands are displayed in the control follows the ordering of bands in this collection.

Incidentally, in this implementation, I chose to create the different bands on the heap (preferring to keep the implementation details of the bands hidden from users of the control). With a few changes, you could have them as member variables of this (or a derived) control instead. Just make sure you modify the memory management code in OnDestroy if you do.

Here's an example of adding a band to CTimeLineCtrl:

LRESULT
CTimeLineCtrl::OnCreate(LPCREATESTRUCT /*lpcs*/)
{
    ...
    AddBand(new CTimeLineWeekBand(*this));
    ...
}

Enjoy!

Acknowledgements

  • Bjarke Viksoe - atlgdix.h (used for flicker-free drawing)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Tony Ioanides
Web Developer
Australia Australia
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
Generalfatal error RC1015: cannot open include file 'atlres.h'. Pin
Vicky_Jain5-Oct-08 20:23
memberVicky_Jain5-Oct-08 20:23 
GeneralRe: fatal error RC1015: cannot open include file 'atlres.h'. Pin
RedFraggle28-Nov-08 6:59
memberRedFraggle28-Nov-08 6:59 
GeneralRe: fatal error RC1015: cannot open include file 'atlres.h'. Pin
JamesDK9-Sep-09 19:56
memberJamesDK9-Sep-09 19:56 
QuestionCan this code be in c# Pin
Nhilesh B23-Oct-07 23:12
memberNhilesh B23-Oct-07 23:12 
GeneralGeneral Purpose Timeline Control Pin
sdancer7513-Sep-07 23:36
membersdancer7513-Sep-07 23:36 
GeneralRe: General Purpose Timeline Control Pin
gyingLiow20-Jul-08 23:06
membergyingLiow20-Jul-08 23:06 
GeneralRe: General Purpose Timeline Control Pin
maddin23-Sep-09 9:50
membermaddin23-Sep-09 9:50 
Generalnewer WTL versions compilation problems Pin
Jan Stetka10-Jun-07 1:48
memberJan Stetka10-Jun-07 1:48 
GeneralAnother timeline control... Pin
DavidJonson3-Mar-06 15:37
memberDavidJonson3-Mar-06 15:37 
GeneralRe: Another timeline control... Pin
Soliant3-Jun-06 17:05
memberSoliant3-Jun-06 17:05 
GeneralRe: Another timeline control... Pin
memo-517-Nov-07 7:53
membermemo-517-Nov-07 7:53 
GeneralThe sample doesn't want to compile Pin
Renny[RuS]28-Feb-06 1:15
memberRenny[RuS]28-Feb-06 1:15 
GeneralRe: The sample doesn't want to compile Pin
Renny[RuS]28-Feb-06 2:54
memberRenny[RuS]28-Feb-06 2:54 
Generalatltypes and atlcomtime Pin
David Horner27-May-03 13:04
memberDavid Horner27-May-03 13:04 
GeneralRe: atltypes and atlcomtime Pin
Tony I27-May-03 13:36
memberTony I27-May-03 13:36 
GeneralRe: atltypes and atlcomtime Pin
David Horner27-May-03 13:39
memberDavid Horner27-May-03 13:39 
GeneralRe: atltypes and atlcomtime Pin
Marc A. Brown14-Jul-03 2:01
sussMarc A. Brown14-Jul-03 2:01 
GeneralRe: atltypes and atlcomtime Pin
wayneamurphy10-Mar-04 16:07
memberwayneamurphy10-Mar-04 16:07 
QuestionMissing the .dsw and .dsp file? Pin
dxhdxh25-May-03 20:50
memberdxhdxh25-May-03 20:50 
AnswerRe: Missing the .dsw and .dsp file? Pin
Tony I25-May-03 22:10
memberTony I25-May-03 22:10 
AnswerRe: Missing the .dsw and .dsp file? Pin
Anonymous14-Jul-03 0:43
sussAnonymous14-Jul-03 0:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160927.1 | Last Updated 24 May 2003
Article Copyright 2003 by Tony Ioanides
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid