123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Drawing;
- using ZedGraph;
- using System.Drawing.Drawing2D;
- using System.ComponentModel;
- namespace ImpulseVision
- {
- [ToolboxItem(true)]
- class FDProgressBar : Control
- {
- #region <Переменные>
- Animation ProgressAnim = new Animation();
- #endregion
- #region <Свойства>
- public Color BorderColor { get; set; } = FlatColors.GrayDark;
- public Color BackColorProgressLeft { get; set; } = FlatColors.Green;
- public Color BackColorProgressRight { get; set; } = FlatColors.Red;
- public int _Value { get; set; } = 0;
- public int Value
- {
- get => _Value;
- set
- {
- if (value >= ValueMinimum && value <= _ValueMaximum)
- {
- _Value = value;
- if (Animator.IsWork == false)
- {
- ProgressAnim.Value = _Value;
- Invalidate();
- }
- else
- {
- ProgressAction(_Value);
- }
- }
- else
- {
- value = _Value;
- }
- }
- }
- public int _ValueMinimum { get; set; } = 0;
- public int ValueMinimum
- {
- get => _ValueMinimum;
- set
- {
- if(value < _ValueMaximum)
- {
- _ValueMinimum = value;
- if(_ValueMinimum > Value)
- {
- Value= _ValueMinimum;
- Invalidate();
- }
- }
- else
- {
- value = _ValueMinimum;
- }
- }
- }
- public int _ValueMaximum { get; set; } = 100;
- public int ValueMaximum
- {
- get => _ValueMaximum;
- set
- {
- if(value > _ValueMinimum)
- {
- _ValueMaximum = value;
- Invalidate();
- }
- else
- {
- value = _ValueMaximum;
- }
- }
- }
- public int Step { get; set; } = 10;
- #endregion
- public FDProgressBar()
- {
- SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor, true);
- DoubleBuffered = true;
- Size = new Size(200, 20);
- BackColor = FlatColors.GrayLight;
- }
- protected override void OnPaint(PaintEventArgs e)
- {
- base.OnPaint(e);
- Graphics Graph = e.Graphics;
- Graph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
- //очистка холста
- Graph.Clear(Parent.BackColor);
- Rectangle RectBase = new Rectangle(0, 0, Width - 1, Height - 1);
- Rectangle RectProgress = new Rectangle(RectBase.X + 1, RectBase.Y + 1, CalculateProgressRectSize(RectBase) - 2, RectBase.Height - 2);
- //рисуем основу
- DrawBase(Graph, RectBase);
- //рисуем прогресс
- DrawProgress(Graph, RectProgress);
- }
- /// <summary>
- /// расчитывание значения свойства "Value" взависимости от ширины ProgressBar
- /// </summary>
- /// <returns></returns>
- private int CalculateProgressRectSize(Rectangle Rect)
- {
- int Margin = _ValueMaximum - ValueMinimum;
- return Rect.Width * (int)ProgressAnim.Value / Margin;
- }
- #region <Рисование объектов>
- /// <summary>
- /// Рисование основы
- /// </summary>
- private void DrawBase(Graphics graph,Rectangle rect)
- {
- graph.DrawRectangle(new Pen(BackColor), rect);
- graph.FillRectangle(new SolidBrush(BackColor), rect);
- }
- /// <summary>
- /// рисование ProgressBar
- /// </summary>
- private void DrawProgress(Graphics graph, Rectangle rectProgress)
- {
- //для градиента
- LinearGradientBrush LGB = new LinearGradientBrush(rectProgress, BackColorProgressLeft, BackColorProgressRight, 360);
- graph.DrawRectangle(new Pen(LGB), rectProgress);
- graph.FillRectangle(LGB, rectProgress);
- }
- #endregion
- #region <Запуск анимации>
- private void ProgressAction(int PIXELS)
- {
- ProgressAnim = new Animation("ProgressBar_" + Handle, Invalidate, ProgressAnim.Value, PIXELS);
- ProgressAnim.StepDivider = 8;
- Animator.Request(ProgressAnim, true);
- }
- #endregion
- #region <Public Методы>
- public bool PerformStep()
- {
- if(Value < ValueMaximum)
- {
- if(Value + Step >= ValueMaximum)
- {
- Value = ValueMaximum;
- return false;
- }
- else
- {
- Value += Step;
- return true;
- }
- }
- else
- {
- return false;
- }
- }
- public void ResetProgress()
- {
- Value = ValueMinimum;
- }
- #endregion
- }
- }
|