UtilsWin.pas 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. unit UtilsWin;
  2. interface
  3. uses
  4. Windows;
  5. type
  6. TMessageButtons = (mbOK, mbOKCancel, mbYesNo, mbYesNoCancel, mbRetryCancel, mbAbortRetryIgnore);
  7. TMessageIcon = (miNone, miAsterisk, miInformation, miQuestion, miExclamation, miWarning, miHand, miStop, miError);
  8. TMessageModalState = (msDefault, msApplModal, msSystemModal, msTaskModal);
  9. TDefaultMessageType = (mtDefault, mtInformation, mtQuestion, mtWarning, mtProgramError, mtDeviceError);
  10. function ShowMessageBox(const Content, Title: string; Buttons: TMessageButtons = mbOK; Icon: TMessageIcon = miNone; ModalState: TMessageModalState = msDefault; DisplayHelp: Boolean = False; DefaultButton: Integer = 0; Flags: Integer = 0): Integer;
  11. function ShowDefaultMessageBox(const Content, Title: string; MessageType: TDefaultMessageType = mtDefault; Critical: Boolean = False; DisplayHelp: Boolean = False; DefaultButton: Integer = 0): Integer;
  12. function CompressMessageFlags(Buttons: TMessageButtons; Icon: TMessageIcon; ModalState: TMessageModalState; DisplayHelp: Boolean; DefaultButton: Integer): Integer;
  13. implementation
  14. function ShowMessageBox;
  15. begin
  16. Result := MessageBox (GetActiveWindow, PChar (Content), PChar (Title), Flags or CompressMessageFlags (Buttons, Icon, ModalState, DisplayHelp, DefaultButton));
  17. end;
  18. function ShowDefaultMessageBox;
  19. var
  20. Buttons: TMessageButtons;
  21. Icon: TMessageIcon;
  22. begin
  23. case MessageType of
  24. mtInformation: begin
  25. if Critical then
  26. Buttons := mbOKCancel
  27. else
  28. Buttons := mbOK;
  29. Icon := miInformation;
  30. end;
  31. mtQuestion: begin
  32. if Critical then
  33. Buttons := mbYesNoCancel
  34. else
  35. Buttons := mbYesNo;
  36. Icon := miQuestion;
  37. end;
  38. mtWarning: begin
  39. if Critical then
  40. Buttons := mbOKCancel
  41. else
  42. Buttons := mbOK;
  43. Icon := miWarning;
  44. end;
  45. mtProgramError: begin
  46. if Critical then
  47. Buttons := mbOKCancel
  48. else
  49. Buttons := mbOK;
  50. Icon := miError;
  51. end;
  52. mtDeviceError: begin
  53. if Critical then
  54. Buttons := mbRetryCancel
  55. else
  56. Buttons := mbAbortRetryIgnore;
  57. Icon := miError;
  58. end;
  59. else begin
  60. if Critical then
  61. Buttons := mbOKCancel
  62. else
  63. Buttons := mbOK;
  64. Icon := miNone;
  65. end;
  66. end;
  67. Result := ShowMessageBox (Content, Title, Buttons, Icon, msDefault, DisplayHelp, DefaultButton, 0);
  68. end;
  69. function CompressMessageFlags;
  70. begin
  71. case Buttons of
  72. mbOK: Result := mb_OK;
  73. mbOKCancel: Result := mb_OKCancel;
  74. mbYesNo: Result := mb_YesNo;
  75. mbYesNoCancel: Result := mb_YesNoCancel;
  76. mbRetryCancel: Result := mb_RetryCancel;
  77. mbAbortRetryIgnore: Result := mb_AbortRetryIgnore;
  78. else Result := 0;
  79. end;
  80. case Icon of
  81. miAsterisk: Result := Result or mb_IconAsterisk;
  82. miInformation: Result := Result or mb_IconInformation;
  83. miQuestion: Result := Result or mb_IconQuestion;
  84. miExclamation: Result := Result or mb_IconExclamation;
  85. miWarning: Result := Result or mb_IconWarning;
  86. miHand: Result := Result or mb_IconHand;
  87. miStop: Result := Result or mb_IconStop;
  88. miError: Result := Result or mb_IconError;
  89. end;
  90. case ModalState of
  91. msApplModal: Result := Result or mb_ApplModal;
  92. msSystemModal: Result := Result or mb_SystemModal;
  93. msTaskModal: Result := Result or mb_TaskModal;
  94. end;
  95. if DisplayHelp then
  96. Result := Result or mb_Help;
  97. case DefaultButton of
  98. 1: Result := Result or mb_DefButton1;
  99. 2: Result := Result or mb_DefButton2;
  100. 3: Result := Result or mb_DefButton3;
  101. 4: Result := Result or mb_DefButton4;
  102. end;
  103. end;
  104. end.