1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
| {* --- TSLTMessageDlg.InputDateTime -----------------------------------------------------
TSLTMessageDlg.InputDateTime fonctionne comme un InputQuery avec un champ de saisie de type TDateTimePicker
@param Msg Chaine à Afficher
@param Value Valeur affichée à l'ouverture du Dialogue et contiendra la valeur acceptée par l'utilisateur
@param AKind Indique si l'on affiche une sélection de Date (par défaut), une selection de l'Heure ou une double sélection incluant Date ET Heure
@return Booléen InputDateTime renvoie true si l'utilisateur choisit OK et false si l'utilisateur choisit Annuler ou appuie sur Echap.
------------------------------------------------------------------------------ }
class function TSLTMessageDlg.InputDateTime(const Msg: string; var Value: TDateTime; AKind: TSLTTInputDateTimeKind = idtkDate): Boolean;
function GetAveCharSize(AFont: TFont): TSize;
begin
Result := TFontSLTToolHelp.GetTextSize(Msg, AFont);
Result.Width := Result.Width div Max(Length(Msg), 1); // Average !
end;
const
mcHorzMargin = 8;
mcVertMargin = 8;
mcHorzSpacing = 10;
var
MsgForm: TForm;
MsgLabel: TComponent;
DialogUnits: TSize;
InputWidth, InputHeight: Integer;
HorzMargin, VertMargin, HorzSpacing, HorzOffset, VertOffset: Integer;
DatePicker, TimePicker: TDateTimePicker;
IndexControls: Integer;
XY: TPoint;
begin
Result := False;
MsgForm := TSLTMessageDlg.CreateMessageDialog(Msg, mtConfirmation, mbOKCancel);
with MsgForm do
try
if not (ContainsStr(Msg, #13) or ContainsStr(Msg, #10)) then
Caption := Msg;
MsgLabel := FindComponent('Message');
if MsgLabel is TLabel then
begin
DialogUnits := GetAveCharSize(Font);
HorzMargin := MulDiv(mcHorzMargin, DialogUnits.Width, 4);
VertMargin := MulDiv(mcVertMargin, DialogUnits.Height, 8);
HorzSpacing := MulDiv(mcHorzSpacing, DialogUnits.Width, 4);
InputHeight := 0;
InputWidth := 0;
if (AKind = idtkDate) or (AKind = idtkDateTime) then
begin
DatePicker := TDateTimePicker.Create(MsgForm);
DatePicker.Parent := MsgForm;
DatePicker.Kind := dtkDate;
DatePicker.Visible := True;
InputHeight := DatePicker.Height;
DatePicker.Top := TLabel(MsgLabel).Top + TLabel(MsgLabel).Height + VertMargin + VertMargin div 2;
DatePicker.Width := TFontSLTToolHelp.GetTextWidth(DateToStr(Value), DatePicker.Font) + HorzSpacing + InputHeight*2;
InputWidth := DatePicker.Width;
DatePicker.Left := (ClientWidth - DatePicker.Width) div 2 + HorzMargin;
DatePicker.Date := DateOf(Value);
ActiveControl := DatePicker;
end
else
DatePicker := nil;
if (AKind = idtkTime) or (AKind = idtkDateTime) then
begin
TimePicker := TDateTimePicker.Create(MsgForm);
TimePicker.Parent := MsgForm;
TimePicker.Kind := dtkTime;
TimePicker.Visible := True;
InputHeight := TimePicker.Height;
TimePicker.Width := TFontSLTToolHelp.GetTextWidth(TimeToStr(Value), TimePicker.Font) + HorzSpacing + InputHeight*2;
if Assigned(DatePicker) then
begin
TimePicker.Top := DatePicker.Top;
DatePicker.Left := (ClientWidth - TimePicker.Width - HorzSpacing - DatePicker.Width) div 2 + HorzSpacing;
TimePicker.Left := DatePicker.Left + DatePicker.Width + HorzSpacing;
InputWidth := DatePicker.Width + HorzSpacing + TimePicker.Width;
end
else
begin
TimePicker.Top := TLabel(MsgLabel).Top + TLabel(MsgLabel).Height + VertMargin + VertMargin div 2;
TimePicker.Left := (ClientWidth - TimePicker.Width) div 2 + HorzSpacing;
InputWidth := TimePicker.Width;
ActiveControl := TimePicker;
end;
TimePicker.Time := TimeOf(Value);
end
else
TimePicker := nil;
HorzOffset := Max(InputWidth - TLabel(MsgLabel).Width, 0);
VertOffset := VertMargin + InputHeight;
for IndexControls := 0 to ControlCount - 1 do
begin
if Controls[IndexControls] is TButton then
begin
with TButton(Controls[IndexControls]) do
begin
Top := Top + VertOffset;
Left := Left + HorzOffset div 2;
end;
end;
end;
Height := Height + VertOffset;
Width := Width + HorzOffset;
if HorzOffset > 0 then
begin
if Assigned(DatePicker) then
DatePicker.Left := DatePicker.Left + HorzOffset div 2;
if Assigned(TimePicker) then
TimePicker.Left := TimePicker.Left + HorzOffset div 2;
end;
XY := Mouse.CursorPos;
XY.X := XY.X - Width div 2;
XY.Y := XY.Y - Height div 2;
if XY.X < Screen.WorkAreaLeft then
XY.X := Screen.WorkAreaLeft;
if XY.Y < Screen.WorkAreaTop then
XY.Y := Screen.WorkAreaTop;
if XY.X + Width > Screen.WorkAreaWidth then
XY.X := Screen.WorkAreaWidth - Width;
if XY.Y + Height > Screen.WorkAreaHeight then
XY.Y := Screen.WorkAreaHeight - Height;
Left := XY.X;
Top := XY.Y;
if ShowModal() = mrOk then
begin
Result := True;
if (AKind = idtkDate) or (AKind = idtkDateTime) then
Value := DateOf(DatePicker.Date)
else
Value := 0;
if (AKind = idtkTime) or (AKind = idtkDateTime) then
Value := Value + TimeOf(DatePicker.Time);
end;
end
else
Abort;
finally
Free;
end;
end; |
Partager