LoadingCircle.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. //#####################################################################################
  2. //★★★★★★★ http://www.cnpopsoft.com [华普软件] ★★★★★★★
  3. //★★★★★★★ 华普软件-VB、C#专业论文与源码荟萃! ★★★★★★★
  4. //#####################################################################################
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Data;
  9. using System.Drawing;
  10. using System.Drawing.Drawing2D;
  11. using System.Text;
  12. using System.Windows.Forms;
  13. namespace UAS_LabelMachine.CustomControl.SetLoading
  14. {
  15. public partial class LoadingCircle : Control
  16. {
  17. #region 常数
  18. private const double NumberOfDegreesInCircle = 360;
  19. private const double NumberOfDegreesInHalfCircle = NumberOfDegreesInCircle / 2;
  20. private const int DefaultInnerCircleRadius = 8;
  21. private const int DefaultOuterCircleRadius = 10;
  22. private const int DefaultNumberOfSpoke = 10;
  23. private const int DefaultSpokeThickness = 4;
  24. private readonly Color DefaultColor = Color.DarkGray;
  25. private const int MacOSXInnerCircleRadius = 5;
  26. private const int MacOSXOuterCircleRadius = 11;
  27. private const int MacOSXNumberOfSpoke = 12;
  28. private const int MacOSXSpokeThickness = 2;
  29. private const int FireFoxInnerCircleRadius = 6;
  30. private const int FireFoxOuterCircleRadius = 7;
  31. private const int FireFoxNumberOfSpoke = 9;
  32. private const int FireFoxSpokeThickness = 4;
  33. private const int IE7InnerCircleRadius = 8;
  34. private const int IE7OuterCircleRadius = 9;
  35. private const int IE7NumberOfSpoke = 24;
  36. private const int IE7SpokeThickness = 4;
  37. #endregion
  38. #region 枚举
  39. public enum StylePresets
  40. {
  41. MacOSX,
  42. Firefox,
  43. IE7,
  44. Custom
  45. }
  46. #endregion
  47. #region 局部变量
  48. private Timer m_Timer;
  49. private bool m_IsTimerActive;
  50. private int m_NumberOfSpoke;
  51. private int m_SpokeThickness;
  52. private int m_ProgressValue;
  53. private int m_OuterCircleRadius;
  54. private int m_InnerCircleRadius;
  55. private PointF m_CenterPoint;
  56. private Color m_Color;
  57. private Color[] m_Colors;
  58. private double[] m_Angles;
  59. private StylePresets m_StylePreset;
  60. #endregion
  61. #region 属性
  62. /// <summary>
  63. /// 获取和设置控件高亮色
  64. /// </summary>
  65. /// <value>高亮色</value>
  66. [TypeConverter("System.Drawing.ColorConverter"),
  67. Category("LoadingCircle"),
  68. Description("获取和设置控件高亮色")]
  69. public Color Color
  70. {
  71. get
  72. {
  73. return m_Color;
  74. }
  75. set
  76. {
  77. m_Color = value;
  78. GenerateColorsPallet();
  79. Invalidate();
  80. }
  81. }
  82. /// <summary>
  83. /// 获取和设置外围半径
  84. /// </summary>
  85. /// <value>外围半径</value>
  86. [System.ComponentModel.Description("获取和设置外围半径"),
  87. System.ComponentModel.Category("LoadingCircle")]
  88. public int OuterCircleRadius
  89. {
  90. get
  91. {
  92. if (m_OuterCircleRadius == 0)
  93. m_OuterCircleRadius = DefaultOuterCircleRadius;
  94. return m_OuterCircleRadius;
  95. }
  96. set
  97. {
  98. m_OuterCircleRadius = value;
  99. Invalidate();
  100. }
  101. }
  102. /// <summary>
  103. /// 获取和设置内圆半径
  104. /// </summary>
  105. /// <value>内圆半径</value>
  106. [System.ComponentModel.Description("获取和设置内圆半径"),
  107. System.ComponentModel.Category("LoadingCircle")]
  108. public int InnerCircleRadius
  109. {
  110. get
  111. {
  112. if (m_InnerCircleRadius == 0)
  113. m_InnerCircleRadius = DefaultInnerCircleRadius;
  114. return m_InnerCircleRadius;
  115. }
  116. set
  117. {
  118. m_InnerCircleRadius = value;
  119. Invalidate();
  120. }
  121. }
  122. /// <summary>
  123. /// 获取和设置辐条数量
  124. /// </summary>
  125. /// <value>辐条数量</value>
  126. [System.ComponentModel.Description("获取和设置辐条数量"),
  127. System.ComponentModel.Category("LoadingCircle")]
  128. public int NumberSpoke
  129. {
  130. get
  131. {
  132. if (m_NumberOfSpoke == 0)
  133. m_NumberOfSpoke = DefaultNumberOfSpoke;
  134. return m_NumberOfSpoke;
  135. }
  136. set
  137. {
  138. if (m_NumberOfSpoke != value && m_NumberOfSpoke > 0)
  139. {
  140. m_NumberOfSpoke = value;
  141. GenerateColorsPallet();
  142. GetSpokesAngles();
  143. Invalidate();
  144. }
  145. }
  146. }
  147. /// <summary>
  148. /// 获取和设置一个布尔值,表示当前控件<see cref="T:LoadingCircle"/>是否激活。
  149. /// </summary>
  150. /// <value><c>true</c> 表示激活;否则,为<c>false</c>。</value>
  151. [System.ComponentModel.Description("获取和设置一个布尔值,表示当前控件是否激活。"),
  152. System.ComponentModel.Category("LoadingCircle")]
  153. public bool Active
  154. {
  155. get
  156. {
  157. return m_IsTimerActive;
  158. }
  159. set
  160. {
  161. m_IsTimerActive = value;
  162. ActiveTimer();
  163. }
  164. }
  165. /// <summary>
  166. /// 获取和设置辐条粗细程度。
  167. /// </summary>
  168. /// <value>辐条粗细值</value>
  169. [System.ComponentModel.Description("获取和设置辐条粗细程度。"),
  170. System.ComponentModel.Category("LoadingCircle")]
  171. public int SpokeThickness
  172. {
  173. get
  174. {
  175. if (m_SpokeThickness <= 0)
  176. m_SpokeThickness = DefaultSpokeThickness;
  177. return m_SpokeThickness;
  178. }
  179. set
  180. {
  181. m_SpokeThickness = value;
  182. Invalidate();
  183. }
  184. }
  185. /// <summary>
  186. /// 获取和设置旋转速度。
  187. /// </summary>
  188. /// <value>旋转速度</value>
  189. [System.ComponentModel.Description("获取和设置旋转速度。"),
  190. System.ComponentModel.Category("LoadingCircle")]
  191. public int RotationSpeed
  192. {
  193. get
  194. {
  195. return m_Timer.Interval;
  196. }
  197. set
  198. {
  199. if (value > 0)
  200. m_Timer.Interval = value;
  201. }
  202. }
  203. /// <summary>
  204. /// 快速设置预定义风格。
  205. /// </summary>
  206. /// <value>风格的值</value>
  207. [Category("LoadingCircle"),
  208. Description("快速设置预定义风格。"),
  209. DefaultValue(typeof(StylePresets), "Custom")]
  210. public StylePresets StylePreset
  211. {
  212. get { return m_StylePreset; }
  213. set
  214. {
  215. m_StylePreset = value;
  216. switch (m_StylePreset)
  217. {
  218. case StylePresets.MacOSX:
  219. SetCircleAppearance(MacOSXNumberOfSpoke,
  220. MacOSXSpokeThickness, MacOSXInnerCircleRadius,
  221. MacOSXOuterCircleRadius);
  222. break;
  223. case StylePresets.Firefox:
  224. SetCircleAppearance(FireFoxNumberOfSpoke,
  225. FireFoxSpokeThickness, FireFoxInnerCircleRadius,
  226. FireFoxOuterCircleRadius);
  227. break;
  228. case StylePresets.IE7:
  229. SetCircleAppearance(IE7NumberOfSpoke,
  230. IE7SpokeThickness, IE7InnerCircleRadius,
  231. IE7OuterCircleRadius);
  232. break;
  233. case StylePresets.Custom:
  234. SetCircleAppearance(DefaultNumberOfSpoke,
  235. DefaultSpokeThickness,
  236. DefaultInnerCircleRadius,
  237. DefaultOuterCircleRadius);
  238. break;
  239. }
  240. }
  241. }
  242. #endregion
  243. #region 构造函数及事件处理
  244. public LoadingCircle()
  245. {
  246. SetStyle(ControlStyles.UserPaint, true);
  247. SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
  248. SetStyle(ControlStyles.ResizeRedraw, true);
  249. SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  250. m_Color = DefaultColor;
  251. GenerateColorsPallet();
  252. GetSpokesAngles();
  253. GetControlCenterPoint();
  254. m_Timer = new Timer();
  255. m_Timer.Tick += new EventHandler(aTimer_Tick);
  256. ActiveTimer();
  257. this.Resize += new EventHandler(LoadingCircle_Resize);
  258. }
  259. void LoadingCircle_Resize(object sender, EventArgs e)
  260. {
  261. GetControlCenterPoint();
  262. }
  263. void aTimer_Tick(object sender, EventArgs e)
  264. {
  265. m_ProgressValue = ++m_ProgressValue % m_NumberOfSpoke;
  266. Invalidate();
  267. }
  268. protected override void OnPaint(PaintEventArgs e)
  269. {
  270. if (m_NumberOfSpoke > 0)
  271. {
  272. e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
  273. int intPosition = m_ProgressValue;
  274. for (int intCounter = 0; intCounter < m_NumberOfSpoke; intCounter++)
  275. {
  276. intPosition = intPosition % m_NumberOfSpoke;
  277. DrawLine(e.Graphics,
  278. GetCoordinate(m_CenterPoint, m_InnerCircleRadius, m_Angles[intPosition]),
  279. GetCoordinate(m_CenterPoint, m_OuterCircleRadius, m_Angles[intPosition]),
  280. m_Colors[intCounter], m_SpokeThickness);
  281. intPosition++;
  282. }
  283. }
  284. base.OnPaint(e);
  285. }
  286. #endregion
  287. #region 局部方法
  288. private Color Darken(Color _objColor, int _intPercent)
  289. {
  290. int intRed = _objColor.R;
  291. int intGreen = _objColor.G;
  292. int intBlue = _objColor.B;
  293. return Color.FromArgb(_intPercent, Math.Min(intRed, byte.MaxValue), Math.Min(intGreen, byte.MaxValue), Math.Min(intBlue, byte.MaxValue));
  294. }
  295. private void GenerateColorsPallet()
  296. {
  297. m_Colors = GenerateColorsPallet(m_Color, Active, m_NumberOfSpoke);
  298. }
  299. private Color[] GenerateColorsPallet(Color _objColor, bool _blnShadeColor, int _intNbSpoke)
  300. {
  301. Color[] objColors = new Color[NumberSpoke];
  302. byte bytIncrement = (byte)(byte.MaxValue / NumberSpoke);
  303. byte PERCENTAGE_OF_DARKEN = 0;
  304. for (int intCursor = 0; intCursor < NumberSpoke; intCursor++)
  305. {
  306. if (_blnShadeColor)
  307. {
  308. if (intCursor == 0 || intCursor < NumberSpoke - _intNbSpoke)
  309. objColors[intCursor] = _objColor;
  310. else
  311. {
  312. PERCENTAGE_OF_DARKEN += bytIncrement;
  313. if (PERCENTAGE_OF_DARKEN > byte.MaxValue)
  314. PERCENTAGE_OF_DARKEN = byte.MaxValue;
  315. objColors[intCursor] = Darken(_objColor, PERCENTAGE_OF_DARKEN);
  316. }
  317. }
  318. else
  319. objColors[intCursor] = _objColor;
  320. }
  321. return objColors;
  322. }
  323. private void GetControlCenterPoint()
  324. {
  325. m_CenterPoint = GetControlCenterPoint(this);
  326. }
  327. private PointF GetControlCenterPoint(Control _objControl)
  328. {
  329. return new PointF(_objControl.Width / 2, _objControl.Height / 2 - 1);
  330. }
  331. private void DrawLine(Graphics _objGraphics, PointF _objPointOne, PointF _objPointTwo,
  332. Color _objColor, int _intLineThickness)
  333. {
  334. using (Pen objPen = new Pen(new SolidBrush(_objColor), _intLineThickness))
  335. {
  336. objPen.StartCap = LineCap.Round;
  337. objPen.EndCap = LineCap.Round;
  338. _objGraphics.DrawLine(objPen, _objPointOne, _objPointTwo);
  339. }
  340. }
  341. private PointF GetCoordinate(PointF _objCircleCenter, int _intRadius, double _dblAngle)
  342. {
  343. double dblAngle = Math.PI * _dblAngle / NumberOfDegreesInHalfCircle;
  344. return new PointF(_objCircleCenter.X + _intRadius * (float)Math.Cos(dblAngle),
  345. _objCircleCenter.Y + _intRadius * (float)Math.Sin(dblAngle));
  346. }
  347. private void GetSpokesAngles()
  348. {
  349. m_Angles = GetSpokesAngles(NumberSpoke);
  350. }
  351. private double[] GetSpokesAngles(int _intNumberSpoke)
  352. {
  353. double[] Angles = new double[_intNumberSpoke];
  354. double dblAngle = (double)NumberOfDegreesInCircle / _intNumberSpoke;
  355. for (int shtCounter = 0; shtCounter < _intNumberSpoke; shtCounter++)
  356. Angles[shtCounter] = (shtCounter == 0 ? dblAngle : Angles[shtCounter - 1] + dblAngle);
  357. return Angles;
  358. }
  359. private void ActiveTimer()
  360. {
  361. if (m_IsTimerActive)
  362. m_Timer.Start();
  363. else
  364. {
  365. m_Timer.Stop();
  366. m_ProgressValue = 0;
  367. }
  368. GenerateColorsPallet();
  369. Invalidate();
  370. }
  371. #endregion
  372. #region 全局方法
  373. /// <summary>
  374. /// 获取适合控件区域的矩形大小。
  375. /// </summary>
  376. /// <param name="proposedSize">The custom-sized area for a control.</param>
  377. /// <returns>
  378. /// An ordered pair of type <see cref="T:System.Drawing.Size"></see> representing the width and height of a rectangle.
  379. /// </returns>
  380. public override Size GetPreferredSize(Size proposedSize)
  381. {
  382. proposedSize.Width =
  383. (m_OuterCircleRadius + m_SpokeThickness) * 2;
  384. return proposedSize;
  385. }
  386. /// <summary>
  387. /// 设置控件的外观
  388. /// </summary>
  389. /// <param name="numberSpoke">条数</param>
  390. /// <param name="spokeThickness">粗细</param>
  391. /// <param name="innerCircleRadius">内圆半径</param>
  392. /// <param name="outerCircleRadius">外圆半径</param>
  393. public void SetCircleAppearance(int numberSpoke, int spokeThickness,
  394. int innerCircleRadius, int outerCircleRadius)
  395. {
  396. NumberSpoke = numberSpoke;
  397. SpokeThickness = spokeThickness;
  398. InnerCircleRadius = innerCircleRadius;
  399. OuterCircleRadius = outerCircleRadius;
  400. Invalidate();
  401. }
  402. #endregion
  403. }
  404. }