📄 monthcal.pas
字号:
{*******************************************************}
{ }
{ EhLib v1.54 }
{ TMonthCalendar component }
{ }
{ Part of ComCtrls unit from Delphi 4 }
{ }
{*******************************************************}
unit MonthCal;
interface
uses Messages, Windows, SysUtils, CommCtrl, Classes, Controls, Forms,
Menus, Graphics, StdCtrls, RichEdit, ToolWin;
resourcestring
sDateTimeMax = 'Date exceeds maximum of %s';
sDateTimeMin = 'Date is less than minimum of %s';
sNeedAllowNone = 'You must be in ShowCheckbox mode to set to this date';
sFailSetCalDateTime = 'Failed to set calendar date or time';
sFailSetCalMaxSelRange = 'Failed to set maximum selection range';
sFailSetCalMinMaxRange = 'Failed to set calendar min/max range';
sCalRangeNeedsMultiSelect = 'Date range can only be used in multiselect mode';
sFailsetCalSelRange = 'Failed to set calendar selected range';
sInvalidComCtl32 = 'This control requires version 4.70 or greater of COMCTL32.DLL';
type
{ Calendar common control support }
TCommonCalendar = class;
ECommonCalendarError = class(Exception);
TMonthCalColors = class(TPersistent)
private
Owner: TCommonCalendar;
FBackColor: TColor;
FTextColor: TColor;
FTitleBackColor: TColor;
FTitleTextColor: TColor;
FMonthBackColor: TColor;
FTrailingTextColor: TColor;
procedure SetColor(Index: Integer; Value: TColor);
procedure SetAllColors;
public
constructor Create(AOwner: TCommonCalendar);
procedure Assign(Source: TPersistent); override;
published
property BackColor: TColor index 0 read FBackColor write SetColor default clWindow;
property TextColor: TColor index 1 read FTextColor write SetColor default clWindowText;
property TitleBackColor: TColor index 2 read FTitleBackColor write SetColor default clActiveCaption;
property TitleTextColor: TColor index 3 read FTitleTextColor write SetColor default clWhite;
property MonthBackColor: TColor index 4 read FMonthBackColor write SetColor default clWhite;
property TrailingTextColor: TColor index 5 read FTrailingTextColor
write SetColor default clInactiveCaptionText;
end;
TCalDayOfWeek = (dowMonday, dowTuesday, dowWednesday, dowThursday,
dowFriday, dowSaturday, dowSunday, dowLocaleDefault);
TOnGetMonthInfoEvent = procedure(Sender: TObject; Month: DWORD;
var MonthBoldInfo: DWORD) of object;
TDateTimeColors = TMonthCalColors; // for backward compatibility
TCommonCalendar = class(TWinControl)
private
FCalColors: TMonthCalColors;
FCalExceptionClass: ExceptClass;
FDateTime: TDateTime;
FEndDate: TDate;
FFirstDayOfWeek: TCalDayOfWeek;
FMaxDate: TDate;
FMaxSelectRange: Integer;
FMinDate: TDate;
FMonthDelta: Integer;
FMultiSelect: Boolean;
FShowToday: Boolean;
FShowTodayCircle: Boolean;
FWeekNumbers: Boolean;
FOnGetMonthInfo: TOnGetMonthInfoEvent;
function DoStoreEndDate: Boolean;
function DoStoreMaxDate: Boolean;
function DoStoreMinDate: Boolean;
function GetDate: TDate;
procedure SetCalColors(Value: TMonthCalColors);
procedure SetDate(Value: TDate);
procedure SetDateTime(Value: TDateTime);
procedure SetEndDate(Value: TDate);
procedure SetFirstDayOfWeek(Value: TCalDayOfWeek);
procedure SetMaxDate(Value: TDate);
procedure SetMaxSelectRange(Value: Integer);
procedure SetMinDate(Value: TDate);
procedure SetMonthDelta(Value: Integer);
procedure SetMultiSelect(Value: Boolean);
procedure SetRange(MinVal, MaxVal: TDate);
procedure SetSelectedRange(Date, EndDate: TDate);
procedure SetShowToday(Value: Boolean);
procedure SetShowTodayCircle(Value: Boolean);
procedure SetWeekNumbers(Value: Boolean);
protected
procedure CheckEmptyDate; virtual;
procedure CheckValidDate(Value: TDate); virtual;
procedure CreateWnd; override;
function GetCalendarHandle: HWND; virtual; abstract;
function GetCalStyles: DWORD; virtual;
function MsgSetCalColors(ColorIndex: Integer; ColorValue: TColor): Boolean; virtual; abstract;
function MsgSetDateTime(Value: TSystemTime): Boolean; virtual; abstract;
function MsgSetRange(Flags: Integer; SysTime: PSystemTime): Boolean; virtual; abstract;
property CalColors: TMonthCalColors read FCalColors write SetCalColors;
property CalendarHandle: HWND read GetCalendarHandle;
property CalExceptionClass: ExceptClass read FCalExceptionClass write FCalExceptionClass;
property Date: TDate read GetDate write SetDate;
property DateTime: TDateTime read FDateTime write SetDateTime;
property EndDate: TDate read FEndDate write SetEndDate stored DoStoreEndDate;
property FirstDayOfWeek: TCalDayOfWeek read FFirstDayOfWeek write SetFirstDayOfWeek
default dowLocaleDefault;
property MaxDate: TDate read FMaxDate write SetMaxDate stored DoStoreMaxDate;
property MaxSelectRange: Integer read FMaxSelectRange write SetMaxSelectRange default 31;
property MinDate: TDate read FMinDate write SetMinDate stored DoStoreMinDate;
property MonthDelta: Integer read FMonthDelta write SetMonthDelta default 1;
property MultiSelect: Boolean read FMultiSelect write SetMultiSelect default False;
property ShowToday: Boolean read FShowToday write SetShowToday default True;
property ShowTodayCircle: Boolean read FShowTodayCircle write
SetShowTodayCircle default True;
property WeekNumbers: Boolean read FWeekNumbers write SetWeekNumbers default False;
property OnGetMonthInfo: TOnGetMonthInfoEvent read FOnGetMonthInfo write FOnGetMonthInfo;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure BoldDays(Days: array of DWORD; var MonthBoldInfo: DWORD);
end;
{ TMonthCalendar }
EMonthCalError = class(ECommonCalendarError);
TMonthCalendar = class(TCommonCalendar)
private
procedure CMFontChanged(var Message: TMessage); message CM_FONTCHANGED;
procedure CNNotify(var Message: TWMNotify); message CN_NOTIFY;
protected
function CanAutoSize(var NewWidth, NewHeight: Integer): Boolean;
procedure ConstrainedResize(var MinWidth, MinHeight, MaxWidth,
MaxHeight: Integer);
procedure CreateParams(var Params: TCreateParams); override;
function GetCalendarHandle: HWND; override;
function MsgSetCalColors(ColorIndex: Integer; ColorValue: TColor): Boolean; override;
function MsgSetDateTime(Value: TSystemTime): Boolean; override;
function MsgSetRange(Flags: Integer; SysTime: PSystemTime): Boolean; override;
public
constructor Create(AOwner: TComponent); override;
published
property Align;
// property Anchors;
// property AutoSize;
// property BorderWidth;
// property BiDiMode;
property CalColors;
// property Constraints;
property MultiSelect; // must be before date stuff
property Date;
property DragCursor;
// property DragKind;
property DragMode;
property Enabled;
property EndDate;
property FirstDayOfWeek;
property Font;
property ImeMode;
property ImeName;
property MaxDate;
property MaxSelectRange;
property MinDate;
// property ParentBiDiMode;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ShowHint;
property ShowToday;
property ShowTodayCircle;
property TabOrder;
property TabStop;
property Visible;
property WeekNumbers;
property OnClick;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
// property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnGetMonthInfo;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
// property OnStartDock;
property OnStartDrag;
end;
implementation
{ TMonthCalColors }
const
ColorIndex: array[0..5] of Integer = (MCSC_BACKGROUND, MCSC_TEXT,
MCSC_TITLEBK, MCSC_TITLETEXT, MCSC_MONTHBK, MCSC_TRAILINGTEXT);
constructor TMonthCalColors.Create(AOwner: TCommonCalendar);
begin
Owner := AOwner;
FBackColor := clWindow;
FTextColor := clWindowText;
FTitleBackColor := clActiveCaption;
FTitleTextColor := clWhite;
FMonthBackColor := clWhite;
FTrailingTextColor := clInactiveCaptionText;
end;
procedure TMonthCalColors.Assign(Source: TPersistent);
var
SourceName: string;
begin
if Source = nil then SourceName := 'nil'
else SourceName := Source.ClassName;
if (Source = nil) or not (Source is TMonthCalColors) then
raise EConvertError.CreateFmt('Cannot assign a %s to a %s', [SourceName, ClassName]);
FBackColor := TMonthCalColors(Source).BackColor;
FTextColor := TMonthCalColors(Source).TextColor;
FTitleBackColor := TMonthCalColors(Source).TitleBackColor;
FTitleTextColor := TMonthCalColors(Source).TitleTextColor;
FMonthBackColor := TMonthCalColors(Source).MonthBackColor;
FTrailingTextColor := TMonthCalColors(Source).TrailingTextColor;
end;
procedure TMonthCalColors.SetColor(Index: Integer; Value: TColor);
begin
case Index of
0: FBackColor := Value;
1: FTextColor := Value;
2: FTitleBackColor := Value;
3: FTitleTextColor := Value;
4: FMonthBackColor := Value;
5: FTrailingTextColor := Value;
end;
if Owner.HandleAllocated then
Owner.MsgSetCalColors(ColorIndex[Index], ColorToRGB(Value));
end;
procedure TMonthCalColors.SetAllColors;
begin
SetColor(0, FBackColor);
SetColor(1, FTextColor);
SetColor(2, FTitleBackColor);
SetColor(3, FTitleTextColor);
SetColor(4, FMonthBackColor);
SetColor(5, FTrailingTextColor);
end;
{ TCommonCalendar }
function InitCommonControl(CC: Integer): Boolean;
var
ICC: TInitCommonControlsEx;
begin
ICC.dwSize := SizeOf(TInitCommonControlsEx);
ICC.dwICC := CC;
Result := InitCommonControlsEx(ICC);
if not Result then InitCommonControls;
end;
procedure CheckCommonControl(CC: Integer);
begin
if not InitCommonControl(CC) then
raise EComponentError.Create(SInvalidComCtl32);
end;
constructor TCommonCalendar.Create(AOwner: TComponent);
begin
CheckCommonControl(ICC_DATE_CLASSES);
inherited Create(AOwner);
FShowToday := True;
FShowTodayCircle := True;
ControlStyle := [csOpaque, csClickEvents, csDoubleClicks, csReflector];
FCalColors := TDateTimeColors.Create(Self);
FDateTime := Now;
FFirstDayOfWeek := dowLocaleDefault;
FMaxSelectRange := 31;
FMonthDelta := 1;
end;
destructor TCommonCalendar.Destroy;
begin
inherited Destroy;
FCalColors.Free;
end;
procedure TCommonCalendar.BoldDays(Days: array of DWORD; var MonthBoldInfo: DWORD);
var
I: DWORD;
begin
MonthBoldInfo := 0;
for I := Low(Days) to High(Days) do
if (Days[I] > 0) and (Days[I] < 32) then
MonthBoldInfo := MonthBoldInfo or ($00000001 shl (Days[I] - 1));
end;
procedure TCommonCalendar.CheckEmptyDate;
begin
// do nothing
end;
procedure TCommonCalendar.CheckValidDate(Value: TDate);
begin
if (FMaxDate <> 0.0) and (Value > FMaxDate) then
raise CalExceptionClass.CreateFmt(sDateTimeMax, [DateToStr(FMaxDate)]);
if (FMinDate <> 0.0) and (Value < FMinDate) then
raise CalExceptionClass.CreateFmt(sDateTimeMin, [DateToStr(FMinDate)]);
end;
procedure TCommonCalendar.CreateWnd;
begin
inherited CreateWnd;
FCalColors.SetAllColors;
SetRange(FMinDate, FMaxDate);
SetMaxSelectRange(FMaxSelectRange);
SetMonthDelta(FMonthDelta);
SetFirstDayOfWeek(FFirstDayOfWeek);
if FMultiSelect then
SetSelectedRange(FDateTime, FEndDate)
else
SetDateTime(FDateTime);
end;
const
MCS_NOTODAYCIRCLE = $0008;
function TCommonCalendar.GetCalStyles: DWORD;
const
ShowTodayFlags: array[Boolean] of DWORD = (MCS_NOTODAY, 0);
ShowTodayCircleFlags: array[Boolean] of DWORD = (MCS_NOTODAYCIRCLE, 0);
WeekNumFlags: array[Boolean] of DWORD = (0, MCS_WEEKNUMBERS);
MultiSelFlags: array[Boolean] of DWORD = (0, MCS_MULTISELECT);
begin
Result := MCS_DAYSTATE or ShowTodayFlags[FShowToday] or
ShowTodayCircleFlags[FShowTodayCircle] or WeekNumFlags[FWeekNumbers] or
MultiSelFlags[FMultiSelect];
end;
function TCommonCalendar.DoStoreEndDate: Boolean;
begin
Result := FMultiSelect;
end;
function TCommonCalendar.DoStoreMaxDate: Boolean;
begin
Result := FMaxDate <> 0.0;
end;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -