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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
|
//------------------------------------------------------------------------------
(* SoLuTions is an Versatile Library for Delphi -
* -
* Version alternative publiée sur "www.developpez.net" -
* Post : "MessageBox ou MessageDlg ?" -
* Post Number : 2530570 -
* Post URL = "http://www.developpez.net/forums/d417066/environnements-developpement/delphi/debutant/messagebox-messagedlg/#post2530570"
* -
* Version alternative publiée sur "www.developpez.net" -
* Post : "Personnaliser les boutons du MessageDialog" -
* Post Number : 4662116 -
* Post URL = "http://www.developpez.net/forums/d811727/environnements-developpement/delphi/debutant/personnaliser-boutons-messagedialog/#post4662116"
* -
* Copyright ou © ou Copr. "SLT Solutions", (2006) -
* contributeur : ShaiLeTroll (2007) - Passage en Classe d'un code procédural -
* contributeur : ShaiLeTroll (2012) - Passage en Enregistrement avancé au lieu d'une classe
* contributeur : ShaiLeTroll (2012) - Renommage Fichier et Correction XE2 -
* contributeur : ShaiLeTroll (2012) - Documentation Insight -
* -
* ShaiLeTroll@gmail.com -
* -
* Ce logiciel est un programme informatique servant à aider les développeurs -
* Delphi avec une bibliothèque polyvalente, adaptable et fragmentable. -
* -
* Ce logiciel est régi par la licence CeCILL-C soumise au droit français et -
* respectant les principes de diffusion des logiciels libres. Vous pouvez -
* utiliser, modifier et/ou redistribuer ce programme sous les conditions -
* de la licence CeCILL-C telle que diffusée par le CEA, le CNRS et l'INRIA -
* sur le site "http://www.cecill.info". -
* -
* En contrepartie de l'accessibilité au code source et des droits de copie, -
* de modification et de redistribution accordés par cette licence, il n'est -
* offert aux utilisateurs qu'une garantie limitée. Pour les mêmes raisons, -
* seule une responsabilité restreinte pèse sur l'auteur du programme, le -
* titulaire des droits patrimoniaux et les concédants successifs. -
* -
* A cet égard l'attention de l'utilisateur est attirée sur les risques -
* associés au chargement, à l'utilisation, à la modification et/ou au -
* développement et à la reproduction du logiciel par l'utilisateur étant -
* donné sa spécificité de logiciel libre, qui peut le rendre complexe à -
* manipuler et qui le réserve donc à des développeurs et des professionnels -
* avertis possédant des connaissances informatiques approfondies. Les -
* utilisateurs sont donc invités à charger et tester l'adéquation du -
* logiciel à leurs besoins dans des conditions permettant d'assurer la -
* sécurité de leurs systèmes et ou de leurs données et, plus généralement, -
* à l'utiliser et l'exploiter dans les mêmes conditions de sécurité. -
* -
* Le fait que vous puissiez accéder à cet en-tête signifie que vous avez -
* pris connaissance de la licence CeCILL-C, et que vous en avez accepté les -
* termes. -
* -
*----------------------------------------------------------------------------*)
unit SLT.Controls.VCL.DialogsEx;
interface
uses System.Classes, System.Types, System.UITypes, System.Math,
VCL.Forms, Vcl.Controls, VCL.StdCtrls, Vcl.Buttons, VCL.Dialogs, VCL.Graphics,
Winapi.Windows;
type
/// <summary>Type de valeurs devant être récupérées par le dialogue fournit par la méthode TSLTMessageDlg.InputDateTime</summary>
TSLTTInputDateTimeKind = (idtkDate, idtkTime, idtkDateTime);
/// <summary>TSLTMessageDlg fournit une variante d'appel à MessageDlgCustomBtn et MessageDlgFont</summary>
TSLTMessageDlg = class(TObject)
/// <summary>DEFAULT permet de conserver le libellé d'origine parmi des libellés personnalisés</summary>
const DEFAULT = '';
/// <summary>TSLTMessageDlg.Show fonctionne comme MessageDlgPosHelpCustomBtn avec des libellés personnalisés pour chaque bouton</summary>
class function Show(const Msg: string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons; const ButtonStrings: array of String; HelpCtx: Longint = 0; X: Integer = -1; Y: Integer = -1; const HelpFileName: string = ''): Integer; overload; static;
/// <summary>TSLTMessageDlg.Show fonctionne comme MessageDlgPosHelpFont avec la Gestion de la Font pour la fenêtre et les Boutons</summary>
class function Show(const Msg: string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons; FontMsg: TFont; HelpCtx: Longint = 0; X: Integer = -1; Y: Integer = -1; const HelpFileName: string = ''): Integer; overload; static;
/// <summary>TSLTMessageDlg.Show fonctionne comme MessageDlgPosHelpFont avec la Gestion de la Font pour la fenêtre et les Boutons dont un bouton par défaut</summary>
class function Show(const Msg: string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons; DefaultButton: TMsgDlgBtn; FontMsg: TFont = nil; HelpCtx: Longint = 0; X: Integer = -1; Y: Integer = -1; const HelpFileName: string = ''): Integer; overload; static;
/// <summary>TSLTMessageDlg.InputDateTime fournit un dialogue proche d'un InputQuery avec un sélecteur de Date</summary>
class function InputDateTime(const Msg: string; var Value: TDateTime; AKind: TSLTTInputDateTimeKind = idtkDate): Boolean; static;
/// <summary>TSLTMessageDlg.InputPrinter fournit un dialogue proche d'un InputQuery avec un sélecteur d'imprimante bien plus simple que le TPrintDialog</summary>
class function InputPrinter(const Msg: string; var PrinterName: string): Boolean; static;
/// <summary>TSLTMessageDlg.InputCombo fournit un dialogue proche d'un InputQuery avec un sélecteur en liste déroulante</summary>
class function InputCombo(const Msg: string; ComboItems: TStrings; var ComboItemIndex: Integer): Boolean; static;
end;
/// <summary>MessageDlgCustomBtn fonctionne comme MessageDlg avec des libellés personnalisés pour chaque bouton</summary>
/// <param name="Msg">Chaine à Afficher</param>
/// <param name="DlgType">Type du Dialogue</param>
/// <param name="Buttons">Ensemble de boutons</param>
/// <param name="ButtonStrings">Fourni les libellés personnalisés pour chaque bouton</param>
/// <param name="HelpCtx">Contexte d'Aide</param>
/// <returns>Entier Résultat associé à chaque Bouton</returns>
function MessageDlgCustomBtn(const Msg: string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons; const ButtonStrings: array of String; HelpCtx: Longint = 0): Integer;
/// <summary>MessageDlgPosHelpCustomBtn fonctionne comme MessageDlgPosHelp avec des libellés personnalisés pour chaque bouton</summary>
/// <param name="Msg">Chaine à Afficher</param>
/// <param name="DlgType">Type du Dialogue</param>
/// <param name="Buttons">Ensemble de boutons</param>
/// <param name="ButtonStrings">Fourni les libellés personnalisés pour chaque bouton</param>
/// <param name="HelpCtx">Contexte d'Aide</param>
/// <param name="X">Position Horizontale du Message</param>
/// <param name="Y">Position Verticale du Message</param>
/// <param name="HelpFileName">Fichier d'aide</param>
/// <returns>Entier Résultat associé à chaque Bouton</returns>
function MessageDlgPosHelpCustomBtn(const Msg: string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons; const ButtonStrings: array of String; HelpCtx: Longint; X, Y: Integer; const HelpFileName: string): Integer;
/// <summary>MessageDlgFont fonctionne comme MessageDlg avec la Gestion de la Font pour la fenêtre et les Boutons</summary>
/// <param name="Msg">Chaine à Afficher</param>
/// <param name="DlgType">Type du Dialogue</param>
/// <param name="Buttons">Ensemble de boutons</param>
/// <param name="FontMsg">Font spécifique pour Fenêtre et Boutons</param>
/// <param name="HelpCtx">Contexte d'Aide</param>
/// <returns>Entier Résultat associé à chaque Bouton</returns>
function MessageDlgFont(const Msg: string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons; FontMsg: TFont; HelpCtx: Longint = 0): Integer; overload;
/// <summary>MessageDlgFont fonctionne comme MessageDlg avec la Gestion de la Font pour la fenêtre et les Boutons</summary>
/// <param name="Msg">Chaine à Afficher</param>
/// <param name="DlgType">Type du Dialogue</param>
/// <param name="Buttons">Ensemble de boutons</param>
/// <param name="DefaultButton">Désigne le bouton qui sera actif par défaut</param>
/// <param name="FontMsg">Font spécifique pour Fenêtre et Boutons</param>
/// <param name="HelpCtx">Contexte d'Aide</param>
/// <returns>Entier Résultat associé à chaque Bouton</returns>
function MessageDlgFont(const Msg: string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons; DefaultButton: TMsgDlgBtn; FontMsg: TFont; HelpCtx: Longint = 0): Integer; overload;
/// <summary>MessageDlgPosFont fonctionne comme MessageDlgPos avec la Gestion de la Font pour la fenêtre et les Boutons</summary>
/// <param name="Msg">Chaine à Afficher</param>
/// <param name="DlgType">Type du Dialogue</param>
/// <param name="Buttons">Ensemble de boutons</param>
/// <param name="FontMsg">Font spécifique pour Fenêtre et Boutons</param>
/// <param name="HelpCtx">Contexte d'Aide</param>
/// <param name="X">Position Horizontale du Message</param>
/// <param name="Y">Position Verticale du Message</param>
/// <returns>Entier Résultat associé à chaque Bouton</returns>
function MessageDlgPosFont(const Msg: string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons; FontMsg: TFont; HelpCtx: Longint; X, Y: Integer): Integer; overload;
/// <summary>MessageDlgPosFont fonctionne comme MessageDlgPos avec la Gestion de la Font pour la fenêtre et les Boutons</summary>
/// <param name="Msg">Chaine à Afficher</param>
/// <param name="DlgType">Type du Dialogue</param>
/// <param name="Buttons">Ensemble de boutons</param>
/// <param name="DefaultButton">Désigne le bouton qui sera actif par défaut</param>
/// <param name="FontMsg">Font spécifique pour Fenêtre et Boutons</param>
/// <param name="HelpCtx">Contexte d'Aide</param>
/// <param name="X">Position Horizontale du Message</param>
/// <param name="Y">Position Verticale du Message</param>
/// <returns>Entier Résultat associé à chaque Bouton</returns>
function MessageDlgPosFont(const Msg: string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons; DefaultButton: TMsgDlgBtn; FontMsg: TFont; HelpCtx: Longint; X, Y: Integer): Integer; overload;
/// <summary>MessageDlgPosHelpFont fonctionne comme MessageDlgPosHelp avec la Gestion de la Font pour la fenêtre et les Boutons</summary>
/// <param name="Msg">Chaine à Afficher</param>
/// <param name="DlgType">Type du Dialogue</param>
/// <param name="Buttons">Ensemble de boutons</param>
/// <param name="FontMsg">Font spécifique pour Fenêtre et Boutons</param>
/// <param name="HelpCtx">Contexte d'Aide</param>
/// <param name="X">Position Horizontale du Message</param>
/// <param name="Y">Position Verticale du Message</param>
/// <param name="HelpFileName">Fichier d'aide</param>
/// <returns>Entier Résultat associé à chaque Bouton</returns>
function MessageDlgPosHelpFont(const Msg: string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons; FontMsg: TFont; HelpCtx: Longint; X, Y: Integer; const HelpFileName: string): Integer; overload;
/// <summary>MessageDlgPosHelpFont fonctionne comme MessageDlgPosHelp avec la Gestion de la Font pour la fenêtre et les Boutons</summary>
/// <param name="Msg">Chaine à Afficher</param>
/// <param name="DlgType">Type du Dialogue</param>
/// <param name="Buttons">Ensemble de boutons</param>
/// <param name="DefaultButton">Désigne le bouton qui sera actif par défaut</param>
/// <param name="FontMsg">Font spécifique pour Fenêtre et Boutons</param>
/// <param name="HelpCtx">Contexte d'Aide</param>
/// <param name="X">Position Horizontale du Message</param>
/// <param name="Y">Position Verticale du Message</param>
/// <param name="HelpFileName">Fichier d'aide</param>
/// <returns>Entier Résultat associé à chaque Bouton</returns>
function MessageDlgPosHelpFont(const Msg: string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons; DefaultButton: TMsgDlgBtn; FontMsg: TFont; HelpCtx: Longint; X, Y: Integer; const HelpFileName: string): Integer; overload;
implementation
uses System.SysUtils, System.DateUtils,
Vcl.ComCtrls, Vcl.Printers,
SLT.Controls.VCL.GraphicsEx;
{ TSLTMessageDlg }
{* --- TSLTMessageDlg.InputCombo -----------------------------------------------------
TSLTMessageDlg.InputCombo fournit un dialogue proche d'un InputQuery avec un sélecteur en liste déroulante
@param Msg Chaine à Afficher
@param ComboItems Liste des élements de la liste déroulante
@param ComboItemIndex Element pré-selectionné
@return Booléen InputCombo renvoie true si l'utilisateur choisit OK et false si l'utilisateur choisit Annuler ou appuie sur Echap.
------------------------------------------------------------------------------ }
class function TSLTMessageDlg.InputCombo(const Msg: string; ComboItems: TStrings; var ComboItemIndex: Integer): 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, I, K: Integer;
HorzMargin, VertMargin, HorzSpacing, HorzOffset, VertOffset: Integer;
PickCombo: TComboBox;
PickText: string;
IndexControls: Integer;
XY: TPoint;
begin
Result := False;
MsgForm := CreateMessageDialog(Msg, mtConfirmation, mbOKCancel);
with MsgForm do
try
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);
PickCombo := TComboBox.Create(MsgForm);
PickCombo.Parent := MsgForm;
PickCombo.Visible := True;
PickCombo.Style := csDropDownList;
InputHeight := PickCombo.Height;
PickCombo.Items.Assign(ComboItems);
PickCombo.ItemIndex := ComboItemIndex;
K := TLabel(MsgLabel).Width;
for I := 0 to PickCombo.Items.Count - 1 do
begin
PickText := PickCombo.Items[I];
K := Max(K, TFontSLTToolHelp.GetTextWidth(PickText, PickCombo.Font));
end;
PickCombo.Top := TLabel(MsgLabel).Top + TLabel(MsgLabel).Height + VertMargin + VertMargin div 2;
PickCombo.Width := K + HorzSpacing + InputHeight*2;
InputWidth := PickCombo.Width;
ActiveControl := PickCombo;
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;
PickCombo.Left := (ClientWidth - PickCombo.Width) div 2 + HorzMargin;
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
ComboItemIndex := PickCombo.ItemIndex;
Result := True;
end;
end
else
Abort;
finally
Free;
end;
end;
{* --- 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 := CreateMessageDialog(Msg, mtConfirmation, mbOKCancel);
with MsgForm do
try
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;
{* --- TSLTMessageDlg.InputPrinter -----------------------------------------------------
TSLTMessageDlg.InputPrinter fournit un dialogue proche d'un InputQuery avec un sélecteur d'imprimante bien plus simple que le TPrintDialog
@param Msg Chaine à Afficher
@param PrinterName Nom de l'imprimante affichée à l'ouverture du Dialogue et contiendra la valeur acceptée par l'utilisateur
@return Booléen InputPrinter renvoie true si l'utilisateur choisit OK et false si l'utilisateur choisit Annuler ou appuie sur Echap.
------------------------------------------------------------------------------ }
class function TSLTMessageDlg.InputPrinter(const Msg: string; var PrinterName: string): Boolean;
var
PrinterIndex: Integer;
begin
PrinterIndex := Printer.Printers.IndexOf(PrinterName);
Result := InputCombo(Msg, Printer.Printers, PrinterIndex) and (PrinterIndex >= 0);
if Result then
PrinterName := Printer.Printers.Strings[PrinterIndex];
end;
{* --- TSLTMessageDlg.Show -----------------------------------------------------
TSLTMessageDlg.Show fonctionne comme MessageDlgPosHelpCustomBtn avec des libellés personnalisés pour chaque bouton
@param Msg Chaine à Afficher
@param DlgType Type du Dialogue
@param Buttons Ensemble de boutons
@param ButtonStrings Fourni les libellés personnalisés pour chaque bouton
@param HelpCtx Contexte d'Aide
@param X Position Horizontale du Message
@param Y Position Verticale du Message
@param HelpFileName Fichier d'aide
@return Entier Résultat associé à chaque Bouton
------------------------------------------------------------------------------ }
class function TSLTMessageDlg.Show(const Msg: string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons; const ButtonStrings: array of String; HelpCtx: Longint = 0; X: Integer = -1; Y: Integer = -1; const HelpFileName: string = ''): Integer;
begin
Result := MessageDlgPosHelpCustomBtn(Msg, DlgType, Buttons, ButtonStrings, HelpCtx, X, Y, HelpFileName);
end;
{* --- TSLTMessageDlg.Show -----------------------------------------------------
TSLTMessageDlg.Show fonctionne comme MessageDlgPosHelpFont avec la Gestion de la Font pour la fenêtre et les Boutons
@param Msg Chaine à Afficher
@param DlgType Type du Dialogue
@param Buttons Ensemble de boutons
@param FontMsg Font spécifique pour Fenêtre et Boutons
@param DefaultButton Désigne le bouton qui sera actif par défaut
@param HelpCtx Contexte d'Aide
@param X Position Horizontale du Message
@param Y Position Verticale du Message
@param HelpFileName Fichier d'aide
@return Entier Résultat associé à chaque Bouton
------------------------------------------------------------------------------ }
class function TSLTMessageDlg.Show(const Msg: string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons; FontMsg: TFont; HelpCtx: Longint = 0; X: Integer = -1; Y: Integer = -1; const HelpFileName: string = ''): Integer;
begin
Result := MessageDlgPosHelpFont(Msg, DlgType, Buttons, FontMsg, HelpCtx, X, Y, HelpFileName);
end;
{* --- TSLTMessageDlg.Show -----------------------------------------------------
TSLTMessageDlg.Show fonctionne comme MessageDlgPosHelpFont avec la Gestion de la Font pour la fenêtre et les Boutons
@param Msg Chaine à Afficher
@param DlgType Type du Dialogue
@param Buttons Ensemble de boutons
@param FontMsg Font spécifique pour Fenêtre et Boutons
@param DefaultButton Désigne le bouton qui sera actif par défaut
@param HelpCtx Contexte d'Aide
@param X Position Horizontale du Message
@param Y Position Verticale du Message
@param HelpFileName Fichier d'aide
@return Entier Résultat associé à chaque Bouton
------------------------------------------------------------------------------ }
class function TSLTMessageDlg.Show(const Msg: string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons; DefaultButton: TMsgDlgBtn; FontMsg: TFont = nil; HelpCtx: Longint = 0; X: Integer = -1; Y: Integer = -1; const HelpFileName: string = ''): Integer;
begin
Result := MessageDlgPosHelpFont(Msg, DlgType, Buttons, DefaultButton, FontMsg, HelpCtx, X, Y, HelpFileName);
end;
{ Procedural code }
{* --- MessageDlgCustomBtn -----------------------------------------------------
MessageDlgCustomBtn fonctionne comme MessageDlg avec des libellés personnalisés pour chaque bouton
@param Msg Chaine à Afficher
@param DlgType Type du Dialogue
@param Buttons Ensemble de boutons
@param ButtonStrings Fourni les libellés personnalisés pour chaque bouton
@param HelpCtx Contexte d'Aide
@return Entier Résultat associé à chaque Bouton
------------------------------------------------------------------------------ }
function MessageDlgCustomBtn(const Msg: string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons; const ButtonStrings: array of String; HelpCtx: Longint = 0): Integer;
begin
Result := MessageDlgPosHelpCustomBtn(Msg, DlgType, Buttons, ButtonStrings, HelpCtx, -1, -1, '');
end;
{* --- MessageDlgPosHelpCustomBtn ----------------------------------------------
MessageDlgPosHelpCustomBtn fonctionne comme MessageDlgPosHelp avec des libellés personnalisés pour chaque bouton
@param Msg Chaine à Afficher
@param DlgType Type du Dialogue
@param Buttons Ensemble de boutons
@param ButtonStrings Fourni les libellés personnalisés pour chaque bouton
@param HelpCtx Contexte d'Aide
@param X Position Horizontale du Message
@param Y Position Verticale du Message
@param HelpFileName Fichier d'aide
@return Entier Résultat associé à chaque Bouton
------------------------------------------------------------------------------ }
function MessageDlgPosHelpCustomBtn(const Msg: string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons; const ButtonStrings: array of String; HelpCtx: Longint; X, Y: Integer; const HelpFileName: string): Integer;
const
ModalResults: array[TMsgDlgBtn] of Integer = (mrYes, mrNo, mrOk, mrCancel, mrAbort, mrRetry, mrIgnore, mrAll, mrNoToAll, mrYesToAll, mrHelp, mrClose);
var
MsgForm: TForm;
IndexControls: Integer;
IndexButtons: TMsgDlgBtn;
IndexStrings: Integer;
NewCaption: string;
NewWidth, AllWidth, BaseWidth, BaseHeigth, BaseLeft, BaseTop: Integer;
TextRect: TRect;
ButtonLine: Integer;
begin
MsgForm := CreateMessageDialog(Msg, DlgType, Buttons);
with MsgForm do
try
if Length(ButtonStrings) > 0 then
begin
IndexStrings := Low(ButtonStrings);
for IndexButtons := Low(IndexButtons) to High(IndexButtons) do
begin
if (IndexButtons in Buttons) and (IndexStrings <= High(ButtonStrings)) then
begin
for IndexControls := 0 to ControlCount - 1 do
begin
if Controls[IndexControls] is TButton then
begin
with TButton(Controls[IndexControls]) do
begin
if ModalResults[IndexButtons] = ModalResult then
begin
NewCaption := ButtonStrings[IndexStrings];
if NewCaption <> '' then
Caption := NewCaption;
Inc(IndexStrings);
if IndexStrings > High(ButtonStrings) then
Break;
end;
end;
end;
end;
end;
end;
end;
AllWidth := 0;
BaseHeigth := 0;
BaseTop := MaxInt;
for IndexControls := 0 to ControlCount - 1 do
begin
if Controls[IndexControls] is TButton then
begin
with TButton(Controls[IndexControls]) do
begin
TextRect := Rect(0,0,0,0);
Winapi.Windows.DrawText(Canvas.Handle, PChar(Caption), -1, TextRect, DT_CALCRECT or DT_LEFT or DT_SINGLELINE or DrawTextBiDiModeFlagsReadingOnly);
with TextRect do
NewWidth := Right - Left + 16;
if NewWidth > Width then
Width := NewWidth;
Inc(AllWidth, Width + 8);
if Height > BaseHeigth then
BaseHeigth := Height;
if Top < BaseTop then
BaseTop := Top;
end;
end;
end;
// 2/3 de l'écran au maximum
if AllWidth > ClientWidth then
begin
if AllWidth + 16 > (Screen.WorkAreaWidth div 3 * 2) then
ClientWidth := (Screen.WorkAreaWidth div 3 * 2)
else
ClientWidth := AllWidth + 16;
end;
if AllWidth + 16 > ClientWidth then
BaseWidth := ClientWidth - 16
else
BaseWidth := AllWidth;
BaseLeft := (MsgForm.ClientWidth - BaseWidth) div 2;
ButtonLine := 0;
for IndexControls := 0 to ControlCount - 1 do
begin
if Controls[IndexControls] is TButton then
begin
with TButton(Controls[IndexControls]) do
begin
if (BaseWidth <> AllWidth) and (BaseLeft + Width > MsgForm.ClientWidth) then
begin
BaseLeft := (MsgForm.ClientWidth - BaseWidth) div 2;
Inc(BaseTop, BaseHeigth + 8);
Inc(ButtonLine);
end;
Left := BaseLeft;
Top := BaseTop;
Inc(BaseLeft, Width + 8);
end;
end;
end;
if ButtonLine > 0 then
ClientHeight := ClientHeight + ButtonLine * (BaseHeigth + 8);
HelpContext := HelpCtx;
HelpFile := HelpFileName;
if X >= 0 then Left := X;
if Y >= 0 then Top := Y;
if (Y < 0) and (X < 0) then Position := poScreenCenter;
Result := ShowModal;
finally
Free;
end;
end;
{ MessageDlgFont }
{* --- MessageDlgFont ----------------------------------------------------------
MessageDlgFont fonctionne comme MessageDlg avec la Gestion de la Font pour la fenêtre et les Boutons
@param Msg Chaine à Afficher
@param DlgType Type du Dialogue
@param Buttons Ensemble de boutons
@param FontMsg Font spécifique pour Fenêtre et Boutons
@param HelpCtx Contexte d'Aide
@return Entier Résultat associé à chaque Bouton
------------------------------------------------------------------------------ }
function MessageDlgFont(const Msg: string; DlgType: TMsgDlgType;
Buttons: TMsgDlgButtons; FontMsg: TFont; HelpCtx: Longint = 0): Integer;
begin
Result := MessageDlgPosHelpFont(Msg, DlgType, Buttons, FontMsg, HelpCtx, -1, -1, '');
end;
{* --- MessageDlgFont ----------------------------------------------------------
MessageDlgFont fonctionne comme MessageDlg avec la Gestion de la Font pour la fenêtre et les Boutons
@param Msg Chaine à Afficher
@param DlgType Type du Dialogue
@param Buttons Ensemble de boutons
@param DefaultButton Désigne le bouton qui sera actif par défaut
@param FontMsg Font spécifique pour Fenêtre et Boutons
@param HelpCtx Contexte d'Aide
@return Entier Résultat associé à chaque Bouton
------------------------------------------------------------------------------ }
function MessageDlgFont(const Msg: string; DlgType: TMsgDlgType;
Buttons: TMsgDlgButtons; DefaultButton: TMsgDlgBtn; FontMsg: TFont;
HelpCtx: Longint = 0): Integer;
begin
Result := MessageDlgPosHelpFont(Msg, DlgType, Buttons, DefaultButton, FontMsg, HelpCtx, -1, -1, '');
end;
{* --- MessageDlgPosFont -------------------------------------------------------
MessageDlgPosFont fonctionne comme MessageDlgPos avec la Gestion de la Font pour la fenêtre et les Boutons
@param Msg Chaine à Afficher
@param DlgType Type du Dialogue
@param Buttons Ensemble de boutons
@param FontMsg Font spécifique pour Fenêtre et Boutons
@param HelpCtx Contexte d'Aide
@param X Position Horizontale du Message
@param Y Position Verticale du Message
@return Entier Résultat associé à chaque Bouton
------------------------------------------------------------------------------ }
function MessageDlgPosFont(const Msg: string; DlgType: TMsgDlgType;
Buttons: TMsgDlgButtons; FontMsg: TFont;
HelpCtx: Longint; X, Y: Integer): Integer;
begin
Result := MessageDlgPosHelpFont(Msg, DlgType, Buttons, FontMsg, HelpCtx, X, Y, '');
end;
{* --- MessageDlgPosFont -------------------------------------------------------
MessageDlgPosFont fonctionne comme MessageDlgPos avec la Gestion de la Font pour la fenêtre et les Boutons
@param Msg Chaine à Afficher
@param DlgType Type du Dialogue
@param Buttons Ensemble de boutons
@param DefaultButton Désigne le bouton qui sera actif par défaut
@param FontMsg Font spécifique pour Fenêtre et Boutons
@param HelpCtx Contexte d'Aide
@param X Position Horizontale du Message
@param Y Position Verticale du Message
@return Entier Résultat associé à chaque Bouton
------------------------------------------------------------------------------ }
function MessageDlgPosFont(const Msg: string; DlgType: TMsgDlgType;
Buttons: TMsgDlgButtons; DefaultButton: TMsgDlgBtn; FontMsg: TFont;
HelpCtx: Longint; X, Y: Integer): Integer;
begin
Result := MessageDlgPosHelpFont(Msg, DlgType, Buttons, DefaultButton, FontMsg, HelpCtx, X, Y, '');
end;
{* --- Internal MessageDlgFont forward ----------------------------------------}
function _MessageDlgPosHelpFont(const Msg: string; DlgType: TMsgDlgType;
Buttons: TMsgDlgButtons; const DefaultButtonArray: array of TMsgDlgBtn;
FontMsg: TFont; HelpCtx: Longint; X, Y: Integer;
const HelpFileName: string): Integer; forward;
{* --- MessageDlgPosHelpFont ---------------------------------------------------
MessageDlgPosHelpFont fonctionne comme MessageDlgPosHelp avec la Gestion de la Font pour la fenêtre et les Boutons
@param Msg Chaine à Afficher
@param DlgType Type du Dialogue
@param Buttons Ensemble de boutons
@param FontMsg Font spécifique pour Fenêtre et Boutons
@param HelpCtx Contexte d'Aide
@param X Position Horizontale du Message
@param Y Position Verticale du Message
@param HelpFileName Fichier d'aide
@return Entier Résultat associé à chaque Bouton
------------------------------------------------------------------------------ }
function MessageDlgPosHelpFont(const Msg: string; DlgType: TMsgDlgType;
Buttons: TMsgDlgButtons; FontMsg: TFont; HelpCtx: Longint; X, Y: Integer;
const HelpFileName: string): Integer;
begin
Result := _MessageDlgPosHelpFont(Msg, DlgType, Buttons, [], FontMsg, HelpCtx, X, Y, HelpFileName);
end;
{* --- MessageDlgPosHelpFont ---------------------------------------------------
MessageDlgPosHelpFont fonctionne comme MessageDlgPosHelp avec la Gestion de la Font pour la fenêtre et les Boutons
@param Msg Chaine à Afficher
@param DlgType Type du Dialogue
@param Buttons Ensemble de boutons
@param DefaultButton Désigne le bouton qui sera actif par défaut
@param FontMsg Font spécifique pour Fenêtre et Boutons
@param HelpCtx Contexte d'Aide
@param X Position Horizontale du Message
@param Y Position Verticale du Message
@param HelpFileName Fichier d'aide
@return Entier Résultat associé à chaque Bouton
------------------------------------------------------------------------------ }
function MessageDlgPosHelpFont(const Msg: string; DlgType: TMsgDlgType;
Buttons: TMsgDlgButtons; DefaultButton: TMsgDlgBtn; FontMsg: TFont;
HelpCtx: Longint; X, Y: Integer;
const HelpFileName: string): Integer;
var
DefaultButtonArray: array of TMsgDlgBtn;
begin
SetLength(DefaultButtonArray, 1);
DefaultButtonArray[0] := DefaultButton;
Result := _MessageDlgPosHelpFont(Msg, DlgType, Buttons, DefaultButtonArray, FontMsg, HelpCtx, X, Y, HelpFileName);
end;
{* --- Internal MessageDlgFont ------------------------------------------------}
function _MessageDlgPosHelpFont(const Msg: string; DlgType: TMsgDlgType;
Buttons: TMsgDlgButtons; const DefaultButtonArray: array of TMsgDlgBtn;
FontMsg: TFont; HelpCtx: Longint; X, Y: Integer;
const HelpFileName: string): Integer;
const
IMAGE_WIDTH = 20;
var
MsgForm: TForm;
MsgLabel: TComponent;
IndexControls: Integer;
OldWidth, OldFontSize, OldLabelWidth, OldLabelHeight, Decalage, DecalageButton: Integer;
OldButton: TButton;
OverButton: TBitBtn;
DefaultButton: TMsgDlgBtn;
NoDefault: Boolean;
procedure SetOverButtonKind(AKind: TBitBtnKind; BtnType: TMsgDlgBtn);
begin
OverButton.Kind := AKind;
OverButton.ModalResult := OldButton.ModalResult;
if OldButton.Default then
begin
OldButton.Default := False;
OverButton.Default := (Length(DefaultButtonArray) = 0) or ((Length(DefaultButtonArray) = 1) and (DefaultButtonArray[0] = BtnType));
end
else
OverButton.Default := (Length(DefaultButtonArray) = 1) and (DefaultButtonArray[0] = BtnType);
if OverButton.Default then
OverButton.TabOrder := 0; // Ne devrais pas être nécessaire en théorie, mais en pratique cela fonctionne bien mieux !
if OldButton.Cancel then
begin
OldButton.Cancel := False;
OverButton.Cancel := True;
end;
if Assigned(OldButton.OnClick) then
OverButton.OnClick := OldButton.OnClick;
end;
begin
if Length(DefaultButtonArray) = 0 then
begin
if mbOk in Buttons then DefaultButton := mbOk else
if mbYes in Buttons then DefaultButton := mbYes else
DefaultButton := mbRetry;
end
else
DefaultButton := DefaultButtonArray[0];
// Si le bouton par défaut n'est pas parmi les boutons affichés, c'est que l'on souhaite aucun bouton par défaut
NoDefault := (Length(DefaultButtonArray) = 1) and not (DefaultButtonArray[0] in Buttons);
MsgForm := CreateMessageDialog(Msg, DlgType, Buttons, DefaultButton);
with MsgForm do
try
MsgLabel := FindComponent('Message');
if MsgLabel is TLabel then
begin
OldLabelWidth := TLabel(MsgLabel).Width;
OldLabelHeight := TLabel(MsgLabel).Height;
OldFontSize := System.Math.Max(TLabel(MsgLabel).Font.Size, 1);
if Assigned(FontMsg) then
TLabel(MsgLabel).Font.Assign(FontMsg);
TLabel(MsgLabel).Width := System.Math.Min(System.Math.Max(OldLabelWidth, Round(OldLabelWidth * TLabel(MsgLabel).Font.Size / OldFontSize)), Screen.Width div 2);
OldWidth := Width;
Width := System.Math.Max(Width, Width - OldLabelWidth + TLabel(MsgLabel).Width);
DecalageButton := 0;
for IndexControls := 0 to ControlCount - 1 do
if Controls[IndexControls] is TButton then
Inc(DecalageButton, IMAGE_WIDTH);
Decalage := (Width - OldWidth) div 2 + DecalageButton;
Width := Width + DecalageButton;
for IndexControls := 0 to ControlCount - 1 do
begin
if Controls[IndexControls] is TButton then
begin
OldButton := TButton(Controls[IndexControls]); // utilisé dans SetOverButtonKind
OverButton := TBitBtn.Create(MsgForm);
OverButton.Left := OldButton.Left + Decalage - DecalageButton;
OverButton.Top := OldButton.Top;
OverButton.Height := OldButton.Height + 2;
OverButton.Width := OldButton.Width + IMAGE_WIDTH;
OverButton.Parent := OldButton.Parent;
OverButton.Anchors := OldButton.Anchors - [akTop] + [akBottom];
Dec(DecalageButton, IMAGE_WIDTH);
case OldButton.ModalResult of
mrNone: SetOverButtonKind(bkHelp, mbHelp);
mrOk: SetOverButtonKind(bkOK, mbOK);
mrCancel: SetOverButtonKind(bkCancel, mbCancel);
mrAbort: SetOverButtonKind(bkAbort, mbAbort);
mrRetry: SetOverButtonKind(bkRetry, mbRetry);
mrIgnore: SetOverButtonKind(bkIgnore, mbIgnore);
mrYes: SetOverButtonKind(bkYes, mbYes);
mrNo: SetOverButtonKind(bkNo, mbNo);
mrAll: SetOverButtonKind(bkAll, mbAll);
mrNoToAll: SetOverButtonKind(bkNo, mbYesToAll);
mrYesToAll: SetOverButtonKind(bkYes, mbNoToAll);
end;
OldButton.Enabled := False;
OldButton.Visible := False;
end;
end;
// Création d'un champ servant de capture au clavier (touche ENTER)
// lorsque l'on ne veut pas de bouton par défaut
if NoDefault then
begin
with TButton.Create(MsgForm) do
begin
Parent := MsgForm;
SetBounds(-1, -1, 0, 0);
Show();
TabOrder := 0;
end;
end;
Height := System.Math.Max(Height, Height - OldLabelHeight + TLabel(MsgLabel).Height);
Left := (Screen.Width div 2) - (Width div 2);
Top := (Screen.Height div 2) - (Height div 2);
end;
HelpContext := HelpCtx;
HelpFile := HelpFileName;
if X >= 0 then Left := X;
if Y >= 0 then Top := Y;
if (Y < 0) and (X < 0) then Position := poScreenCenter;
Result := ShowModal();
finally
Free;
end;
end;
end. |
Partager