0

How can I make Back Color feature work on the TDateTimePicker. I would just like to set the backbround color of the control.

Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
Arch Brooks
  • 183
  • 1
  • 3
  • 19

3 Answers3

2

The date time picker control, when shown in a themed app, has colours that are determined by the theme. You cannot control the colours used by the themed paint.

You could disable themes for the control using SetWindowTheme and achieve what you want. Here is an example using an interposer class:

type
  TDateTimePicker = class(Vcl.ComCtrls.TDateTimePicker)
  protected
    procedure CreateWnd; override;
    procedure CNNotify(var Message: TWMNotifyDT); message CN_NOTIFY;
  end;

procedure TDateTimePicker.CreateWnd;
begin
  inherited;
  SetWindowTheme(WindowHandle, '', '');
  CalColors.BackColor := clFuchsia;
  CalColors.MonthBackColor := clFuchsia;
  CalColors.TitleBackColor := clFuchsia;
end;

procedure TDateTimePicker.CNNotify(var Message: TWMNotifyDT);
var
  DropDownHandle: HWND;
begin
  inherited;
  case Message.NMHdr.code of
  DTN_DROPDOWN:
    begin
      DropDownHandle := Perform(DTM_GETMONTHCAL, 0, 0);
      SetWindowTheme(DropDownHandle, '', '');
    end;
  end;
end;

enter image description here

Note that we also need to disable themes on the date/time picker's child month calendar control. That's because the calendar is drawn using a separate window from the main control.

You could choose not to disable themes for the main control, in which case the control looks like this:

enter image description here

But I feel that looks a little odd.

@RRUZ wrote an excellent answer on the closely associated topic of VCL styles for the date time picker: Style properties for TDateTimePicker

Community
  • 1
  • 1
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
0

Use the Win32 API SendMessage() function, or the TDateTimePicker's own Perform() method, to send the DTP a DTM_SETMCCOLOR message.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

you can use SendMessage() to disable the windows theme

uses
Winapi.CommCtrl,Vcl.Styles,Vcl.Themes,uxTheme;
procedure TForm1.DateTimePicker1DropDown(Sender: TObject);
var hwnd: WinAPI.Windows.HWND;
begin
     hwnd:=SendMessage(TDateTimePicker(Sender).Handle,DTM_GETMONTHCAL,0,0);
     uxTheme.setWindowTheme(hwnd,'','');
end;

after the CalColors it works

Result Image

Link Youtube Vidéo

eyllanesc
  • 235,170
  • 19
  • 170
  • 241