4d1c55e8cea59d940047b5906bc16c60b296deb1.svn-base 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. namespace Core.Mes.ClientFrameWork
  9. {
  10. public partial class BusyControl : UserControl
  11. {
  12. #region Constants
  13. private const int DEFAULT_INTERVAL = 60;
  14. private readonly Color DEFAULT_TICK_COLOR = Color.FromArgb(58, 58, 58);
  15. private const int DEFAULT_TICK_WIDTH = 2;
  16. private const int MINIMUM_INNER_RADIUS = 4;
  17. private const int MINIMUM_OUTER_RADIUS = 8;
  18. private Size MINIMUM_CONTROL_SIZE = new Size(28, 28);
  19. private const int MINIMUM_PEN_WIDTH = 2;
  20. #endregion
  21. #region Enums
  22. public enum Direction
  23. {
  24. CLOCKWISE,
  25. ANTICLOCKWISE
  26. }
  27. #endregion
  28. #region Members
  29. private int m_Interval;
  30. Pen m_Pen = null;
  31. PointF m_CentrePt = new PointF();
  32. int m_InnerRadius = 0;
  33. int m_OuterRadius = 0;
  34. int m_StartAngle = 0;
  35. int m_AlphaStartValue = 0;
  36. int m_SpokesCount = 0;
  37. int m_AngleIncrement = 0;
  38. int m_AlphaDecrement = 0;
  39. Timer m_Timer = null;
  40. #endregion
  41. #region Properties
  42. /// <summary>
  43. /// Time interval for each tick.
  44. /// </summary>
  45. public int Interval
  46. {
  47. get
  48. {
  49. return m_Interval;
  50. }
  51. set
  52. {
  53. if (value > 0)
  54. {
  55. m_Interval = value;
  56. }
  57. else
  58. {
  59. m_Interval = DEFAULT_INTERVAL;
  60. }
  61. }
  62. }
  63. private Color m_TickColor;
  64. /// <summary>
  65. /// Color of the tick
  66. /// </summary>
  67. public Color TickColor
  68. {
  69. get { return m_TickColor; }
  70. set { m_TickColor = value; }
  71. }
  72. private Direction m_Rotation;
  73. /// <summary>
  74. /// Direction of rotation - CLOCKWISE/ANTICLOCKWISE
  75. /// </summary>
  76. public Direction Rotation
  77. {
  78. get { return m_Rotation; }
  79. set { m_Rotation = value; }
  80. }
  81. /// <summary>
  82. /// Angle at which the tick should start
  83. /// </summary>
  84. public int StartAngle
  85. {
  86. get
  87. {
  88. return m_StartAngle;
  89. }
  90. set
  91. {
  92. m_StartAngle = value;
  93. }
  94. }
  95. #endregion
  96. #region Construction/Initialization
  97. /// <summary>
  98. /// Ctor
  99. /// </summary>
  100. public BusyControl()
  101. {
  102. this.DoubleBuffered = true;
  103. InitializeComponent();
  104. this.BackColor = Color.Transparent;
  105. this.TickColor = DEFAULT_TICK_COLOR;
  106. this.MinimumSize = MINIMUM_CONTROL_SIZE;
  107. this.Interval = DEFAULT_INTERVAL;
  108. // Default rotation direction is clockwise
  109. this.Rotation = Direction.CLOCKWISE;
  110. // Default starting angle is 12 o'clock
  111. this.StartAngle = 270;
  112. // Default number of Spokes in this control is 12
  113. m_SpokesCount = 12;
  114. // Default alpha value of the first spoke is 255
  115. m_AlphaStartValue = 255;
  116. // Calculate the angle between adjacent spokes
  117. m_AngleIncrement = (int)(360 / m_SpokesCount);
  118. // Calculate the change in alpha between adjacent spokes
  119. m_AlphaDecrement = (int)((m_AlphaStartValue - 15) / m_SpokesCount);
  120. m_Pen = new Pen(TickColor, DEFAULT_TICK_WIDTH);
  121. m_Pen.EndCap = System.Drawing.Drawing2D.LineCap.Round;
  122. m_Pen.StartCap = System.Drawing.Drawing2D.LineCap.Round;
  123. m_Timer = new Timer();
  124. m_Timer.Interval = this.Interval;
  125. m_Timer.Tick += new EventHandler(OnTimerTick);
  126. }
  127. #endregion
  128. #region EventHandlers
  129. /// <summary>
  130. /// Handle the Tick event
  131. /// </summary>
  132. /// <param name="sender">Timer</param>
  133. /// <param name="e">EventArgs</param>
  134. void OnTimerTick(object sender, EventArgs e)
  135. {
  136. if (Rotation == Direction.CLOCKWISE)
  137. {
  138. m_StartAngle += m_AngleIncrement;
  139. if (m_StartAngle >= 360)
  140. m_StartAngle = 0;
  141. }
  142. else if (Rotation == Direction.ANTICLOCKWISE)
  143. {
  144. m_StartAngle -= m_AngleIncrement;
  145. if (m_StartAngle <= -360)
  146. m_StartAngle = 0;
  147. }
  148. Invalidate();
  149. }
  150. /// <summary>
  151. /// Handles the Paint Event of the control
  152. /// </summary>
  153. /// <param name="e">PaintEventArgs</param>
  154. protected override void OnPaint(PaintEventArgs e)
  155. {
  156. // All the painting will be handled by us.
  157. //base.OnPaint(e);
  158. e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
  159. e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  160. // Since the Rendering of the spokes is dependent upon the current size of the
  161. // control, the following calculation needs to be done within the Paint eventhandler.
  162. int alpha = m_AlphaStartValue;
  163. int angle = m_StartAngle;
  164. // Calculate the location around which the spokes will be drawn
  165. int width = (this.Width < this.Height) ? this.Width : this.Height;
  166. m_CentrePt = new PointF(this.Width / 2, this.Height / 2);
  167. // Calculate the width of the pen which will be used to draw the spokes
  168. m_Pen.Width = (int)(width / 15);
  169. if (m_Pen.Width < MINIMUM_PEN_WIDTH)
  170. m_Pen.Width = MINIMUM_PEN_WIDTH;
  171. // Calculate the inner and outer radii of the control. The radii should not be less than the
  172. // Minimum values
  173. m_InnerRadius = (int)(width * (140 / (float)800));
  174. if (m_InnerRadius < MINIMUM_INNER_RADIUS)
  175. m_InnerRadius = MINIMUM_INNER_RADIUS;
  176. m_OuterRadius = (int)(width * (250 / (float)800));
  177. if (m_OuterRadius < MINIMUM_OUTER_RADIUS)
  178. m_OuterRadius = MINIMUM_OUTER_RADIUS;
  179. // Render the spokes
  180. for (int i = 0; i < m_SpokesCount; i++)
  181. {
  182. PointF pt1 = new PointF(m_InnerRadius * (float)Math.Cos(ConvertDegreesToRadians(angle)), m_InnerRadius * (float)Math.Sin(ConvertDegreesToRadians(angle)));
  183. PointF pt2 = new PointF(m_OuterRadius * (float)Math.Cos(ConvertDegreesToRadians(angle)), m_OuterRadius * (float)Math.Sin(ConvertDegreesToRadians(angle)));
  184. pt1.X += m_CentrePt.X;
  185. pt1.Y += m_CentrePt.Y;
  186. pt2.X += m_CentrePt.X;
  187. pt2.Y += m_CentrePt.Y;
  188. m_Pen.Color = Color.FromArgb(alpha, this.TickColor);
  189. e.Graphics.DrawLine(m_Pen, pt1, pt2);
  190. if (Rotation == Direction.CLOCKWISE)
  191. {
  192. angle -= m_AngleIncrement;
  193. }
  194. else if (Rotation == Direction.ANTICLOCKWISE)
  195. {
  196. angle += m_AngleIncrement;
  197. }
  198. //if (i < 5)
  199. // alpha -= 45;
  200. alpha -= m_AlphaDecrement;
  201. }
  202. }
  203. #endregion
  204. #region Helpers
  205. /// <summary>
  206. /// Converts Degrees to Radians
  207. /// </summary>
  208. /// <param name="degrees">Degrees</param>
  209. /// <returns></returns>
  210. private double ConvertDegreesToRadians(int degrees)
  211. {
  212. return ((Math.PI / (double)180) * degrees);
  213. }
  214. #endregion
  215. #region APIs
  216. /// <summary>
  217. /// Start the Tick Control rotation
  218. /// </summary>
  219. public void Start()
  220. {
  221. if (m_Timer != null)
  222. {
  223. m_Timer.Interval = this.Interval;
  224. m_Timer.Enabled = true;
  225. }
  226. }
  227. /// <summary>
  228. /// Stop the Tick Control rotation
  229. /// </summary>
  230. public void Stop()
  231. {
  232. if (m_Timer != null)
  233. {
  234. m_Timer.Enabled = false;
  235. }
  236. }
  237. #endregion
  238. }
  239. }