问题描述
当用户单击 DateTimePicker 并设置为下拉日历时,我希望各种日期(通过我创建的代码选择)以某种方式突出显示 - 彩色背景、粗体、彩色字体,这无关紧要.我只想标记某些日期....我该怎么做?
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?
推荐答案
是的,您可以这样做.首先,您必须初始化控件:
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);
(使用 CommCtrl
).
然后您只需响应 MCN_GETDAYSTATE
通知.您可以创建自己的 TDateTimePicker
后代,也可以使用拦截器类".
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.
然后你想使用位域
Month Bitfield
0 00000000000000000000000000000000
1 01001001001001001001001001001001
2 00000000000000000000000000000000
哪些是
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;
这篇关于您如何以编程方式在 Delphi 的 TDateTimePicker 日历中标记日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!