在Delphi的TDateTimePicker日历中如何以编程方式标记日期? [英] How do you programmatically mark a date in Delphi's TDateTimePicker's calendar?

查看:463
本文介绍了在Delphi的TDateTimePicker日历中如何以编程方式标记日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户单击一个DateTimePicker时,设置为下拉一个日历,我希望通过某种方式突出显示各种日期(通过我创建的代码选择) - 彩色背景,粗体字体,彩色字体,没关系。我只想要确定某些日期。 ...如何做到这一点?



谢谢,一如既往!

解决方案>

是的,你可以这样做。首先,您必须初始化控件:

  const 
DTM_GETMCSTYLE = DTM_FIRST + 12;
DTM_SETMCSTYLE = DTM_FIRST + 11;
...
SendMessage(DateTimePicker1.Handle,
DTM_SETMCSTYLE,
0,
SendMessage(DateTimePicker1.Handle,DTM_GETMCSTYLE,0,0)或MCS_DAYSTATE);

使用CommCtrl )。 >

然后您只需回复 MCN_GETDAYSTATE 通知。您可以创建自己的 TDateTimePicker 后代,或者您可以使用拦截器类。

 键入
TDateTimePicker = class(ComCtrls.TDateTimePicker)
protected
procedure WndProc(var Message:TMessage);覆盖
结束

...

程序TDateTimePicker.WndProc(var Message:TMessage);
var
i:integer;
开始
继承;
case Message.Msg
WM_NOTIFY:
with PNMDayState(Message.LParam)^ do
如果nmhdr.code = MCN_GETDAYSTATE然后
begin
//第一个可见的日子是SystemTimeToDateTime(stStart);
// cDayState可能是三个,因为大多数时候三个月都是
//同时可见。当然,其中第二个是
//'当前显示月份'。
//每个月由DWORD(32位无符号整数)
// bitfield表示,其中0表示不粗体,1表示粗体。
//例如,以下代码将选择所有天:
for i:= 0 to cDayState - 1 do
PMonthDayState(Cardinal(prgDayState)+ i * sizeof(TMonthDayState))^ := $ FFFFFFFF;
结束
结束
结束

另一个例子:假设当前显示包含三个月,而您只想选择天在当前显示的月份,即在中间月份。假设您希望每三天被选中,从选定的日期开始。



然后,您要使用位域

  Month Bitfield 
0 00000000000000000000000000000000
1 01001001001001001001001001001001
2 0000000000000000000000000000000000

哪些是

  Month Bitfield 
0 $ 00000000
1 $ 49249249
2 $ 00000000

以十六进制。所以你要做的是/ /

  for i:= 0 to cDayState  -  1 do 
if i = 1 then
PMonthDayState(主键(prgDayState)+ i * sizeof(TMonthDayState))^:= $ 49249249
else
PMonthDayState(cardinal(prgDayState)+ i * sizeof(TMonthDayState))^:= $ 00000000;

屏幕截图http://privat.rejbrand.se/datetimepick.png


When users click a DateTimePicker, set to drop down a calendar, I would like various dates (chosen via code I create) to highlighted in some way--colored background, bold font, colored font, it doesn't matter. I just want certain dates to be marked. ... How would I do this?

Thanks, as always!

解决方案

Yes, you can do this. First, you have to initialize the control:

const
  DTM_GETMCSTYLE = DTM_FIRST + 12;
  DTM_SETMCSTYLE = DTM_FIRST + 11;
...
SendMessage(DateTimePicker1.Handle,
  DTM_SETMCSTYLE,
  0,
  SendMessage(DateTimePicker1.Handle, DTM_GETMCSTYLE, 0, 0) or MCS_DAYSTATE);

(uses CommCtrl).

Then you simply have to respond to the MCN_GETDAYSTATE notification. Either you can create your own TDateTimePicker descendant, or you can use an 'interceptor class'.

type
  TDateTimePicker = class(ComCtrls.TDateTimePicker)    
  protected
    procedure WndProc(var Message: TMessage); override;
  end;

  ...

procedure TDateTimePicker.WndProc(var Message: TMessage);
var
  i: integer;
begin
  inherited;
  case Message.Msg of
    WM_NOTIFY:
      with PNMDayState(Message.LParam)^ do
        if nmhdr.code = MCN_GETDAYSTATE then
        begin
          // The first visible day is SystemTimeToDateTime(stStart);
          // cDayState is probably three, because most often three months are
          // visible at the same time. Of course, the second of these is the
          // 'currently displayed month'.
          // Each month is represented by a DWORD (32-bit unsigned integer)
          // bitfield, where 0 means not bold, and 1 means bold.
          // For instance, the following code will select all days:
          for i := 0 to cDayState - 1 do
            PMonthDayState(Cardinal(prgDayState) + i*sizeof(TMonthDayState))^ := $FFFFFFFF;
        end;
  end;
end;

Another example: Assume that the current display consists of three months, and that you only want to select days in the 'currently displayed month', that is, in the middle month. Assume that you want every third day to be selected, starting with a selected day.

Then you want to use the bitfields

Month  Bitfield
0      00000000000000000000000000000000
1      01001001001001001001001001001001
2      00000000000000000000000000000000

which are

Month  Bitfield
0      $00000000
1      $49249249
2      $00000000

in hexadecimal. So you do

for i := 0 to cDayState - 1 do
  if i = 1 then
    PMonthDayState(cardinal(prgDayState) + i*sizeof(TMonthDayState))^ := $49249249
  else
    PMonthDayState(cardinal(prgDayState) + i*sizeof(TMonthDayState))^ := $00000000;

Screenshot http://privat.rejbrand.se/datetimepick.png

这篇关于在Delphi的TDateTimePicker日历中如何以编程方式标记日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