如何在TMonthCalendar中加粗月份的天数?

问题描述 投票:3回答:1

我有一个使用日历的日志,我想加粗记录信息的日期。我将它们放在3D数组TDiaryLog = array[1900..2399] of array[1..12] of array[1..31] of POneDay;中。但是在OnGetMonthInfo中,当我必须用粗体的日期来建立列表时,它只给我Month,而不是年份。如果没有年份,我应该如何知道必须经过哪个月份呢?当12月在日历中显示为当前月份时,从明年1月起会有几天显示!

procedure TMainForm.CalendarGetMonthInfo(Sender: TObject; Month: Cardinal;
  var MonthBoldInfo: Cardinal);
begin
end;
delphi calendar delphi-2009
1个回答
3
投票

[我制作了一个新组件,在其中我截获了MCN_GETDAYSTATE消息,我也从消息信息中提取了年份...一直存在,但是Delphi认为年份没有用。

  TOnGetMonthInfoExEvent = procedure(Sender: TObject; Year, Month: Word;
    var MonthBoldInfo: LongWord) of object;

  TNewMonthCalendar = class(TMonthCalendar)
  private
    FOnGetMonthInfoEx: TOnGetMonthInfoExEvent;
    procedure CNNotify(var Msg: TWMNotifyMC); message CN_NOTIFY;
  published
    property OnGetMonthInfoEx: TOnGetMonthInfoExEvent read FOnGetMonthInfoEx write FOnGetMonthInfoEx;
  end;

procedure TNewMonthCalendar.CNNotify(var Msg: TWMNotifyMC);
var
  I: Integer;
  Month, Year: Word;
  DS: PNMDayState;
  CurState: PMonthDayState;
begin
 if (Msg.NMHdr.code = MCN_GETDAYSTATE) and Assigned(FOnGetMonthInfoEx) then begin
  DS:= Msg.NMDayState;
  FillChar(DS.prgDayState^, DS.cDayState * SizeOf(TMonthDayState), 0);
  CurState:= DS.prgDayState;
  for I:= 0 to DS.cDayState - 1 do begin
   Year:= DS.stStart.wYear;
   Month:= DS.stStart.wMonth + I;
   if Month > 12 then begin Inc(Year); Dec(Month, 12); end;
   FOnGetMonthInfoEx(Self, Year, Month, CurState^);
   Inc(CurState);
  end;
 end
 else inherited;
end;

Bonus

而且,作为奖励,您需要执行此操作来更新对当前月份视图的粗体信息所做的更改...,因为它不适用于Invalidate

procedure TNewMonthCalendar.RefreshDayState;
var N: Cardinal;
    Range: array[0..1] of TSystemTime;
    Year, Month: Word;
    States: array of TMonthDayState;
    I: Integer;
begin
 if not Assigned(FOnGetMonthInfoEx) then Exit;
 N:= SendMessage(Handle, MCM_GETMONTHRANGE, GMR_DAYSTATE, LPARAM(@Range));
 Year:= Range[0].wYear;
 Month:= Range[0].wMonth;
 SetLength(States, N);
 FillChar(States[0], N * SizeOf(TMonthDayState), 0);
 for I:= 0 to N-1 do begin
  FOnGetMonthInfoEx(Self, Year, Month, States[I]);
  Inc(Month);
  if Month > 12 then
   begin Dec(Month, 12); Inc(Year); end;
 end;
 SendMessage(Handle, MCM_SETDAYSTATE, N, LPARAM(@States[0]));
end;
© www.soinside.com 2019 - 2024. All rights reserved.