CnConsole.pas 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. {******************************************************************************}
  2. { CnPack For Delphi/C++Builder }
  3. { 中国人自己的开放源码第三方开发包 }
  4. { (C)Copyright 2001-2018 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 CnConsole;
  21. {* |<PRE>
  22. ================================================================================
  23. * 软件名称:不可视工具组件包
  24. * 单元名称:控制台组件 TCnConsole 单元
  25. * 单元作者:刘啸 (liuxiao@cnpack.org)
  26. * 菩提
  27. * 备 注:为 GUI 程序增加控制台
  28. * 开发平台:PWinXP + Delphi 5.0
  29. * 兼容测试:PWinXP + Delphi 5.0
  30. * 本 地 化:该单元中无字符串资源
  31. * 单元标识:$Id$
  32. * 修改记录:2008.10.14 v1.1
  33. * 菩提加入控制文本颜色的功能
  34. * 2006.10.05 v1.0
  35. * 创建单元
  36. ================================================================================
  37. |</PRE>}
  38. interface
  39. {$I CnPack.inc}
  40. uses
  41. SysUtils, Classes, Windows,
  42. CnClasses, CnConsts, CnCompConsts;
  43. {控制台文本的颜色常量}
  44. const
  45. tfBlue =1;
  46. tfGreen =2;
  47. tfRed =4;
  48. tfIntensity = 8;
  49. tfWhite = $f;
  50. tbBlue =$10;
  51. tbGreen =$20;
  52. tbRed =$40;
  53. tbIntensity = $80;
  54. type
  55. TCnConsole = class(TCnComponent)
  56. private
  57. FConsoleTitle: string;
  58. FEnabled: Boolean;
  59. FConsoleHandle:THandle;
  60. procedure SetConsoleTitle(const Value: string);
  61. procedure SetEnabled(const Value: Boolean);
  62. protected
  63. procedure GetComponentInfo(var AName, Author, Email, Comment: string); override;
  64. public
  65. procedure ResetConsole;
  66. {* 复位控制台,在属性Enabled为True时有效。}
  67. procedure SetTextColor(const aColor:WORD);
  68. {* 设置控制台的前景颜色}
  69. constructor Create(AOwner: TComponent); override;
  70. destructor Destroy; override;
  71. published
  72. property Enabled: Boolean read FEnabled write SetEnabled;
  73. {* 为True时启动控制台,为False关闭控制台}
  74. property ConsoleTitle: string read FConsoleTitle write SetConsoleTitle;
  75. {* 控制台的标题}
  76. end;
  77. implementation
  78. { TCnConsole }
  79. constructor TCnConsole.Create(AOwner: TComponent);
  80. begin
  81. inherited;
  82. FEnabled := False;
  83. end;
  84. destructor TCnConsole.Destroy;
  85. begin
  86. if not (csDesigning in ComponentState) and FEnabled then
  87. FreeConsole;
  88. inherited;
  89. end;
  90. procedure TCnConsole.GetComponentInfo(var AName, Author, Email,
  91. Comment: string);
  92. begin
  93. AName := SCnConsoleName;
  94. Author := SCnPack_LiuXiao;
  95. Email := SCnPack_LiuXiaoEmail;
  96. Comment := SCnConsoleComment;
  97. end;
  98. procedure TCnConsole.ResetConsole;
  99. begin
  100. if csDesigning in ComponentState then
  101. Exit;
  102. if FEnabled then
  103. begin
  104. FreeConsole;
  105. AllocConsole;
  106. FConsoleHandle := GetStdHandle(STD_OUTPUT_HANDLE);
  107. if FConsoleTitle <> '' then
  108. Windows.SetConsoleTitle(PChar(FConsoleTitle));
  109. end;
  110. end;
  111. procedure TCnConsole.SetConsoleTitle(const Value: string);
  112. begin
  113. FConsoleTitle := Value;
  114. if FEnabled and not (csDesigning in ComponentState) then
  115. if (FConsoleTitle <> '') or not (csLoading in ComponentState) then
  116. Windows.SetConsoleTitle(PChar(FConsoleTitle));
  117. end;
  118. procedure TCnConsole.SetEnabled(const Value: Boolean);
  119. begin
  120. if FEnabled <> Value then
  121. begin
  122. FEnabled := Value;
  123. if csDesigning in ComponentState then
  124. Exit;
  125. if FEnabled then
  126. begin
  127. AllocConsole;
  128. SetConsoleTitle(PChar(FConsoleTitle));
  129. FConsoleHandle := GetStdHandle(STD_OUTPUT_HANDLE);
  130. end
  131. else
  132. begin
  133. FreeConsole;
  134. end;
  135. end;
  136. end;
  137. procedure TCnConsole.SetTextColor(const aColor: WORD);
  138. begin
  139. if FEnabled then
  140. SetConsoleTextAttribute(FConsoleHandle, aColor);
  141. end;
  142. end.