Skip to main content
Email Password   helpLost your password?

What is this control?

This is an attempt to reproduce the functionality of MS Outlook Calendar control as it appears in the Calendar view. It supports similar features as the MS Calendar control. This is a work in progress. Study the code in the demo, there you will see how to access the date item data when overriding clicks and double clicks. Below are a couple of screen shots of the control:

Month view

Week view

Introduction

Like many developers I searched within the CodeProject and other sites for a control similar to this one but could not find one so I decided to create my own. I needed a control with similar functionality, but not exactly the same as the Calendar control found in MS Outlook.

This piece or code/control was developed to help me display appointments and tasks in a Real Estate program that I am developing. I did not like the one that was already available but I wanted the look and feel to be similar to MS-Outlook. I hope you will find this control useful and use it in your applications.

What this code provides

One of the things I learned about the original Outlook Calendar control after I created this control was that it can display week views from 1 to 6 weeks at a time. This is one of the modifications I will be making to this control in the near future.

Disclaimer and acknowledgements

This control and the source code are free to be used with commercial and non commercial software. However you are not allowed to sell the source code for profit. The author of this article does not take any responsibility for any damage done or caused directly or indirectly by this source code or an application using this source code.

If you decide to redistribute the source code, please include my name and e-mail somewhere in the source. If you create an application with this control, I would appreciate an email describing what it is or a screen shot of it so that I'll know it is being used and may serve as an incentive to continue improving this code/control.

Special thanks go to Tom Furuya for sharing his excellent ColorSpy utility. It was very useful in determining the colors and the magnified views of the mouse pointer locations were critical in getting some of the drawing dimensions correct in this control.

Background

The code and its classes in this article provide all the functionality to not only display a Calendar control in Week or Month views but also to include and display entries (Appointments/Tasks) for the selected dates. Although this code does not provide the dialog boxes or windows necessary to enter entries it does provide you with the methods, properties and overridables necessary to create, modify and delete these entries.

I know this code is not perfect and that someone may actually find a better way to paint the control. I have done all that I can to make sure the painting of different views are as efficient as possible. Of course, if I make further enhancements or fixes to the code or if any of you come up with fixes, I will update this article.

Using the code

  1. The first step in using the CWVDateCtrl class is to add the following files to your project:
  2. You will also need to add a library link to the project.

You also need to add a custom control object within your FormView or Dialog Window. Make sure to name the class: DBSWMDateCtrl.

Visual Studio 6

Visual Studio .NET 2003

Now we create the variable for this control. Assign the variable name m_WVCtrl by pressing the right mouse button on the Dialog or FormView class and clicking the Add Member Variable... menu option. Enter CWMDateCtrl as the variable type and m_WVCtrl as the variable name.

Next, you need to attach the variable name to the custom control. Within the FormView or Dialog class code, locate the DoDataExchange procedure and add the following code anywhere after the //}}AFX_DATA_MAP line:

DDX_Control(pDX, IDC_CALCTRL, m_WMCtrl);

Now, within the FormView or Dialog box (usually within OnInitialUpdate or OnInitDialog) enter the following lines of code. You can initially display the Calendar control in Month or Week view by simply specifying WV_MONTHVIEW or WV_WEEKVIEW. This method is explained in more detail below:

COleDateTime dtS = COleDateTime::GetCurrentDate();
// Display Calendar in Month View

m_WVCtrl.SetCurrentDate(dtS, FALSE, WV_MONTHVIEW);

or

COleDateTime dtS = COleDateTime::GetCurrentDate();
// Display Calendar in Week View

m_WVCtrl.SetCurrentDate(dtS, FALSE, WV_WEEKVIEW);

If you need to change the view of the control at runtime you can use the SetView method:

m_WMCtrl.SetView(WV_WEEKVIEW, TRUE);

or

m_WMCtrl.SetView(WV_MONTHVIEW, TRUE);

When you compile your code and display the window which contains this custom control you should see the Calendar control displayed in Week or Month view.


Member functions

These functions are in alphabetical order.

void CWMDateCtrl::DeleteAllItems()

Description

Syntax

BOOL CWMDateCtrl::DeleteItem(long nItem)

Description

Syntax

COleDateTime CWMDateCtrl::GetCurrentDate()

Description

Syntax

void CWMDateCtrl::GetDateRange(COleDateTime *pStartDate, COleDateTime *pEndDate)

Description

Syntax

int CWMDateCtrl::GetDay()

int CWMDateCtrl::GetMonth()

int CWMDateCtrl::GetYear()

Description

Syntax

int CWMDateCtrl::GetView()

Description

Syntax

DWORD CWMDateCtrl::GetItemData(long nItem)

Description

Syntax

long CWMDateCtrl::InsertItem(COleDateTime dtStart, COleDateTime dtEnd, CString strLine /*=""*/, int nImage /*=-1*/)

Description

Syntax

BOOL CWMDateCtrl::IsMilitaryTime()

Description

Syntax

void CWMDateCtrl::SetBkColor(COLORREF clrBk)

