mProgressProFrm.pas 3.5 KB

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