mProgressProFrm.pas 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. var
  36. ProgressProForm: TProgressProForm = nil;
  37. const
  38. sc_DragMove = $f012;
  39. implementation
  40. uses ScUtils;
  41. {$R *.dfm}
  42. procedure ProgressProCreate(AMaxValue: Integer; ATitle: string);
  43. begin
  44. if ProgressProForm = nil then
  45. ProgressProForm := TProgressProForm.Create(nil);
  46. ProgressProForm.lblTitle.Caption := ATitle;
  47. ProgressProForm.Gauge1.MaxValue := AMaxValue;
  48. ProgressProForm.Gauge1.Progress := 0;
  49. ProgressProForm.Show;
  50. ProgressProForm.Update;
  51. end;
  52. procedure ProgressProFree;
  53. begin
  54. if ProgressProForm <> nil then
  55. begin
  56. with ProgressProForm.Gauge1 do
  57. begin
  58. if (Progress <> MaxValue) then
  59. ProgressProRun('已完成。', MaxValue, pmtAdd, pptSet);
  60. end;
  61. // 关闭前要延迟500ms,有些地方如果不延迟,关得太快,感觉进度条没走完就关了,体验很不好。
  62. Sleep(500);
  63. FreeAndNil(ProgressProForm);
  64. end;
  65. end;
  66. procedure ProgressProRun(AText: string; APos: Integer;
  67. ATextType: TProgressMemoType; APosType: TProgressPosType);
  68. begin
  69. if ProgressProForm = nil then Exit;
  70. with ProgressProForm do
  71. begin
  72. if APosType = pptAdd then
  73. Gauge1.Progress := Gauge1.Progress + APos
  74. else if APosType = pptSet then
  75. Gauge1.Progress := APos;
  76. if Gauge1.Progress > Gauge1.MaxValue then // 如果算得不对,缩回5格
  77. Gauge1.Progress := Gauge1.MaxValue - 2;
  78. lblPercent.Caption := IntToStr(Gauge1.PercentDone) + '%';
  79. if (ATextType <> pmtNo) or (AText <> '') then
  80. begin
  81. if ATextType = pmtAdd then
  82. begin
  83. if lblHint1.Caption = '' then
  84. begin
  85. lblHint1.Caption := AText;
  86. lblHint1.Update;
  87. end
  88. else if lblHint2.Caption = '' then
  89. begin
  90. lblHint2.Caption := AText;
  91. lblHint2.Update;
  92. end
  93. else if lblHint3.Caption = '' then
  94. begin
  95. lblHint3.Caption := AText;
  96. lblHint3.Update;
  97. end
  98. else
  99. begin
  100. lblHint1.Caption := lblHint2.Caption;
  101. lblHint2.Caption := lblHint3.Caption;
  102. lblHint3.Caption := AText;
  103. lblHint1.Update;
  104. lblHint2.Update;
  105. lblHint3.Update;
  106. end;
  107. end
  108. else if ATextType = pmtEdit then
  109. begin
  110. if (lblHint1.Caption = '') or (lblHint2.Caption = '') then
  111. begin
  112. lblHint1.Caption := AText;
  113. lblHint1.Update;
  114. end
  115. else if lblHint3.Caption = '' then
  116. begin
  117. lblHint2.Caption := AText;
  118. lblHint2.Update;
  119. end
  120. else
  121. begin
  122. lblHint3.Caption := AText;
  123. lblHint3.Update;
  124. end;
  125. end;
  126. end;
  127. // Update;
  128. end;
  129. end;
  130. procedure ProgressProTitle(ATitle: string);
  131. begin
  132. if ProgressProForm = nil then Exit;
  133. with ProgressProForm do
  134. begin
  135. lblTitle.Caption := ATitle;
  136. lblTitle.Update;
  137. end;
  138. end;
  139. function ProgressProHandle: THandle;
  140. begin
  141. Result := 0;
  142. if ProgressProForm <> nil then
  143. Result := ProgressProForm.Handle;
  144. end;
  145. procedure TProgressProForm.Shape1MouseDown(Sender: TObject;
  146. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  147. begin
  148. ReleaseCapture;
  149. (ProgressProForm as TWinControl).PerForm(wm_SysCommand, sc_DragMove, 0);
  150. end;
  151. end.