mProgressProFrm.pas 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. mmTask: TMemo;
  18. procedure Shape1MouseDown(Sender: TObject; Button: TMouseButton;
  19. Shift: TShiftState; X, Y: Integer);
  20. private
  21. { Private declarations }
  22. public
  23. { Public declarations }
  24. end;
  25. TProgressPosType = (pptNo, pptAdd, pptSet); // 进度不变;增加进度;指定进度;
  26. TProgressMemoType = (pmtNo, pmtAdd, pmtEdit); // 备注信息不变;增加一条备注;修改当前这条备注。
  27. procedure ProgressProCreate(AMaxValue: Integer = 100; ATitle: string = '正在处理请稍候>>>');
  28. procedure ProgressProFree;
  29. procedure ProgressProRun(AMemoValue: string; APosValue: Integer = 10;
  30. AMemoType: TProgressMemoType = pmtAdd; APosType: TProgressPosType = pptAdd);
  31. procedure ProgressProTitle(ATitle: string);
  32. var
  33. ProgressProForm: TProgressProForm = nil;
  34. const
  35. sc_DragMove = $f012;
  36. implementation
  37. uses ScUtils;
  38. {$R *.dfm}
  39. procedure ProgressProCreate(AMaxValue: Integer; ATitle: string);
  40. begin
  41. if ProgressProForm = nil then
  42. ProgressProForm := TProgressProForm.Create(nil);
  43. ProgressProForm.lblTitle.Caption := ATitle;
  44. ProgressProForm.Gauge1.MaxValue := AMaxValue;
  45. ProgressProForm.Gauge1.Progress := 0;
  46. ProgressProForm.Show;
  47. ProgressProForm.Update;
  48. end;
  49. procedure ProgressProFree;
  50. begin
  51. if ProgressProForm <> nil then
  52. begin
  53. with ProgressProForm.Gauge1 do
  54. begin
  55. if (Progress <> MaxValue) then
  56. ProgressProRun('已完成。', MaxValue, pmtAdd, pptSet);
  57. end;
  58. // 关闭前要延迟500ms,有些地方如果不延迟,关得太快,感觉进度条没走完就关了,体验很不好。
  59. Sleep(500);
  60. FreeAndNil(ProgressProForm);
  61. end;
  62. end;
  63. procedure ProgressProRun(AMemoValue: string; APosValue: Integer;
  64. AMemoType: TProgressMemoType; APosType: TProgressPosType);
  65. begin
  66. if ProgressProForm = nil then Exit;
  67. with ProgressProForm do
  68. begin
  69. if APosType = pptAdd then
  70. Gauge1.Progress := Gauge1.Progress + APosValue
  71. else if APosType = pptSet then
  72. Gauge1.Progress := APosValue;
  73. if Gauge1.Progress > Gauge1.MaxValue then // 如果算得不对,缩回5格
  74. Gauge1.Progress := Gauge1.MaxValue - 2;
  75. lblPercent.Caption := IntToStr(Gauge1.PercentDone) + '%';
  76. if (AMemoType <> pmtNo) or (AMemoValue <> '') then
  77. begin
  78. with mmTask.Lines do
  79. begin
  80. if (AMemoType = pmtEdit) and (Count > 0) then
  81. Delete(Count - 1);
  82. Add(AMemoValue);
  83. end;
  84. end;
  85. Update;
  86. end;
  87. end;
  88. procedure ProgressProTitle(ATitle: string);
  89. begin
  90. if ProgressProForm = nil then Exit;
  91. with ProgressProForm do
  92. begin
  93. lblTitle.Caption := ATitle;
  94. Update;
  95. end;
  96. end;
  97. procedure TProgressProForm.Shape1MouseDown(Sender: TObject;
  98. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  99. begin
  100. ReleaseCapture;
  101. (ProgressProForm as TWinControl).PerForm(wm_SysCommand, sc_DragMove, 0);
  102. end;
  103. end.