mProgressProFrm.pas 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. {*******************************************************************************
  2. 单元名称: mProgressProFrm.pas
  3. 单元说明: 任务清单效果的进度条。
  4. 作者时间: Chenshilong, 2015-12-07
  5. *******************************************************************************}
  6. unit mProgressProFrm;
  7. interface
  8. uses
  9. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  10. Dialogs, ComCtrls, StdCtrls, ExtCtrls, Gauges, jpeg;
  11. type
  12. TProgressProForm = class(TForm)
  13. Shape1: TShape;
  14. Gauge1: TGauge;
  15. lblTitle: TLabel;
  16. lblPercent: TLabel;
  17. lblHint1: TLabel;
  18. lblHint2: TLabel;
  19. lblHint3: TLabel;
  20. procedure Shape1MouseDown(Sender: TObject; Button: TMouseButton;
  21. Shift: TShiftState; X, Y: Integer);
  22. private
  23. { Private declarations }
  24. public
  25. { Public declarations }
  26. end;
  27. TProgressPosType = (pptNo, pptAdd, pptSet); // 进度不变;增加进度;指定进度;
  28. TProgressMemoType = (pmtNo, pmtAdd, pmtEdit); // 备注信息不变;增加一条备注;修改当前这条备注。
  29. procedure ProgressProCreate(AMaxValue: Integer = 100; ATitle: string = '正在处理请稍候>>>');
  30. procedure ProgressProFree;
  31. procedure ProgressProRun(AText: string; APos: Integer = 10;
  32. ATextType: TProgressMemoType = pmtAdd; APosType: TProgressPosType = pptAdd);
  33. procedure ProgressProTitle(ATitle: string);
  34. function ProgressProHandle: THandle;
  35. procedure ProgressProHide; // 进度窗太大,会挡住其它提示条,所以隐藏先。
  36. var
  37. ProgressProForm: TProgressProForm = nil;
  38. const
  39. sc_DragMove = $f012;
  40. implementation
  41. uses ScUtils;
  42. {$R *.dfm}
  43. procedure ProgressProCreate(AMaxValue: Integer; ATitle: string);
  44. begin
  45. if ProgressProForm = nil then
  46. ProgressProForm := TProgressProForm.Create(nil);
  47. ProgressProForm.lblTitle.Caption := ATitle;
  48. ProgressProForm.Gauge1.MaxValue := AMaxValue;
  49. ProgressProForm.Gauge1.Progress := 0;
  50. ProgressProForm.Show;
  51. ProgressProForm.Update;
  52. end;
  53. procedure ProgressProFree;
  54. begin
  55. if ProgressProForm <> nil then
  56. begin
  57. if not ProgressProForm.Visible then
  58. begin
  59. ProgressProForm.Visible := True;
  60. ProgressProForm.Update;
  61. end;
  62. with ProgressProForm.Gauge1 do
  63. begin
  64. if (Progress <> MaxValue) then
  65. ProgressProRun('已完成。', MaxValue, pmtAdd, pptSet);
  66. end;
  67. // 关闭前要延迟500ms,有些地方如果不延迟,关得太快,感觉进度条没走完就关了,体验很不好。
  68. Sleep(500);
  69. FreeAndNil(ProgressProForm);
  70. end;
  71. end;
  72. procedure ProgressProRun(AText: string; APos: Integer;
  73. ATextType: TProgressMemoType; APosType: TProgressPosType);
  74. begin
  75. if ProgressProForm = nil then Exit;
  76. with ProgressProForm do
  77. begin
  78. if APosType = pptAdd then
  79. Gauge1.Progress := Gauge1.Progress + APos
  80. else if APosType = pptSet then
  81. Gauge1.Progress := APos;
  82. if Gauge1.Progress > Gauge1.MaxValue then // 如果算得不对,缩回5格
  83. Gauge1.Progress := Gauge1.MaxValue - 2;
  84. lblPercent.Caption := IntToStr(Gauge1.PercentDone) + '%';
  85. if (ATextType <> pmtNo) or (AText <> '') then
  86. begin
  87. if ATextType = pmtAdd then
  88. begin
  89. if lblHint1.Caption = '' then
  90. begin
  91. lblHint1.Caption := AText;
  92. lblHint1.Update;
  93. end
  94. else if lblHint2.Caption = '' then
  95. begin
  96. lblHint2.Caption := AText;
  97. lblHint2.Update;
  98. end
  99. else if lblHint3.Caption = '' then
  100. begin
  101. lblHint3.Caption := AText;
  102. lblHint3.Update;
  103. end
  104. else
  105. begin
  106. lblHint1.Caption := lblHint2.Caption;
  107. lblHint2.Caption := lblHint3.Caption;
  108. lblHint3.Caption := AText;
  109. lblHint1.Update;
  110. lblHint2.Update;
  111. lblHint3.Update;
  112. end;
  113. end
  114. else if ATextType = pmtEdit then
  115. begin
  116. if (lblHint1.Caption = '') or (lblHint2.Caption = '') then
  117. begin
  118. lblHint1.Caption := AText;
  119. lblHint1.Update;
  120. end
  121. else if lblHint3.Caption = '' then
  122. begin
  123. lblHint2.Caption := AText;
  124. lblHint2.Update;
  125. end
  126. else
  127. begin
  128. lblHint3.Caption := AText;
  129. lblHint3.Update;
  130. end;
  131. end;
  132. end;
  133. // Update;
  134. end;
  135. end;
  136. procedure ProgressProTitle(ATitle: string);
  137. begin
  138. if ProgressProForm = nil then Exit;
  139. with ProgressProForm do
  140. begin
  141. lblTitle.Caption := ATitle;
  142. lblTitle.Update;
  143. end;
  144. end;
  145. function ProgressProHandle: THandle;
  146. begin
  147. Result := 0;
  148. if ProgressProForm <> nil then
  149. Result := ProgressProForm.Handle;
  150. end;
  151. procedure TProgressProForm.Shape1MouseDown(Sender: TObject;
  152. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  153. begin
  154. ReleaseCapture;
  155. (ProgressProForm as TWinControl).PerForm(wm_SysCommand, sc_DragMove, 0);
  156. end;
  157. procedure ProgressProHide;
  158. begin
  159. if ProgressProForm <> nil then
  160. ProgressProForm.Hide;
  161. end;
  162. end.