FDProgressBar.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows.Forms;
  7. using System.Drawing;
  8. using ZedGraph;
  9. using System.Drawing.Drawing2D;
  10. using System.ComponentModel;
  11. namespace ImpulseVision
  12. {
  13. [ToolboxItem(true)]
  14. class FDProgressBar : Control
  15. {
  16. #region <Переменные>
  17. Animation ProgressAnim = new Animation();
  18. #endregion
  19. #region <Свойства>
  20. public Color BorderColor { get; set; } = FlatColors.GrayDark;
  21. public Color BackColorProgressLeft { get; set; } = FlatColors.Green;
  22. public Color BackColorProgressRight { get; set; } = FlatColors.Red;
  23. public int _Value { get; set; } = 0;
  24. public int Value
  25. {
  26. get => _Value;
  27. set
  28. {
  29. if (value >= ValueMinimum && value <= _ValueMaximum)
  30. {
  31. _Value = value;
  32. if (Animator.IsWork == false)
  33. {
  34. ProgressAnim.Value = _Value;
  35. Invalidate();
  36. }
  37. else
  38. {
  39. ProgressAction(_Value);
  40. }
  41. }
  42. else
  43. {
  44. value = _Value;
  45. }
  46. }
  47. }
  48. public int _ValueMinimum { get; set; } = 0;
  49. public int ValueMinimum
  50. {
  51. get => _ValueMinimum;
  52. set
  53. {
  54. if(value < _ValueMaximum)
  55. {
  56. _ValueMinimum = value;
  57. if(_ValueMinimum > Value)
  58. {
  59. Value= _ValueMinimum;
  60. Invalidate();
  61. }
  62. }
  63. else
  64. {
  65. value = _ValueMinimum;
  66. }
  67. }
  68. }
  69. public int _ValueMaximum { get; set; } = 100;
  70. public int ValueMaximum
  71. {
  72. get => _ValueMaximum;
  73. set
  74. {
  75. if(value > _ValueMinimum)
  76. {
  77. _ValueMaximum = value;
  78. Invalidate();
  79. }
  80. else
  81. {
  82. value = _ValueMaximum;
  83. }
  84. }
  85. }
  86. public int Step { get; set; } = 10;
  87. #endregion
  88. public FDProgressBar()
  89. {
  90. SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor, true);
  91. DoubleBuffered = true;
  92. Size = new Size(200, 20);
  93. BackColor = FlatColors.GrayLight;
  94. }
  95. protected override void OnPaint(PaintEventArgs e)
  96. {
  97. base.OnPaint(e);
  98. Graphics Graph = e.Graphics;
  99. Graph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  100. //очистка холста
  101. Graph.Clear(Parent.BackColor);
  102. Rectangle RectBase = new Rectangle(0, 0, Width - 1, Height - 1);
  103. Rectangle RectProgress = new Rectangle(RectBase.X + 1, RectBase.Y + 1, CalculateProgressRectSize(RectBase) - 2, RectBase.Height - 2);
  104. //рисуем основу
  105. DrawBase(Graph, RectBase);
  106. //рисуем прогресс
  107. DrawProgress(Graph, RectProgress);
  108. }
  109. /// <summary>
  110. /// расчитывание значения свойства "Value" взависимости от ширины ProgressBar
  111. /// </summary>
  112. /// <returns></returns>
  113. private int CalculateProgressRectSize(Rectangle Rect)
  114. {
  115. int Margin = _ValueMaximum - ValueMinimum;
  116. return Rect.Width * (int)ProgressAnim.Value / Margin;
  117. }
  118. #region <Рисование объектов>
  119. /// <summary>
  120. /// Рисование основы
  121. /// </summary>
  122. private void DrawBase(Graphics graph,Rectangle rect)
  123. {
  124. graph.DrawRectangle(new Pen(BackColor), rect);
  125. graph.FillRectangle(new SolidBrush(BackColor), rect);
  126. }
  127. /// <summary>
  128. /// рисование ProgressBar
  129. /// </summary>
  130. private void DrawProgress(Graphics graph, Rectangle rectProgress)
  131. {
  132. //для градиента
  133. LinearGradientBrush LGB = new LinearGradientBrush(rectProgress, BackColorProgressLeft, BackColorProgressRight, 360);
  134. graph.DrawRectangle(new Pen(LGB), rectProgress);
  135. graph.FillRectangle(LGB, rectProgress);
  136. }
  137. #endregion
  138. #region <Запуск анимации>
  139. private void ProgressAction(int PIXELS)
  140. {
  141. ProgressAnim = new Animation("ProgressBar_" + Handle, Invalidate, ProgressAnim.Value, PIXELS);
  142. ProgressAnim.StepDivider = 8;
  143. Animator.Request(ProgressAnim, true);
  144. }
  145. #endregion
  146. #region <Public Методы>
  147. public bool PerformStep()
  148. {
  149. if(Value < ValueMaximum)
  150. {
  151. if(Value + Step >= ValueMaximum)
  152. {
  153. Value = ValueMaximum;
  154. return false;
  155. }
  156. else
  157. {
  158. Value += Step;
  159. return true;
  160. }
  161. }
  162. else
  163. {
  164. return false;
  165. }
  166. }
  167. public void ResetProgress()
  168. {
  169. Value = ValueMinimum;
  170. }
  171. #endregion
  172. }
  173. }