Search code examples
c++windows-xpdatetimepickerlegacyc++builder-5

How to hide the "Today" button in TDateTimePicker?


I am currently working on a legacy program built in C++Builder 5.

I am working on an area where we have a few TDateTimePicker controls. The dtp itself works correctly, but due to a localization issue I am currently working on, I am looking to remove the "Today" button at the bottom of the picker which lets the user click on it to automatically select today's date.

An alternative to this would be to just remove the formatted date string from the bottom, so that it says "Today" rather than "Today MM/DD/YYYY".

I have found a few solutions that allow for formatting of the Today text in other languages, but none for C++. I have tried using some of the built in functions, such as height and text, but have been unsuccessful. I am not usually the one who works in this code, so I may hopefully be missing something simple.

EDIT: I am using Windows XP in a VM for this project.


Solution

  • To remove the "Today" text, you have to enable the MCS_NOTODAY window style on the DTP's child month calendar. That style has been available since Internet Explorer 3.

    In the TDateTimePicker::OnDropDown event, you can use DateTime_GetMonthCal() to get the HWND of the month calendar and then use SetWindowLong/Ptr(GWL_STYLE) to enable the MCS_NOTODAY style.

    #include <commctrl.h>
    
    void __fastcall TMyForm::DateTimePicker1DropDown(TObject *Sender)
    {
        HWND hCalendar = DateTime_GetMonthCal(DateTimePicker1->Handle);
        LONG_PTR style = GetWindowLongPtr(hCalendar, GWL_STYLE);
        SetWindowLongPtr(hCalendar, GWL_STYLE, style | MCS_NOTODAY);
    }
    

    On Vista and later, you can alternatively use DateTime_SetMonthCalStyle() to enable the MCS_NOTODAY style.

    DWORD style = DateTime_GetMonthCalStyle(DateTimePicker1->Handle);
    DateTime_SetMonthCalStyle(DateTimePicker1->Handle, style | MCS_NOTODAY);