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);
}
///
/// расчитывание значения свойства "Value" взависимости от ширины ProgressBar
///
///
private int CalculateProgressRectSize(Rectangle Rect)
{
int Margin = _ValueMaximum - ValueMinimum;
return Rect.Width * (int)ProgressAnim.Value / Margin;
}
#region <Рисование объектов>
///
/// Рисование основы
///
private void DrawBase(Graphics graph,Rectangle rect)
{
graph.DrawRectangle(new Pen(BackColor), rect);
graph.FillRectangle(new SolidBrush(BackColor), rect);
}
///
/// рисование ProgressBar
///
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 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
}
}