📄 pickdate.pas
字号:
begin
if not (csDesigning in ComponentState) then begin
FCalendar.CalendarDate := StrToDateFmtDef(DefDateFormat(FFourDigitYear),
Value, SysUtils.Date);
CalendarChange(nil);
end;
end;
{$ENDIF}
procedure TPopupCalendar.PrevYearBtnClick(Sender: TObject);
begin
FCalendar.PrevYear;
end;
procedure TPopupCalendar.NextYearBtnClick(Sender: TObject);
begin
FCalendar.NextYear;
end;
procedure TPopupCalendar.PrevMonthBtnClick(Sender: TObject);
begin
FCalendar.PrevMonth;
end;
procedure TPopupCalendar.NextMonthBtnClick(Sender: TObject);
begin
FCalendar.NextMonth;
end;
procedure TPopupCalendar.CalendarChange(Sender: TObject);
begin
FTitleLabel.Caption := FormatDateTime('MMMM, YYYY', FCalendar.CalendarDate);
end;
{ TSelectDateDlg }
type
TSelectDateDlg = class(TForm)
Calendar: TRxCalendar;
TitleLabel: TLabel;
procedure PrevMonthBtnClick(Sender: TObject);
procedure NextMonthBtnClick(Sender: TObject);
procedure PrevYearBtnClick(Sender: TObject);
procedure NextYearBtnClick(Sender: TObject);
procedure CalendarChange(Sender: TObject);
procedure CalendarDblClick(Sender: TObject);
procedure TopPanelDblClick(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
{ Private declarations }
FBtns: array[0..3] of TRxSpeedButton;
procedure SetDate(Date: TDateTime);
function GetDate: TDateTime;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
property Date: TDateTime read GetDate write SetDate;
end;
constructor TSelectDateDlg.Create(AOwner: TComponent);
var
Control: TWinControl;
begin
{$IFDEF CBUILDER}
inherited CreateNew(AOwner, 0);
{$ELSE}
inherited CreateNew(AOwner);
{$ENDIF}
Caption := LoadStr(SDateDlgTitle);
{$IFDEF WIN32}
BorderStyle := bsToolWindow;
{$ELSE}
BorderStyle := bsDialog;
{$ENDIF}
BorderIcons := [biSystemMenu];
ClientHeight := 154;
ClientWidth := 222;
FontSetDefault(Font);
Color := clBtnFace;
Position := poScreenCenter;
ShowHint := True;
KeyPreview := True;
Control := TPanel.Create(Self);
with Control as TPanel do begin
Parent := Self;
SetBounds(0, 0, 222, 22);
Align := alTop;
BevelInner := bvLowered;
ParentColor := True;
ParentFont := True;
end;
TitleLabel := TLabel.Create(Self);
with TitleLabel do begin
Parent := Control;
SetBounds(35, 4, 152, 14);
Alignment := taCenter;
AutoSize := False;
Caption := '';
ParentFont := True;
Font.Color := clBlue;
Font.Style := [fsBold];
Transparent := True;
OnDblClick := TopPanelDblClick;
end;
FBtns[0] := TRxTimerSpeedButton.Create(Self);
with FBtns[0] do begin
Parent := Control;
SetBounds(3, 3, 16, 16);
Glyph.Handle := LoadBitmap(hInstance, SBtnGlyphs[0]);
OnClick := PrevYearBtnClick;
Hint := LoadStr(SPrevYear);
end;
FBtns[1] := TRxTimerSpeedButton.Create(Self);
with FBtns[1] do begin
Parent := Control;
SetBounds(18, 3, 16, 16);
Glyph.Handle := LoadBitmap(hInstance, SBtnGlyphs[1]);
OnClick := PrevMonthBtnClick;
Hint := LoadStr(SPrevMonth);
end;
FBtns[2] := TRxTimerSpeedButton.Create(Self);
with FBtns[2] do begin
Parent := Control;
SetBounds(188, 3, 16, 16);
Glyph.Handle := LoadBitmap(hInstance, SBtnGlyphs[2]);
OnClick := NextMonthBtnClick;
Hint := LoadStr(SNextMonth);
end;
FBtns[3] := TRxTimerSpeedButton.Create(Self);
with FBtns[3] do begin
Parent := Control;
SetBounds(203, 3, 16, 16);
Glyph.Handle := LoadBitmap(hInstance, SBtnGlyphs[3]);
OnClick := NextYearBtnClick;
Hint := LoadStr(SNextYear);
end;
Control := TPanel.Create(Self);
with Control as TPanel do begin
Parent := Self;
SetBounds(0, 133, 222, 21);
Align := alBottom;
BevelInner := bvNone;
BevelOuter := bvNone;
ParentFont := True;
ParentColor := True;
end;
with TButton.Create(Self) do begin
Parent := Control;
SetBounds(0, 0, 112, 21);
Caption := ResStr(SOKButton);
ModalResult := mrOk;
end;
with TButton.Create(Self) do begin
Parent := Control;
SetBounds(111, 0, 111, 21);
Caption := ResStr(SCancelButton);
ModalResult := mrCancel;
Cancel := True;
end;
Control := TPanel.Create(Self);
with Control as TPanel do begin
Parent := Self;
SetBounds(0, 22, 222, 111);
Align := alClient;
BevelInner := bvLowered;
ParentFont := True;
ParentColor := True;
end;
Calendar := TRxCalendar.Create(Self);
with Calendar do begin
Parent := Control;
Align := alClient;
ParentFont := True;
SetBounds(2, 2, 218, 113);
Color := clWhite;
TabOrder := 0;
UseCurrentDate := False;
OnChange := CalendarChange;
OnDblClick := CalendarDblClick;
end;
OnKeyDown := FormKeyDown;
Calendar.CalendarDate := Trunc(Now);
ActiveControl := Calendar;
end;
procedure TSelectDateDlg.SetDate(Date: TDateTime);
begin
if Date = NullDate then Date := SysUtils.Date;
try
Calendar.CalendarDate := Date;
CalendarChange(nil);
except
Calendar.CalendarDate := SysUtils.Date;
end;
end;
function TSelectDateDlg.GetDate: TDateTime;
begin
Result := Calendar.CalendarDate;
end;
procedure TSelectDateDlg.TopPanelDblClick(Sender: TObject);
begin
SetDate(Trunc(Now));
end;
procedure TSelectDateDlg.PrevYearBtnClick(Sender: TObject);
begin
Calendar.PrevYear;
end;
procedure TSelectDateDlg.NextYearBtnClick(Sender: TObject);
begin
Calendar.NextYear;
end;
procedure TSelectDateDlg.PrevMonthBtnClick(Sender: TObject);
begin
Calendar.PrevMonth;
end;
procedure TSelectDateDlg.NextMonthBtnClick(Sender: TObject);
begin
Calendar.NextMonth;
end;
procedure TSelectDateDlg.CalendarChange(Sender: TObject);
begin
TitleLabel.Caption := FormatDateTime('MMMM, YYYY', Calendar.CalendarDate);
end;
procedure TSelectDateDlg.CalendarDblClick(Sender: TObject);
begin
ModalResult := mrOK;
end;
procedure TSelectDateDlg.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
case Key of
VK_RETURN: ModalResult := mrOK;
VK_ESCAPE: ModalResult := mrCancel;
VK_NEXT:
begin
if ssCtrl in Shift then Calendar.NextYear
else Calendar.NextMonth;
TitleLabel.Update;
end;
VK_PRIOR:
begin
if ssCtrl in Shift then Calendar.PrevYear
else Calendar.PrevMonth;
TitleLabel.Update;
end;
VK_TAB:
begin
if Shift = [ssShift] then Calendar.PrevMonth
else Calendar.NextMonth;
TitleLabel.Update;
end;
end; {case}
end;
{ SelectDate routines }
function CreateDateDialog(const DlgCaption: TCaption): TSelectDateDlg;
begin
Result := TSelectDateDlg.Create(Application);
try
if DlgCaption <> '' then Result.Caption := DlgCaption;
if Screen.PixelsPerInch <> 96 then begin { scale to screen res }
Result.ScaleBy(Screen.PixelsPerInch, 96);
{ The ScaleBy method does not scale the font well, so set the
font back to the original info. }
Result.Calendar.ParentFont := True;
FontSetDefault(Result.Font);
Result.Left := (Screen.Width div 2) - (Result.Width div 2);
Result.Top := (Screen.Height div 2) - (Result.Height div 2);
end;
except
Result.Free;
raise;
end;
end;
function PopupDate(var Date: TDateTime; Edit: TWinControl): Boolean;
var
D: TSelectDateDlg;
P: TPoint;
W, H, X, Y: Integer;
begin
Result := False;
D := CreateDateDialog('');
try
D.BorderIcons := [];
D.HandleNeeded;
D.Position := poDesigned;
W := D.Width;
H := D.Height;
P := (Edit.ClientOrigin);
Y := P.Y + Edit.Height - 1;
if (Y + H) > Screen.Height then Y := P.Y - H + 1;
if Y < 0 then Y := P.Y + Edit.Height - 1;
X := (P.X + Edit.Width) - W;
if X < 0 then X := P.X;
D.Left := X;
D.Top := Y;
D.Date := Date;
if D.ShowModal = mrOk then begin
Date := D.Date;
Result := True;
end;
finally
D.Free;
end;
end;
function SelectDate(var Date: TDateTime; const DlgCaption: TCaption;
AStartOfWeek: TDayOfWeekName; AWeekends: TDaysOfWeek;
AWeekendColor: TColor; BtnHints: TStrings): Boolean;
var
D: TSelectDateDlg;
I: Integer;
begin
Result := False;
D := CreateDateDialog(DlgCaption);
try
D.Date := Date;
with D.Calendar do begin
StartOfWeek := AStartOfWeek;
Weekends := AWeekends;
WeekendColor := AWeekendColor;
end;
if (BtnHints <> nil) then
for I := 0 to Min(BtnHints.Count - 1, 3) do begin
if BtnHints[I] <> '' then
D.FBtns[I].Hint := BtnHints[I];
end;
if D.ShowModal = mrOk then begin
Date := D.Date;
Result := True;
end;
finally
D.Free;
end;
end;
function SelectDateStr(var StrDate: string; const DlgCaption: TCaption;
AStartOfWeek: TDayOfWeekName; AWeekends: TDaysOfWeek;
AWeekendColor: TColor; BtnHints: TStrings): Boolean;
var
DateValue: TDateTime;
begin
if StrDate <> '' then begin
try
DateValue := StrToDateFmt(ShortDateFormat, StrDate);
except
DateValue := Date;
end;
end
else DateValue := Date;
Result := SelectDate(DateValue, DlgCaption, AStartOfWeek, AWeekends,
AWeekendColor, BtnHints);
if Result then StrDate := FormatDateTime(ShortDateFormat, DateValue);
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -