CnProgressFrm.pas 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. {******************************************************************************}
  2. { CnPack For Delphi/C++Builder }
  3. { 中国人自己的开放源码第三方开发包 }
  4. { (C)Copyright 2001-2016 CnPack 开发组 }
  5. { ------------------------------------ }
  6. { }
  7. { 本开发包是开源的自由软件,您可以遵照 CnPack 的发布协议来修 }
  8. { 改和重新发布这一程序。 }
  9. { }
  10. { 发布这一开发包的目的是希望它有用,但没有任何担保。甚至没有 }
  11. { 适合特定目的而隐含的担保。更详细的情况请参阅 CnPack 发布协议。 }
  12. { }
  13. { 您应该已经和开发包一起收到一份 CnPack 发布协议的副本。如果 }
  14. { 还没有,可访问我们的网站: }
  15. { }
  16. { 网站地址:http://www.cnpack.org }
  17. { 电子邮件:master@cnpack.org }
  18. { }
  19. {******************************************************************************}
  20. unit CnProgressFrm;
  21. {* |<PRE>
  22. ================================================================================
  23. * 软件名称:公共窗体库
  24. * 单元名称:通用进度条窗体单元
  25. * 单元作者:周劲羽 (zjy@cnpack.org)
  26. * 备 注:该窗体由程序内部控制开启和关闭,请勿直接创建窗体实例
  27. * 该单元提供以下几个过程用于显示动态提示窗体:
  28. * ShowProgress - 显示进度条窗体
  29. * HideProgress - 隐藏进度条窗体
  30. * UpdateProgress - 更新当前进度
  31. * UpdateProgressTitle - 更新窗体标题
  32. * 使用方法:在需要显示提示窗口的单元中uses本单元,当需要显示提示信息时直接
  33. * 直接调用ShowXXXX过程即可。
  34. * 注意事项:同一时间屏幕上只能显示一个进度窗体,窗体显示时其它所有窗体均不
  35. * 能使用,但显示该窗体的代码仍可以继续运行。
  36. * 开发平台:PWin98SE + Delphi 5.0
  37. * 兼容测试:PWin9X/2000/XP + Delphi 5/6
  38. * 本 地 化:该单元中的字符串尚不符合本地化处理方式
  39. * 单元标识:$Id$
  40. * 修改记录:2008.03.08 V1.1
  41. * xierenxixi 修改其禁用方式
  42. * 2002.04.03 V1.0
  43. * 创建单元
  44. ================================================================================
  45. |</PRE>}
  46. interface
  47. {$I CnPack.inc}
  48. uses
  49. Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  50. Buttons, StdCtrls, ComCtrls;
  51. type
  52. { TProgressForm }
  53. TProgressForm = class(TForm)
  54. SpeedButton1: TSpeedButton;
  55. lblTitle: TLabel;
  56. ProgressBar: TProgressBar;
  57. Label1: TLabel;
  58. private
  59. { Private declarations }
  60. FPerLabel: TLabel;
  61. public
  62. { Public declarations }
  63. procedure DoCreate; override;
  64. end;
  65. procedure ShowProgress(const Title: string; AMax: Integer = 100);
  66. {* 显示进度条窗体,参数为窗体标题以及最大值,默认 100(百分比形式),可自定义成其他值}
  67. procedure HideProgress;
  68. {* 关闭进度条窗体}
  69. procedure UpdateProgress(Value: Integer);
  70. {* 更新当前进度,参数为进度值:当 Max 为 100 时可接受范围为 0..100,此时 Value 代表百分比}
  71. procedure UpdateProgressTitle(const Title: string);
  72. {* 更新进度条窗体标题,参数为标题}
  73. procedure UpdateProgressMax(Value: Integer);
  74. {* 更新进度条最大值,参数为新的最大值}
  75. implementation
  76. {$R *.DFM}
  77. var
  78. ProgressForm: TProgressForm = nil; // 进度条窗体实例
  79. FormList: Pointer; // 被禁用的窗体列表指针
  80. // 显示窗体
  81. procedure ShowProgress(const Title: string; AMax: Integer = 100);
  82. begin
  83. if not Assigned(ProgressForm) then
  84. ProgressForm := TProgressForm.Create(Application.MainForm)
  85. else
  86. ProgressForm.BringToFront;
  87. ProgressForm.lblTitle.Caption := Title;
  88. ProgressForm.ProgressBar.Max := AMax;
  89. ProgressForm.Show;
  90. // xierenxixi 修改
  91. FormList := DisableTaskWindows(ProgressForm.Handle);
  92. ProgressForm.Update;
  93. end;
  94. // 关闭窗体
  95. procedure HideProgress;
  96. begin
  97. if not Assigned(ProgressForm) then Exit;
  98. // xierenxixi 修改
  99. EnableTaskWindows(FormList);
  100. ProgressForm.Hide;
  101. Application.ProcessMessages;
  102. ProgressForm.Free;
  103. ProgressForm := nil;
  104. end;
  105. // 更新进度
  106. procedure UpdateProgress(Value: Integer);
  107. begin
  108. if Assigned(ProgressForm) then
  109. begin
  110. ProgressForm.ProgressBar.Position := Value;
  111. ProgressForm.FPerLabel.Caption := Format('%-3d%%', [Round(Value/ProgressForm.ProgressBar.Max * 100)]);
  112. ProgressForm.Update;
  113. Application.ProcessMessages;
  114. end;
  115. end;
  116. // 更新标题
  117. procedure UpdateProgressTitle(const Title: string);
  118. begin
  119. if Assigned(ProgressForm) then
  120. begin
  121. ProgressForm.lblTitle.Caption := Title;
  122. ProgressForm.Update;
  123. Application.ProcessMessages;
  124. end;
  125. end;
  126. // 更新进度条最大值
  127. procedure UpdateProgressMax(Value: Integer);
  128. begin
  129. if Assigned(ProgressForm) then
  130. begin
  131. ProgressForm.ProgressBar.Max:=Value;
  132. ProgressForm.Update;
  133. Application.ProcessMessages;
  134. end;
  135. end;
  136. { TProgressForm }
  137. procedure TProgressForm.DoCreate;
  138. begin
  139. inherited;
  140. FPerLabel := TLabel.Create(Self);
  141. FPerLabel.Caption := ' '; // 100%
  142. FPerLabel.Parent := Label1.Parent;
  143. FPerLabel.Top := Label1.Top;
  144. FPerLabel.Left := ProgressBar.Left + ProgressBar.Width - FPerLabel.Width;
  145. end;
  146. end.