Year, Month and Date Selector enhanced | CodeGuru

Year, Month and Date Selector enhanced

This article is based on the code written by Shekar Narayanan and Subby Rajan. I have replaced the next and previous month buttons by 4 owner-drawn buttons representing previous year, previous month, next year and next month for 2 reasons: 1) pressing the right mouse button for changing the year isn’t user friendly in my […]

Written By
CodeGuru Staff
CodeGuru Staff
Apr 6, 1999
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

This article is based on the code written by Shekar Narayanan
and Subby Rajan.

I have replaced the next and previous month buttons by 4 owner-drawn
buttons representing previous year, previous month, next year and
next month for 2 reasons:

1) pressing the right mouse button for changing the year isn’t user
friendly in my opinion.

2) Default button behaviour. As it was before pressing the left mouse
button over the minus button and moving the mouse away before
releasing the button, leaves the minus button in the pressed
state.

By making the button owner-drawn, the focus rectangle can be removed
since we have to make sure that the focus is always on the window
containing the dates.

For this the treatment of the right mouse button has been removed.

Furthermore I have enhanced the keyboard treatment for next and previous day.
In case of a VK_RIGHT/VK_LEFT and the current month
stays valid, only the changed cells (previous day / current day)
need to be repainted instead of the whole window (which avoids the
annoying flicker of the window). For this the functions PreviousDay
and NextDay now return TRUE in case the month or year has changed.

I have also introduced VK_UP/VK_DOWN and VK_PRIOR/VK_NEXT (cursor
up/down, page up/down) which changed the date to the previous/next
week and the previous/next month.

The main reason why the WM_KILLFOCUS didn’t work, was caused by
the use of GetParent()->EnableWindow (FALSE). This has been corrected.

To make the Control Language independent, the Months and Weekdays
array are now initialized with the local settings defined in the
Control Panel.

It is now possible to set the date before calling the DoModal()
function of the dialog including the control. This can be done
by either calling the member functions SetDay (was SetDate()),
SetMonth and SetYear or by calling the new member function
SetDate which accepts 3 integers.

Since I am using Visual C++ 4.2 I alse replaced the false constants
by FALSE.

Download demo project – 22.7 KB

Download source – 8.3 KB

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.