mProgressProFrm.pas 3.1 KB

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