Delphi单元规范格式.pas 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 CnCodeDemo;
  21. {* |<PRE>
  22. ================================================================================
  23. * 软件名称:CnPack组件包
  24. * 单元名称:组件代码规范示例单元
  25. * 单元作者:周劲羽 (zjy@cnpack.org)
  26. * 开发平台:PWin98SE + Delphi 5.0
  27. * 兼容测试:PWin9X/2000/XP + Delphi 5/6
  28. * 本 地 化:该单元中的字符串均符合本地化处理方式
  29. * 单元标识:$Id: Delphi单元规范格式.pas,v 1.8 2009/02/25 12:32:57 liuxiao Exp $
  30. * 备 注:- 此单元仅仅只作为 CnPack 的代码规范示例单元,供阅读与对比用,不参与
  31. * 实际的编译与调试。
  32. * - 本例中的TCnTimer采用单独的线程进行定时控制,精度比TTimer要高,相应
  33. * 地也占用较多的CPU资源。
  34. * 修改记录:2009.02.18 V1.1
  35. * 更改单元说明
  36. * 2002.04.18 V1.0
  37. * 创建单元
  38. ================================================================================
  39. |</PRE>}
  40. interface
  41. {$I CnPack.inc}
  42. uses
  43. Windows, Classes, SysUtils, ExtCtrls, CnClasses, CnConsts, CnCompConsts;
  44. type
  45. //==============================================================================
  46. // 高精度定时器组件定时线程
  47. //==============================================================================
  48. { TCnTimerThread }
  49. TCnTimer = class;
  50. TCnTimerThread = class(TThread)
  51. private
  52. FOwner: TCnTimer;
  53. FInterval: Word;
  54. FStop: THandle;
  55. protected
  56. constructor Create(CreateSuspended: Boolean); virtual;
  57. procedure Execute; override;
  58. end;
  59. //==============================================================================
  60. // 高精度定时器组件
  61. //==============================================================================
  62. { TCnTimer }
  63. TTimerQuality = (tqHighest, tqHigh, tqLow);
  64. {* 高精度定时器定时精度类型
  65. |<PRE>
  66. tqHighest - 最高精度,采用高优先级的线程定时
  67. tqHigh - 高精度,采用普通优先级的线程定时
  68. tqLow - 低精度,内部使用TTimer进行定时
  69. |</PRE>}
  70. TCnTimer = class(TCnComponent)
  71. {* 高精度定时器组件,使用单独的线程进行定时控制,使用方法与TTimer一样,
  72. 仅增加了一个Quality属性控制定时精度}
  73. private
  74. FOnTimer: TNotifyEvent;
  75. FQuality: TTimerQuality;
  76. FEnabled: Boolean;
  77. FInterval: Word;
  78. FTimerThread: TCnTimerThread;
  79. FTimer: TTimer;
  80. FLastTick: Cardinal;
  81. FLastCountTick: Cardinal;
  82. FActualInterval: Integer;
  83. FActualRate: Integer;
  84. FCount: Integer;
  85. procedure DoTimer;
  86. procedure OnTimerTimer(Sender: TObject);
  87. procedure CreateTimer;
  88. procedure CreateTimerThread;
  89. procedure FreeTimer;
  90. procedure FreeTimerThread;
  91. procedure SetEnabled(Value: Boolean);
  92. procedure SetInterval(Value: Word);
  93. procedure SetQuality(const Value: TTimerQuality);
  94. protected
  95. function GetAuthor: string; override;
  96. function GetComment: string; override;
  97. public
  98. constructor Create(AOwner: TComponent); override;
  99. destructor Destroy; override;
  100. property ActualInterval: Integer read FActualInterval;
  101. {* 实际的定时间隔,单位为毫秒}
  102. property ActualRate: Integer read FActualRate;
  103. {* 实际的定时速度,单位为次每秒}
  104. published
  105. property Enabled: Boolean read FEnabled write SetEnabled;
  106. {* 是否允许定时事件}
  107. property Interval: Word read FInterval write SetInterval default 1000;
  108. {* 定时间隔,单位为毫秒}
  109. property Quality: TTimerQuality read FQuality write SetQuality default tqLow;
  110. {* 定时精度,如果Interval小于55(Win9X)或10(WinNT),建议设为高精度以上}
  111. property OnTimer: TNotifyEvent read FOnTimer write FOnTimer;
  112. {* 定时器事件}
  113. end;
  114. implementation
  115. //==============================================================================
  116. // 高精度定时器组件定时线程
  117. //==============================================================================
  118. { TCnTimerThread }
  119. // 初始化线程
  120. constructor TCnTimerThread.Create(CreateSuspended: Boolean);
  121. begin
  122. inherited Create(CreateSuspended);
  123. FStop := CreateEvent(nil, False, False, nil); // 创建退出用事件
  124. end;
  125. // 线程主体
  126. procedure TCnTimerThread.Execute;
  127. begin
  128. repeat // 等待退出事件置位或 FInterval 毫秒后超时退出
  129. if WaitForSingleObject(FStop, FInterval) = WAIT_TIMEOUT then
  130. Synchronize(FOwner.DoTimer); // 同步方式产生定时事件
  131. until Terminated;
  132. CloseHandle(FStop); // 释放事件句柄
  133. end;
  134. { TCnTimer }
  135. //==============================================================================
  136. // 高精度定时器组件
  137. //==============================================================================
  138. // 组件初始化
  139. constructor TCnTimer.Create(AOwner: TComponent);
  140. begin
  141. inherited Create(AOwner);
  142. FEnabled := False;
  143. FInterval := 1000;
  144. FQuality := tqLow;
  145. FTimer := nil;
  146. FTimerThread := nil;
  147. CreateTimer;
  148. end;
  149. // 释放
  150. destructor TCnTimer.Destroy;
  151. begin
  152. FreeTimer;
  153. FreeTimerThread;
  154. inherited Destroy;
  155. end;
  156. //------------------------------------------------------------------------------
  157. // 事件产生
  158. //------------------------------------------------------------------------------
  159. // 产生定时事件
  160. procedure TCnTimer.DoTimer;
  161. var
  162. Tick: Cardinal;
  163. begin
  164. Tick := GetTickCount;
  165. if (FLastTick = 0) and (FLastCountTick = 0) then
  166. begin
  167. FLastTick := Tick;
  168. FLastCountTick := Tick;
  169. end
  170. else
  171. begin
  172. FActualInterval := Tick - FLastTick;
  173. FLastTick := Tick;
  174. if Tick - FLastCountTick >= 1000 then
  175. begin
  176. FActualRate := FCount;
  177. FLastCountTick := Tick;
  178. FCount := 0;
  179. end else
  180. Inc(FCount);
  181. end;
  182. begin
  183. if Assigned(FOnTimer) then
  184. FOnTimer(Self);
  185. end;
  186. end;
  187. //------------------------------------------------------------------------------
  188. // 内部定时器创建释放
  189. //------------------------------------------------------------------------------
  190. // 内部Timer事件
  191. procedure TCnTimer.OnTimerTimer(Sender: TObject);
  192. begin
  193. DoTimer;
  194. end;
  195. // 创建内部Timer定时器(低精度)
  196. procedure TCnTimer.CreateTimer;
  197. begin
  198. if not Assigned(FTimer) then
  199. begin
  200. FTimer := TTimer.Create(Self);
  201. FTimer.OnTimer := OnTimerTimer;
  202. FTimer.Interval := FInterval;
  203. FTimer.Enabled := FEnabled;
  204. end;
  205. end;
  206. // 创建定时器线程(高精度)
  207. procedure TCnTimer.CreateTimerThread;
  208. begin
  209. if not Assigned(FTimerThread) then
  210. begin
  211. FTimerThread := TCnTimerThread.Create(True);
  212. FTimerThread.FOwner := Self;
  213. FTimerThread.FreeOnTerminate := False;
  214. FTimerThread.Priority := tpNormal;
  215. FTimerThread.FInterval := FInterval;
  216. if FEnabled then
  217. begin
  218. if FInterval > 0 then
  219. begin
  220. SetEvent(FTimerThread.FStop);
  221. FTimerThread.Resume;
  222. end;
  223. end
  224. else
  225. FTimerThread.Suspend;
  226. end;
  227. end;
  228. // 释放内部定时器(低精度)
  229. procedure TCnTimer.FreeTimer;
  230. begin
  231. if Assigned(FTimer) then
  232. begin
  233. FTimer.Free;
  234. FTimer := nil;
  235. end;
  236. end;
  237. // 释放定时器线程(高精度)
  238. procedure TCnTimer.FreeTimerThread;
  239. begin
  240. if Assigned(FTimerThread) then
  241. begin
  242. FTimerThread.Terminate;
  243. SetEvent(FTimerThread.FStop);
  244. if FTimerThread.Suspended then FTimerThread.Resume;
  245. FTimerThread.WaitFor;
  246. FTimerThread.Free;
  247. FTimerThread := nil;
  248. end;
  249. end;
  250. //------------------------------------------------------------------------------
  251. // 属性读写方法
  252. //------------------------------------------------------------------------------
  253. // 设置定时精度
  254. procedure TCnTimer.SetQuality(const Value: TTimerQuality);
  255. begin
  256. if FQuality <> Value then
  257. begin
  258. FQuality := Value;
  259. case FQuality of
  260. tqHighest, tqHigh:
  261. begin
  262. FreeTimer;
  263. CreateTimerThread;
  264. if Value = tqHighest then
  265. FTimerThread.Priority := tpHigher
  266. else
  267. FTimerThread.Priority := tpNormal;
  268. end;
  269. tqLow:
  270. begin
  271. FreeTimerThread;
  272. CreateTimer;
  273. end;
  274. end;
  275. end;
  276. end;
  277. // 设置是否允许定时
  278. procedure TCnTimer.SetEnabled(Value: Boolean);
  279. begin
  280. if Value <> FEnabled then
  281. begin
  282. FEnabled := Value;
  283. if FQuality = tqLow then
  284. FTimer.Enabled := FEnabled
  285. else
  286. begin
  287. if FEnabled then
  288. begin
  289. if FTimerThread.FInterval > 0 then
  290. begin
  291. SetEvent(FTimerThread.FStop);
  292. FTimerThread.Resume;
  293. end;
  294. end
  295. else
  296. FTimerThread.Suspend;
  297. end;
  298. end;
  299. end;
  300. // 设置定时间隔
  301. procedure TCnTimer.SetInterval(Value: Word);
  302. begin
  303. if Value <> FInterval then
  304. begin
  305. FInterval := Value;
  306. Enabled := False;
  307. if FQuality = tqLow then
  308. FTimer.Interval := FInterval
  309. else
  310. FTimerThread.FInterval := FInterval;
  311. Enabled := True;
  312. end;
  313. end;
  314. // 取组件作者
  315. function TCnTimer.GetAuthor: string;
  316. begin
  317. Result := SCnPack_Zjy;
  318. end;
  319. // 取组件注释
  320. function TCnTimer.GetComment: string;
  321. begin
  322. Result := SCnTimerComment;
  323. end;
  324. end.