How to change color of TDateTimePicker?
Asked Answered
A

2

3

How do i change the color of a TDateTimePicker?

A Date and Time Picker can have a color:

enter image description here

Normally this is done by setting the Color:

procedure TForm1.FormCreate(Sender: TObject);
begin
   DateTimePicker1.Color := clLime;
end;

But when using version 6 of the Date and Time Picker Control, the color no longer works:

enter image description here

I tried using SetWindowTheme to disable the style of the TDateTimePicker:

procedure TForm1.FormCreate(Sender: TObject);
begin
   UxTheme.SetWindowTheme(DateTimePicker1.Handle, '', '');
   DateTimePicker1.Color := clLime;
end;

But that just made it angry:

enter image description here

How do i change the color of a DateTimePicker?

I was going to settle for patching the VCL:

procedure TDateTimePicker.CreateWnd;
var
  LChecked: Boolean;
begin
  LChecked := FChecked;
  inherited CreateWnd;
  SetChecked(LChecked);
  if Length(FFormat) > 0 then
     DateTime_SetFormat(Handle, FFormat);

  //20140911 Fix the .Color property not working
  if Self.HandleAllocated then
     Winapi.UxTheme.SetWindowTheme(Self.Handle, '', '');
end;

But disabling the theme of the window doesn't do it.

Bonus Chatter

You can change the color of a version 6 TComboBox with Theme Styles still applied to it:

enter image description here

So it's not a fundamental limitation of common controls version 6 or visual styles.

Duplicate?

i think not.

  • that question deals with how to make a DateTime picker honor the active Delphi style
  • this question deals with hot to make a DateTime picker not honor the active Delphi style

And the answers in those questions do not let you change the color; which is what i need to do.

It's even more ridiculous to suggest that those answers apply, since Style Hooks only do anything if you are using a non-standard (Delphi) style.

Annmarie answered 11/9, 2014 at 15:43 Comment(3)
Style properties for TDateTimePickerKeyser
So it's not a fundamental limitation of common controls version 6 or visual styles. Yes it is. Many themed controls are this way.Stouffer
If you read carefully the accepted answer you will find the solution ... you must intercept the WM_PAINT and WM_ERASEBKGND messages and wrote your own code to paint the control.Pyorrhea
S
2

How do I change the color of a TDateTimePicker?

For v6 comctl32 you cannot.

So it's not a fundamental limitation of common controls version 6 or visual styles.

Well, yes it is. Yes, v6 comctl32 allows user specified color for combo boxes. But not for date time pickers?

In order to get the result you desire you need to take over painting the control, by disabling themes for the control and handling both WM_ERASEBKGND and WM_PAINT. As Rodrigo demonstrates here: Style properties for TDateTimePicker. This is not a whole load of fun to be honest. You need to paint the entire control.

This is something you'll need to get used to with v6 comctl32. It really wants to be in charge of the theme and style of the controls. If you want to vary from that then you have much less freedom than you had in olden times.

Stouffer answered 11/9, 2014 at 18:4 Comment(0)
C
0

Use TJvDatePickerEdit from JVCL library. It can display the defined background color, even when you're using themes.

Colquitt answered 1/5, 2015 at 16:51 Comment(1)
That TJvDatePickerEdit is a cool control, but it doesn't respect the Display | Make everything bigger values.Plate

© 2022 - 2024 — McMap. All rights reserved.