ScProgressFrm.pas 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. {*******************************************************************************
  2. 单元名称: ScProgressFrm.pas
  3. 单元说明: 新进度条。支持多种进度显示方式。包括:
  4. ①按步距增加进度方式(AddProgressForm方法)
  5. ②直接指定进度位置方式(RefreshProgressForm方法)
  6. 作者时间: Chenshilong, 2011-08-25
  7. *******************************************************************************}
  8. unit ScProgressFrm;
  9. interface
  10. uses
  11. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  12. Dialogs, ComCtrls, StdCtrls, ExtCtrls, Gauges, jpeg;
  13. type
  14. TProgressForm = class(TForm)
  15. Shape1: TShape;
  16. Gauge1: TGauge;
  17. lblTitle: TLabel;
  18. lblComplete: TLabel;
  19. lblShowProgress: TLabel;
  20. lblMessage: TLabel;
  21. lblNeedTime: TLabel;
  22. procedure Shape1MouseDown(Sender: TObject; Button: TMouseButton;
  23. Shift: TShiftState; X, Y: Integer);
  24. private
  25. { Private declarations }
  26. public
  27. { Public declarations }
  28. end;
  29. procedure CreateProgressForm(AMaxValue: Integer; ATitle: string = '正在处理,请稍候>>>');
  30. procedure RefreshProgressForm(APosition: Integer; AText: string); overload;
  31. procedure RefreshProgressForm(AText: string); overload;
  32. procedure RefreshProgressForm(APosition: Integer); overload;
  33. // 显示大概需要多少时间。ARecordCount: 记录数
  34. procedure RefreshProgressTime(ARecordCount: Integer);
  35. procedure AddProgressForm(ACount: Integer; AText: string = '');
  36. procedure CloseProgressForm(Delay: Boolean = True);
  37. // 该方法是旧版本所有,为保证正常调用,保留该方法
  38. procedure ShowFloatProgress(Text: string; Position: Integer);
  39. procedure CloseFloatProgress;
  40. var
  41. ProgressForm: TProgressForm = nil;
  42. const
  43. sc_DragMove = $f012;
  44. implementation
  45. uses ScUtils;
  46. {$R *.dfm}
  47. procedure CreateProgressForm(AMaxValue: Integer; ATitle: string);
  48. begin
  49. if (ProgressForm = nil) or (ProgressForm.Gauge1 = nil) then
  50. ProgressForm := TProgressForm.Create(nil);
  51. ProgressForm.lblTitle.Caption := ATitle;
  52. ProgressForm.Gauge1.MaxValue := AMaxValue;
  53. ProgressForm.Gauge1.Progress := 0;
  54. ProgressForm.Show;
  55. ProgressForm.Update;
  56. end;
  57. procedure RefreshProgressForm(APosition: Integer; AText: string);
  58. begin
  59. if ProgressForm = nil then exit;
  60. with ProgressForm do
  61. begin
  62. Gauge1.Progress := APosition;
  63. lblShowProgress.Caption := IntToStr(Gauge1.PercentDone) + '%';
  64. lblMessage.Caption := AText;
  65. Update;
  66. end;
  67. end;
  68. procedure RefreshProgressForm(AText: string);
  69. begin
  70. if ProgressForm = nil then exit;
  71. ProgressForm.lblMessage.Caption := AText;
  72. ProgressForm.Update;
  73. end;
  74. procedure RefreshProgressForm(APosition: Integer);
  75. var sPercent: string;
  76. begin
  77. if ProgressForm = nil then exit;
  78. with ProgressForm do
  79. begin
  80. Gauge1.Progress := APosition;
  81. lblShowProgress.Caption := IntToStr(Gauge1.PercentDone) + '%';
  82. Update;
  83. end;
  84. end;
  85. procedure AddProgressForm(ACount: Integer; AText: string);
  86. var sPercent: string;
  87. begin
  88. if ProgressForm = nil then exit;
  89. with ProgressForm do
  90. begin
  91. Gauge1.Progress := Gauge1.Progress + ACount;
  92. if Gauge1.Progress >= Gauge1.MaxValue then
  93. begin
  94. Gauge1.Progress := Gauge1.Progress - Gauge1.MaxValue;
  95. if Gauge1.Progress = 0 then
  96. Gauge1.Progress := 10;
  97. end;
  98. lblShowProgress.Caption := IntToStr(Gauge1.PercentDone) + '%';
  99. if AText <> '' then
  100. lblMessage.Caption := AText;
  101. Update;
  102. end;
  103. end;
  104. // 1000行 大概需要3分钟 2000行需要6分钟
  105. procedure RefreshProgressTime(ARecordCount: Integer);
  106. var n: Integer;
  107. begin
  108. with ProgressForm do
  109. begin
  110. n := Round(ARecordCount * 3 / 1000);
  111. if n >= 1 then
  112. begin
  113. lblNeedTime.Caption := Format('预计耗时%d分钟,请勿强行关闭或进行其它操作', [n]);
  114. lblNeedTime.Visible := True;
  115. end
  116. else
  117. begin
  118. lblNeedTime.Visible := False;
  119. end;
  120. lblNeedTime.Update;
  121. end;
  122. end;
  123. { Delay: 关闭前是否要延迟200ms,有些地方如果不延迟,关得太快,感觉进度条没走完就关了,
  124. 视觉效果不好。在批量克隆块到清单的时候,如果每处理一个就延迟200ms,会影响速度。}
  125. procedure CloseProgressForm(Delay: Boolean);
  126. begin
  127. if ProgressForm <> nil then
  128. begin
  129. if (ProgressForm.Gauge1.Progress <> ProgressForm.Gauge1.MaxValue)
  130. or (ProgressForm.lblMessage.Caption <> '已完成。') then
  131. RefreshProgressForm(ProgressForm.Gauge1.MaxValue, '已完成。');
  132. if Delay then
  133. Sleep(200);
  134. FreeAndNil(ProgressForm);
  135. end;
  136. end;
  137. procedure ShowFloatProgress(Text: string; Position: Integer);
  138. begin
  139. if ProgressForm = nil then
  140. ProgressForm := TProgressForm.Create(nil);
  141. ProgressForm.lblMessage.Caption := Text;
  142. ProgressForm.Gauge1.Progress := Position;
  143. ProgressForm.lblShowProgress.Caption := IntToStr(ProgressForm.Gauge1.PercentDone) + '%';
  144. ProgressForm.Show;
  145. ProgressForm.Update;
  146. end;
  147. procedure CloseFloatProgress;
  148. begin
  149. CloseProgressForm;
  150. end;
  151. procedure TProgressForm.Shape1MouseDown(Sender: TObject;
  152. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  153. begin
  154. ReleaseCapture;
  155. (ProgressForm as TWinControl).PerForm(wm_SysCommand, sc_DragMove, 0);
  156. end;
  157. end.