Blog

All Blog Posts  |  Next Post  |  Previous Post

Adding weeknumbers to the VCL TDateTimePicker control calendar

Tuesday, August 18, 2009

The VCL component TMonthCalendar that is a wrapper for the Microsoft COMCTRL MonthCalendar has a WeekNumbers property with which you can control whether week numbers are displayed or not in the calendar. It also has properties ShowToday and ShowTodayCircle to control whether to display today's date in the calendar. The VCL TDateTimePicker is also a wrapper for a Microsoft COMCTRL DateTimePicker that at first sight has an identical calendar as the MonthCalendar as dropdown control. Yet, there is nowhere a property to control displaying weeknumbers and/or today's date in this calendar. With a few lines of code though, it is possible to have control over this. Implement the TDateTimePicker.OnDropDown event and add the code:
procedure TForm1.DateTimePicker1DropDown(Sender: TObject);
var
  dwstyle: dword;
  mch: THandle;
  rct: TRect;
begin
  mch := SendMessage((Sender as TDateTimePicker).Handle, DTM_GETMONTHCAL, 0,0);
  dwStyle := GetWindowLong(mch, GWL_STYLE);

  // add this line if you want to see week numbers in the dropdown calendar
  dwStyle := dwStyle or MCS_WEEKNUMBERS;
  // add this line if you do not want to see the circle before today's date in the dropdown calendar
  dwStyle := dwStyle or MCS_NOTODAYCIRCLE;
  // add this line if you do not want to see today's date in the dropdown calendar
  dwStyle := dwStyle or MCS_NOTODAY;

  SetWindowLong(mch, GWL_STYLE, dwStyle);
  // adapt the size of the dropdown calendar as the default size is a bit too small
  SendMessage(mch, MCM_GETMINREQRECT, 0, integer(@rct));
  MoveWindow(mch, 0, 0, rct.Right + 2, rct.Bottom + 2, True);
end;


Bruno Fierens




This blog post has not received any comments yet.



Add a new comment

You will receive a confirmation mail with a link to validate your comment, please use a valid email address.
All fields are required.



All Blog Posts  |  Next Post  |  Previous Post