SsrAsyncStatusStrip.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. 
  2. using System;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Windows.Forms;
  6. namespace Dongke.WinForm.Controls
  7. {
  8. /// <summary>
  9. /// 状态栏控件
  10. /// </summary>
  11. [ToolboxBitmap(typeof(StatusStrip))]
  12. public partial class SsrAsyncStatusStrip : SsrStatusStrip
  13. {
  14. #region 常量
  15. /// <summary>
  16. /// 通信中
  17. /// </summary>
  18. private const string COMMTIME_TEXT = " {0}({1:F0}){2}";
  19. /// <summary>
  20. /// 通信时长
  21. /// </summary>
  22. private const string COMMTIMES_TEXT = "耗时{0:F0}秒 ";
  23. /// <summary>
  24. /// 通信中
  25. /// </summary>
  26. private static readonly string[] COMMTEXTS = new string[] { "   ", ".  ", ".. ", "..." };
  27. #endregion
  28. #region 成员变量
  29. /// <summary>
  30. /// 是否通信状态
  31. /// </summary>
  32. private bool _communicate = false;
  33. /// <summary>
  34. /// 是否显示时间
  35. /// </summary>
  36. private bool _showTimer = true;
  37. /// <summary>
  38. /// 是否显示通信时长
  39. /// </summary>
  40. private bool _showElapsedTimes = false;
  41. /// <summary>
  42. /// 是否显示通信状态
  43. /// </summary>
  44. private bool _showCommText = true;
  45. /// <summary>
  46. /// 通信时文本
  47. /// </summary>
  48. private string _commText = "通信中";
  49. /// <summary>
  50. /// 通信显示文本索引
  51. /// </summary>
  52. private int _commTextsIndex = 0;
  53. /// <summary>
  54. /// 通信开始时间
  55. /// </summary>
  56. private DateTime _commBeginTime;
  57. /// <summary>
  58. ///
  59. /// </summary>
  60. private TimeSpan _commTimes = TimeSpan.Zero;
  61. #endregion
  62. #region 构造函数
  63. /// <summary>
  64. /// 扩展的状态栏控件
  65. /// </summary>
  66. public SsrAsyncStatusStrip()
  67. {
  68. this.InitializeComponent();
  69. this.tmrDateTime.Interval = 1000;
  70. this.tmrCommText.Interval = 1000;
  71. this.timer_Tick(null, null);
  72. this.tmrDateTime.Start();
  73. this.tslblCommLable.Click += tslblCommLable_Click;
  74. }
  75. void tslblCommLable_Click(object sender, EventArgs e)
  76. {
  77. if(!this._communicate)
  78. {
  79. this.ClearCommTimes();
  80. }
  81. }
  82. #endregion
  83. #region 属性
  84. /// <summary>
  85. /// 获取或设置控件的通信状态
  86. /// </summary>
  87. [Description("获取或设置控件的通信状态。"), Category("CustomerEx")]
  88. [DefaultValue(false)]
  89. public bool Communicate
  90. {
  91. get
  92. {
  93. return this._communicate;
  94. }
  95. set
  96. {
  97. if (this._communicate != value)
  98. {
  99. this._communicate = value;
  100. if (this._communicate)
  101. {
  102. this.StartCommunicate();
  103. }
  104. else
  105. {
  106. this.StopCommunicate();
  107. }
  108. }
  109. }
  110. }
  111. /// <summary>
  112. /// 获取或设置控件是否显示本地时间。
  113. /// </summary>
  114. [Description("获取或设置控件是否显示本地时间。"), Category("CustomerEx")]
  115. [DefaultValue(true)]
  116. public bool ShowTimer
  117. {
  118. get
  119. {
  120. return this._showTimer;
  121. }
  122. set
  123. {
  124. if (this._showTimer != value)
  125. {
  126. this._showTimer = value;
  127. if (this._showTimer && this.Visible)
  128. {
  129. this.timer_Tick(null, null);
  130. this.tmrDateTime.Start();
  131. }
  132. else if (this.tmrDateTime.Enabled)
  133. {
  134. this.tmrDateTime.Stop();
  135. this.tslblDateTime.Text = string.Empty;
  136. }
  137. }
  138. }
  139. }
  140. /// <summary>
  141. /// 获取或设置控件是否显示通信时长。
  142. /// </summary>
  143. [Description("获取或设置控件是否显示通信时长。"), Category("CustomerEx")]
  144. [DefaultValue(false)]
  145. public bool ShowElapsedTimes
  146. {
  147. get
  148. {
  149. return this._showElapsedTimes;
  150. }
  151. set
  152. {
  153. if (this._showElapsedTimes != value)
  154. {
  155. this._showElapsedTimes = value;
  156. if (this._showElapsedTimes && this._commTimes > TimeSpan.Zero)
  157. {
  158. this.tslblCommLable.Text = string.Format(COMMTIMES_TEXT, this._commTimes.TotalSeconds);
  159. }
  160. else
  161. {
  162. this.tslblCommLable.Text = string.Empty;
  163. }
  164. }
  165. }
  166. }
  167. /// <summary>
  168. /// 获取或设置控件是否显示通信状态。
  169. /// </summary>
  170. [Description("获取或设置控件是否显示通信状态。"), Category("CustomerEx")]
  171. [DefaultValue(true)]
  172. public bool ShowCommText
  173. {
  174. get
  175. {
  176. return this._showCommText;
  177. }
  178. set
  179. {
  180. if (this._showCommText != value)
  181. {
  182. this._showCommText = value;
  183. }
  184. }
  185. }
  186. /// <summary>
  187. /// 获取或设置通信状态的文本。
  188. /// </summary>
  189. [Description("获取或设置通信状态的文本。"), Category("CustomerEx")]
  190. [DefaultValue("通信中")]
  191. public string CommText
  192. {
  193. get
  194. {
  195. return this._commText;
  196. }
  197. set
  198. {
  199. this._commText = value;
  200. }
  201. }
  202. /// <summary>
  203. /// 获取或设置状态栏的文本。
  204. /// </summary>
  205. [Description("获取或设置状态栏的文本。"), Category("CustomerEx")]
  206. [DefaultValue("")]
  207. public string StripText
  208. {
  209. get
  210. {
  211. return this.tslblStripText.Text;
  212. }
  213. set
  214. {
  215. this.tslblStripText.Text = value;
  216. }
  217. }
  218. #endregion
  219. #region 重写事件
  220. /// <summary>
  221. ///
  222. /// </summary>
  223. /// <param name="e"></param>
  224. protected override void OnVisibleChanged(EventArgs e)
  225. {
  226. base.OnVisibleChanged(e);
  227. if (this._showTimer && this.Visible)
  228. {
  229. this.timer_Tick(null, null);
  230. this.tmrDateTime.Start();
  231. }
  232. else if (this.tmrDateTime.Enabled)
  233. {
  234. this.tmrDateTime.Stop();
  235. this.tslblDateTime.Text = string.Empty;
  236. }
  237. }
  238. #endregion
  239. #region 事件处理
  240. /// <summary>
  241. /// 刷新时间
  242. /// </summary>
  243. /// <param name="sender"></param>
  244. /// <param name="e"></param>
  245. private void timer_Tick(object sender, EventArgs e)
  246. {
  247. this.tslblDateTime.Text = DateTime.Now.ToString(Constant.DATETIME_FORMAT_YYYYMMDDHHMM);
  248. }
  249. /// <summary>
  250. /// 刷新时间
  251. /// </summary>
  252. /// <param name="sender"></param>
  253. /// <param name="e"></param>
  254. private void cTimer_Tick(object sender, EventArgs e)
  255. {
  256. TimeSpan td = DateTime.Now - this._commBeginTime;
  257. this._commTextsIndex = this._commTextsIndex % COMMTEXTS.Length;
  258. if (this._showCommText)
  259. {
  260. this.tslblCommLable.Text = string.Format(COMMTIME_TEXT, this._commText, td.TotalSeconds, COMMTEXTS[this._commTextsIndex++]);
  261. }
  262. else
  263. {
  264. this.tslblCommLable.Text = string.Empty;
  265. }
  266. }
  267. #endregion
  268. #region 公有方法
  269. /// <summary>
  270. /// 清除通信时长显示。
  271. /// </summary>
  272. public void ClearCommTimes()
  273. {
  274. this.tslblCommLable.Text = string.Empty;
  275. }
  276. #endregion
  277. #region 私有方法
  278. /// <summary>
  279. /// 开始通讯
  280. /// </summary>
  281. private void StartCommunicate()
  282. {
  283. // 通信中
  284. if (this.InvokeRequired)
  285. {
  286. this.Invoke(new MethodInvoker(delegate()
  287. {
  288. this.StartCommunicate();
  289. }
  290. ));
  291. }
  292. else
  293. {
  294. this._commTextsIndex = 0;
  295. this._commBeginTime = DateTime.Now;
  296. this._commTimes = TimeSpan.Zero;
  297. this.cTimer_Tick(null, null);
  298. this.tmrCommText.Start();
  299. this.tslblCommImage.Image = Properties.Resources.Status_Communicating_00;
  300. }
  301. }
  302. /// <summary>
  303. /// 结束通讯
  304. /// </summary>
  305. private void StopCommunicate()
  306. {
  307. if (this.InvokeRequired)
  308. {
  309. this.Invoke(new MethodInvoker(delegate()
  310. {
  311. this.StopCommunicate();
  312. }
  313. ));
  314. }
  315. else
  316. {
  317. this._commTimes = DateTime.Now - this._commBeginTime;
  318. this.tmrCommText.Stop();
  319. this._commTextsIndex = 0;
  320. this.tslblCommImage.Image = null;
  321. if (this._showElapsedTimes)
  322. {
  323. this.tslblCommLable.Text = string.Format(COMMTIMES_TEXT, this._commTimes.TotalSeconds);
  324. }
  325. else
  326. {
  327. this.tslblCommLable.Text = string.Empty;
  328. }
  329. this._commTimes = TimeSpan.Zero;
  330. }
  331. }
  332. /// <summary>
  333. /// 设置消息内容
  334. /// </summary>
  335. /// <param name="text"></param>
  336. private void SetStripText(string text)
  337. {
  338. if (this.InvokeRequired)
  339. {
  340. this.Invoke(new MethodInvoker(delegate()
  341. {
  342. SetStripText(text);
  343. }
  344. ));
  345. }
  346. else
  347. {
  348. this.tslblStripText.Text = text;
  349. }
  350. }
  351. #endregion
  352. }
  353. }