Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

shared.pas 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. {*
  2. * Shared methods / classes / functions between Windows programs
  3. *
  4. * This application launches DMDirc on windows and passes control to the
  5. * update engine as necessary.
  6. *
  7. * DMDirc - Open Source IRC Client
  8. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes,
  9. * Michael Nixon
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  27. * SOFTWARE.
  28. *}
  29. unit shared;
  30. interface
  31. uses Windows, SysUtils, Vista;
  32. function nicesize(dsize: extended): string;
  33. function askQuestion(Question: String; Title: string): boolean;
  34. procedure showmessage(message: String; Title: string; context:String = 'Information');
  35. procedure showError(ErrorMessage: String; Title: string; addFooter: boolean = true; includeDescInXP: boolean = true);
  36. function Launch(sProgramToRun: String; hide: boolean = false): TProcessInformation;
  37. function ExecAndWait(sProgramToRun: String; hide: boolean = false): Longword;
  38. procedure RunProgram(sProgramToRun: String; wait: boolean);
  39. { ---------------------------------------------------------------------------- }
  40. implementation
  41. { ----------------------------------------------------------------------------
  42. Takes a size, <dsize> in bytes, and converts it a human readable string with
  43. a suffix (MB or GB).
  44. ---------------------------------------------------------------------------- }
  45. function nicesize(dsize: extended): string;
  46. var
  47. kbytes: single;
  48. mbytes: single;
  49. gbytes: single;
  50. begin
  51. kbytes := dsize / 1024;
  52. mbytes := kbytes / 1024;
  53. gbytes := mbytes / 1024;
  54. if kbytes < 1024 then begin
  55. result := FloatToStrF(kbytes, ffFixed, 10, 2) + ' kB';
  56. exit;
  57. end;
  58. if mbytes < 1024 then begin
  59. result := FloatToStrF(mbytes, ffFixed, 10, 2) + ' MB';
  60. exit;
  61. end;
  62. result := FloatToStrF(gbytes, ffFixed, 10, 2) + ' GB';
  63. exit;
  64. end;
  65. { ----------------------------------------------------------------------------
  66. Ask a question and return True for YES and False for NO
  67. Uses nifty vista task dialog if available
  68. ---------------------------------------------------------------------------- }
  69. function askQuestion(Question: String; Title: string): boolean;
  70. begin
  71. Result := TaskDialog(0, Title, 'Question', Question, TD_ICON_QUESTION, TD_BUTTON_YES + TD_BUTTON_NO) = mrYes;
  72. end;
  73. { ----------------------------------------------------------------------------
  74. Show a message box (information)
  75. Uses nifty vista task dialog if available
  76. ---------------------------------------------------------------------------- }
  77. procedure showmessage(message: String; Title: string; context:String = 'Information');
  78. begin
  79. TaskDialog(0, Title, context, message, TD_ICON_INFORMATION, TD_BUTTON_OK);
  80. end;
  81. { ----------------------------------------------------------------------------
  82. Show an error message
  83. Uses nifty vista task dialog if available
  84. ---------------------------------------------------------------------------- }
  85. procedure showError(ErrorMessage: String; Title: string; addFooter: boolean = true; includeDescInXP: boolean = true);
  86. begin
  87. if addFooter then begin
  88. ErrorMessage := ErrorMessage+#13#10;
  89. ErrorMessage := ErrorMessage+#13#10+'If you feel this is incorrect, or you require some further assistance,';
  90. if not IsWindowsVista then ErrorMessage := ErrorMessage+#13#10;
  91. ErrorMessage := ErrorMessage+'please feel free to contact us.';
  92. end;
  93. TaskDialog(0, Title, 'Sorry, ' + Title + ' is unable to continue.', ErrorMessage, TD_ICON_ERROR, TD_BUTTON_OK, includeDescInXP, false);
  94. end;
  95. { ----------------------------------------------------------------------------
  96. Launch a process (hidden if requested) and immediately return control to
  97. the current thread
  98. ---------------------------------------------------------------------------- }
  99. function Launch(sProgramToRun: String; hide: boolean = false): TProcessInformation;
  100. var
  101. StartupInfo: TStartupInfo;
  102. begin
  103. FillChar(StartupInfo, SizeOf(TStartupInfo), 0);
  104. with StartupInfo do begin
  105. cb := SizeOf(TStartupInfo);
  106. dwFlags := STARTF_USESHOWWINDOW;
  107. if hide then wShowWindow := SW_HIDE
  108. else wShowWindow := SW_SHOWNORMAL;
  109. end;
  110. CreateProcess(nil, PChar(sProgramToRun), nil, nil, False, NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, Result);
  111. end;
  112. { ----------------------------------------------------------------------------
  113. Launch a process (hidden if requested) and wait for it to finish
  114. ---------------------------------------------------------------------------- }
  115. function ExecAndWait(sProgramToRun: String; hide: boolean = false): Longword;
  116. var
  117. ProcessInfo: TProcessInformation;
  118. begin
  119. ProcessInfo := Launch(sProgramToRun, hide);
  120. getExitCodeProcess(ProcessInfo.hProcess, Result);
  121. while Result = STILL_ACTIVE do begin
  122. sleep(1000);
  123. GetExitCodeProcess(ProcessInfo.hProcess, Result);
  124. end;
  125. end;
  126. { ----------------------------------------------------------------------------
  127. Launch a process and either waits for it or returns control immediately
  128. ---------------------------------------------------------------------------- }
  129. procedure RunProgram(sProgramToRun: String; wait: boolean);
  130. begin
  131. if wait then ExecAndWait(sProgramToRun)
  132. else Launch(sProgramToRun);
  133. end;
  134. { ----------------------------------------------------------------------------
  135. ---------------------------------------------------------------------------- }
  136. end.