编码笔记

导航

DateTimePicker如何与Delphi自带Style同步

Delphi 的 DateTimePicker 组件有一个CalColors属性,可以设置 DropDown 打开的日历节目的风格。但如果不使用 Delphi 自带的 Style,在这里设置属性看不到期望的效果。

而使用了 delphi 自带的style,效果又存在瑕疵——日历面板大小有问题。
如果把自带 style 的 client 项关闭,大小倒是对了,之前设置的MonthBackColor属性在边框上也体现出来了,但是和窗体的风格又不统一了。
网上一搜,Stack Overflow 给出了方案——去掉自动绘制 style,去提取 style 的相关元素来设置 CalColors 属性。
尝试一下,效果基本能接受了。记在这里备查。
http://stackoverflow.com/questions/10335310/style-properties-for-tdatetimepicker
 1 uses
 2   Winapi.CommCtrl,
 3   Vcl.Styles,
 4   Vcl.Themes,
 5   uxTheme;
 6 
 7 Procedure SetVclStylesColorsCalendar( DateTimePicker: TDateTimePicker);
 8 Var
 9   LTextColor, LBackColor : TColor;
10 begin
11    uxTheme.SetWindowTheme(DateTimePicker.Handle, '', '');//disable themes in the calendar
12    //get the vcl styles colors
13    LTextColor:=StyleServices.GetSystemColor(clWindowText);
14    LBackColor:=StyleServices.GetSystemColor(clWindow);
15 
16    DateTimePicker.Color:=LBackColor;
17    //set the colors of the calendar
18    DateTimePicker.CalColors.BackColor:=LBackColor;
19    DateTimePicker.CalColors.MonthBackColor:=LBackColor;
20    DateTimePicker.CalColors.TextColor:=LTextColor;
21    DateTimePicker.CalColors.TitleBackColor:=LBackColor;
22    DateTimePicker.CalColors.TitleTextColor:=LTextColor;
23    DateTimePicker.CalColors.TrailingTextColor:=LTextColor;
24 end;
25 
26 procedure TForm2.DateTimePicker1DropDown(Sender: TObject);
27 var
28   hwnd: WinAPi.Windows.HWND;
29 begin
30   hwnd := SendMessage(TDateTimePicker(Sender).Handle, DTM_GETMONTHCAL, 0,0);
31   uxTheme.SetWindowTheme(hwnd, '', '');//disable themes in the drop down window
32 end;
33 procedure TForm2.FormCreate(Sender: TObject);
34 begin
35   SetVclStylesColorsCalendar(DateTimePicker1);
36 end;

 
 

 

posted on 2014-10-24 00:49  封三郎  阅读(1802)  评论(0编辑  收藏  举报