How to change color of TDateTimePicker?

早过忘川 提交于 2020-01-04 01:38:50

问题


How do i change the color of a TDateTimePicker?

A Date and Time Picker can have a color:

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:

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:

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:

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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/25791795/how-to-change-color-of-tdatetimepicker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!