Description

Syntax

void CWMDateCtrl::SetCurrentDate(COleDateTime dtDate, BOOL bMilitaryTime /*= FALSE*/, int nView /*= WV_WEEKVIEW*/)

Description

This method is used to specify the date that is to be used as the current date, if we are displaying the date item times using Military or Regular time and the type of view to be used. Using this method you have to specify a date, preferably the current date (this would be the highlighted date within the control). This control would take this date and determine which other dates are to be painted based on the specified view.

Syntax

void CWMDateCtrl::SetFont(CString strFName, int nSize)

Description

Use this method to change the default font and font size to be used to paint the Calendar control.

Syntax

void CWMDateCtrl::SetImageList(CImageList *pImgList)

Description

Syntax

void CWMDateCtrl::SetItemColor(long nItem, COLORREF clrItem)

Description

Syntax

void CWMDateCtrl::SetItemData(long nItem, DWORD dwData)

Description

Syntax

void CWMDateCtrl::SetMilitaryTime(BOOL bMil)

Description

Syntax

void CWMDateCtrl::SetView(int nV, BOOL bRedraw /*=TRUE*/)

Description

Syntax


Override functions

Currently, there are only two messages that you can override in order to interact with this control. The Calendar control will notify the parent window with a NM_CLICK and NM_DBLCLK. To override these notification messages follow these instructions:

  1. Add the following PROTECTEDmember function to your FormView or dialog box to process double click notifications from the Calendar control:
    afx_msg void OnCellDblClick(NMHDR *pNotifyStruct, 
           LRESULT* pResult); (Left mouse Double click)
    afx_msg void OnCellClick (NMHDR *pNotifyStruct, 
                  LRESULT* pResult); (Left mouse Click)
  2. Next add the following line to your message map:
    ON_NOTIFY(NM_DBLCLK, IDC_CALCTRL, OnCellDblClick)
    ON_NOTIFY(NM_CLICK, IDC_CALCTRL, OnCellClick)
  3. Edit the OnCellDblClick/OnCellClick member function. To access the Calendar information you need to cast pNotifyStruct to NM_WVCELLDATA.
    NM_WVCELLDATA *pData = (NM_WVCELLDATA *)pNotifyStruct;

NM_WVCELLDATA notification structure

CWVCellData *pCell;

CWVCellDataItem *pItem;

int iRow;

int iCol;

BOOL bDNFClicked;

long nItem;

History

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralSetTime() shows DATE instead of TIME in both controls... Pin
AlexEvans
23:09 27 Aug '08  
GeneralNIce job, but... Pin
AlexEvans
18:12 24 Aug '08  
GeneralRe: NIce job, but... Pin
Rolando E. Cruz-Marshall
2:49 25 Aug '08  
GeneralRe: NIce job, but... Pin
AlexEvans
3:46 25 Aug '08  
QuestionVS 2005 control library Pin
pseudokris
14:48 11 Feb '08  
QuestionUse this control in a vb.net 2003 project Pin
dbaratelli
7:56 14 Nov '06  
GeneralSmall bug Pin
Ice_2k
2:21 16 Oct '06  
QuestionWow! Excellent control! Pin
Willem_Le_Roux
6:12 10 Jul '06  
AnswerRe: Wow! Excellent control! Pin
Rolando E. Cruz-Marshall
16:05 10 Jul '06  
GeneralRe: Wow! Excellent control! Pin
Willem_Le_Roux
23:22 10 Jul '06  
GeneralSimilar control for ASP.NET (open-source) Pin
Dan Letecky
11:30 28 Jun '06  
GeneralWhy don't help us????? Pin
pippobuado51
22:23 26 May '06  
QuestionGreat work - let's try printing again... Pin
arnoldkempt
9:01 18 May '06  
GeneralExcellent !!...Just wondered though ?? Pin
si_69
7:02 26 Feb '06  
GeneralRe: Excellent !!...Just wondered though ?? Pin
Rolando E. Cruz-Marshall
11:24 26 Feb '06  
GeneralSmall bug in GetDateRange Pin
Tom Archer - MSFT
15:55 19 Feb '06  
GeneralRe: Small bug in GetDateRange Pin
Rolando E. Cruz-Marshall
17:17 19 Feb '06  
GeneralNew entries don't display Pin
Tom Archer - MSFT
14:41 18 Feb '06  
GeneralUse in VB Pin
tassieboy
18:12 9 Feb '06  
GeneralRe: Use in VB Pin
yangyancheng
20:09 7 Jul '06  
GeneralWay to go Rolando! Pin
c0d3m@n
14:38 8 Feb '06  
QuestionNeed Help, please Pin
pippobuado51
2:57 8 Feb '06  
GeneralA little problem Pin
fvan
5:45 24 Jan '06  
GeneralRe: A little problem Pin
Rolando E. Cruz-Marshall
5:49 24 Jan '06  
GeneralShowing a different month Pin
TWS_Dave
4:25 17 Jan '06  


Last Updated 6 Aug 2005 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009