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 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



Comments

  • has corrected

    Posted by Legacy on 04/19/2003 12:00am

    Originally posted by: AVB

    Has Found, in functions OnPaint() it is necessary to change fragment of the code
    if (!bDone && ((j+1) == nFirstDayofMonth || nFirstDayofMonth == 0))
    {
    bDone = TRUE;
    dc.FillSolidRect(rCurSel,::GetSysColor(COLOR_INFOBK));
    rCell.OffsetRect(nCellWidth,0);
    }
    else if (bDone) {

    on
    if (!bDone && ((j+1) == nFirstDayofMonth ))
    {
    bDone = TRUE;
    dc.FillSolidRect(rCurSel,::GetSysColor(COLOR_INFOBK));
    rCell.OffsetRect(nCellWidth,0);
    }
    else if (!bDone && (j == nFirstDayofMonth ))
    {
    bDone = TRUE;
    dc.FillSolidRect(rCurSel,::GetSysColor(COLOR_INFOBK));
    if (m_nDate == nIndex+1)
    {
    dc.FillSolidRect(rCurSel,::GetSysColor(COLOR_HIGHLIGHT));
    dc.SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
    }
    else
    {
    dc.FillSolidRect(rCurSel,::GetSysColor(COLOR_INFOBK));
    dc.SetTextColor(::GetSysColor(COLOR_INFOTEXT));
    }

    if (nIndex< nDays) {
    CString szT; szT.Format ("%2d",nIndex+1);
    dc.DrawText(szT, rCell, DT_VCENTER|DT_CENTER|DT_SINGLELINE);
    m_rCells[nIndex].CopyRect(rCell);
    nIndex++;
    }
    rCell.OffsetRect(nCellWidth,0);
    }
    else if (bDone) {

    Reply
  • incorrect work

    Posted by Legacy on 04/19/2003 12:00am

    Originally posted by: AVB

    The Class not it is correct works at month, where nFirstDayofMonth=0 and day sunday.
    See:
    02.04; 02.09; 02.15....

    Reply
  • Enhancements, Enhancements

    Posted by Legacy on 02/14/2002 12:00am

    Originally posted by: TheFlashback

    First of all you should not create fonts
    
    every time you need to paint something.
    This is expensive and unnecessary.

    The second thing is instead of using that
    *monster* function that gives you the
    first day of week you should have been using
    a sleak GetDayOfWeek() from COleDateTime

    COleDateTime time(YourYear, YourMonth, 1, 0, 0, 0);
    return time.GetDayOfWeek();

    Reply
  • ExCalendar

    Posted by Legacy on 01/30/2002 12:00am

    Originally posted by: Mike Philips

    If you would like to check, please feel free to download a nice calendar control here
    
    http://www.exontrol.com/sg.jsp?content=products/excalendar

    Regards, Mike

    Reply
  • mfc42ud.lib error due to Unicode setting

    Posted by Legacy on 09/22/1999 12:00am

    Originally posted by: Sridev Raghavan

    The linking error with mfc42ud.lib is seen when the project is compiled without installing Unicode libraries.

    Compile without Unicode
    Remove _UNICODE from the preprocessor defenition, Remove wWinMainCRTStartup from the startup symbol. Replace wcsncpy with strncpy in the code.
    It should now compile without Unicode.

    Reply
  • Year, Month and Date Selector enhanced

    Posted by Legacy on 04/16/1999 12:00am

    Originally posted by: Reinhard Dietrich

    One bug with focus if you use a keyboard.

    I use two Selector buttons controls in my dialog.
    On selecting the first button with the keybord and after closing the calender window the focus (dotted rectangle in the key) and the marker for activ key (black rectangle around the key) remains on the selected button (that's OK). But after moving the focus to the second Selector button and selecting a new date the focus goes back to the first Selector button (!) and the selected key marker remains on the second button.
    What is going wrong?

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

Top White Papers and Webcasts

  • Live Event Date: May 6, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT While you likely have very good reasons for remaining on WinXP after end of support -- an estimated 20-30% of worldwide devices still are -- the bottom line is your security risk is now significant. In the absence of security patches, attackers will certainly turn their attention to this new opportunity. Join Lumension Vice President Paul Zimski in this one-hour webcast to discuss risk and, more importantly, 5 pragmatic risk mitigation techniques …

  • Live Event Date: May 7, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT This eSeminar will explore three popular games engines and how they empower developers to create exciting, graphically rich, and high-performance games for Android® on Intel® Architecture. Join us for a deep dive as experts describe the features, tools, and common challenges using Marmalade, App Game Kit, and Havok game engines, as well as a discussion of the pros and cons of each engine and how they fit into your development …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds