frmWaitingBox.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace IntelligentControlForsx
  10. {
  11. public partial class frmWaitingBox : Form
  12. {
  13. #region Properties
  14. private int _MaxWaitTime;
  15. private int _WaitTime;
  16. private bool _CancelEnable;
  17. private IAsyncResult _AsyncResult;
  18. private EventHandler<EventArgs> _Method;
  19. private bool _IsShown = true;
  20. private readonly int _EffectCount = 10;
  21. private readonly int _EffectTime = 500;
  22. /// <summary>
  23. /// 控制界面显示的特性
  24. /// </summary>
  25. private Timer _Timer;
  26. public string Message { get; private set; }
  27. public int TimeSpan { get; set; }
  28. public bool FormEffectEnable { get; set; }
  29. #endregion
  30. #region frmWaitingBox
  31. public frmWaitingBox(EventHandler<EventArgs> method,int maxWaitTime,string waitMessage,bool cancelEnable,bool timerVisable)
  32. {
  33. maxWaitTime *= 1000;
  34. Initialize(method, maxWaitTime,waitMessage, cancelEnable, timerVisable);
  35. }
  36. public frmWaitingBox(EventHandler<EventArgs> method)
  37. {
  38. int maxWaitTime=60*1000;
  39. string waitMessage = "正在处理数据,请稍后...";
  40. bool cancelEnable=true;
  41. bool timerVisable=true;
  42. Initialize(method, maxWaitTime,waitMessage, cancelEnable, timerVisable);
  43. }
  44. public frmWaitingBox(EventHandler<EventArgs> method, string waitMessage)
  45. {
  46. int maxWaitTime = 60 * 1000;
  47. bool cancelEnable = true;
  48. bool timerVisable = true;
  49. Initialize(method, maxWaitTime, waitMessage, cancelEnable, timerVisable);
  50. }
  51. public frmWaitingBox(EventHandler<EventArgs> method, bool cancelEnable, bool timerVisable)
  52. {
  53. int maxWaitTime = 60*1000;
  54. string waitMessage = "正在处理数据,请稍后...";
  55. Initialize(method, maxWaitTime,waitMessage, cancelEnable, timerVisable);
  56. }
  57. #endregion
  58. #region Initialize
  59. private void Initialize(EventHandler<EventArgs> method, int maxWaitTime,string waitMessage,bool cancelEnable, bool timerVisable)
  60. {
  61. InitializeComponent();
  62. //initialize form
  63. this.FormBorderStyle = FormBorderStyle.None;
  64. this.StartPosition = FormStartPosition.CenterParent;
  65. this.ShowInTaskbar = false;
  66. Color[] c = GetRandColor();
  67. this.panel1.BackColor = c[0];
  68. this.BackColor = c[1];
  69. this.labMessage.Text = waitMessage;
  70. _Timer = new Timer();
  71. _Timer.Interval = _EffectTime/_EffectCount;
  72. _Timer.Tick += _Timer_Tick;
  73. this.Opacity = 0;
  74. FormEffectEnable = true;
  75. //para
  76. TimeSpan = 500;
  77. Message = string.Empty;
  78. _CancelEnable = cancelEnable;
  79. _MaxWaitTime = maxWaitTime;
  80. _WaitTime = 0;
  81. _Method = method;
  82. this.pictureBoxCancel.Visible = _CancelEnable;
  83. this.labTimer.Visible = timerVisable;
  84. this.timer1.Interval = TimeSpan;
  85. this.timer1.Start();
  86. }
  87. #endregion
  88. #region Color
  89. private Color[] GetRandColor()
  90. {
  91. int rMax = 248;
  92. int rMin = 204;
  93. int gMax = 250;
  94. int gMin = 215;
  95. int bMax = 250;
  96. int bMin = 240;
  97. Random r = new Random(DateTime.Now.Millisecond);
  98. int r1 = r.Next(rMin, rMax);
  99. int r2 = r1 + 5;
  100. int g1 = r.Next(gMin, gMax);
  101. int g2 = g1 + 5;
  102. int b1 = r.Next(bMin, bMax);
  103. int b2 = b1 + 5;
  104. Color c1 = Color.FromArgb(r1, g1, b1);
  105. Color c2 = Color.FromArgb(r2, g2, b2);
  106. Color[] c = { c1, c2 };
  107. return c;
  108. }
  109. #endregion
  110. #region Events
  111. private void btnCancel_Click(object sender, EventArgs e)
  112. {
  113. this.Message = "您结束了当前操作!";
  114. this.Close();
  115. }
  116. private void timer1_Tick(object sender, EventArgs e)
  117. {
  118. try
  119. {
  120. _WaitTime += TimeSpan;
  121. this.labTimer.Text = string.Format("{0}秒", _WaitTime / 1000);
  122. if (!this._AsyncResult.IsCompleted)
  123. {
  124. if (_WaitTime > _MaxWaitTime)
  125. {
  126. Message = string.Format("处理数据超时{0}秒,结束当前操作!", _MaxWaitTime / 1000);
  127. this.Close();
  128. }
  129. }
  130. else
  131. {
  132. this.Message = string.Empty;
  133. this.Close();
  134. }
  135. }
  136. catch { }
  137. }
  138. private void frmWaitingBox_Shown(object sender, EventArgs e)
  139. {
  140. _AsyncResult = _Method.BeginInvoke(null, null, null, null);
  141. //Effect
  142. if (FormEffectEnable)
  143. {
  144. _Timer.Start();
  145. }
  146. else
  147. this.Opacity = 1;
  148. }
  149. private void frmWaitingBox_FormClosing(object sender, FormClosingEventArgs e)
  150. {
  151. if (FormEffectEnable)
  152. {
  153. if(this.Opacity>=1)
  154. e.Cancel = true;
  155. _Timer.Start();
  156. }
  157. }
  158. private void _Timer_Tick(object sender, EventArgs e)
  159. {
  160. try
  161. {
  162. if (_IsShown)
  163. {
  164. if (this.Opacity >= 1)
  165. {
  166. _Timer.Stop();
  167. _IsShown = false;
  168. }
  169. this.Opacity += 1.00 / _EffectCount;
  170. }
  171. else
  172. {
  173. if (this.Opacity <= 0)
  174. {
  175. _Timer.Stop();
  176. _IsShown = true;
  177. this.Close();
  178. }
  179. this.Opacity -= 1.00 / _EffectCount;
  180. }
  181. }
  182. catch { }
  183. }
  184. #endregion
  185. }
  186. }