using System;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MaterialSkin.Controls
{
///
/// Material design-like progress bar
///
public class MaterialProgressBar : ProgressBar, IMaterialControl
{
private int _animation;
///
/// Initializes a new instance of the class.
///
public MaterialProgressBar()
{
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
Timer timer = new Timer();
timer.Interval = 33;
timer.Tick += Timer_Tick;
timer.Enabled = true;
}
private void Timer_Tick(object sender, EventArgs e)
{
Invalidate();
}
///
/// Gets or sets the depth.
///
///
/// The depth.
///
[Browsable(false)]
public int Depth { get; set; }
///
/// Gets the skin manager.
///
///
/// The skin manager.
///
[Browsable(false)]
public MaterialSkinManager SkinManager => MaterialSkinManager.Instance;
///
/// Gets or sets the state of the mouse.
///
///
/// The state of the mouse.
///
[Browsable(false)]
public MouseState MouseState { get; set; }
///
/// Performs the work of setting the specified bounds of this control.
///
/// The new property value of the control.
/// The new property value of the control.
/// The new property value of the control.
/// The new property value of the control.
/// A bitwise combination of the values.
protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
{
base.SetBoundsCore(x, y, width, 5, specified);
}
protected override void CreateHandle()
{
base.CreateHandle();
if (Style == ProgressBarStyle.Marquee)
StartAnimation();
}
private void StartAnimation()
{
_animation = 0;
}
///
/// Raises the event.
///
/// A that contains the event data.
protected override void OnPaint(PaintEventArgs e)
{
if (Style == ProgressBarStyle.Marquee)
{
try
{
var animationProgress = (int)(e.ClipRectangle.Width * ((double)_animation / Maximum));
var doneProgress = (int)(e.ClipRectangle.Width * ((double)Value / Maximum));
if (doneProgress + animationProgress > e.ClipRectangle.Width)
doneProgress = e.ClipRectangle.Width - animationProgress;
_animation+=Step;
if (_animation >= 100) _animation = -Value;
e.Graphics.FillRectangle(SkinManager.GetDisabledOrHintBrush(), 0, 0, e.ClipRectangle.Width, e.ClipRectangle.Height);
e.Graphics.FillRectangle(SkinManager.ColorScheme.PrimaryBrush, animationProgress, 0, doneProgress, e.ClipRectangle.Height);
//Task.Delay(33).ContinueWith((x) =>
//{
// try
// {
// System.Action a = () => { };
// if (InvokeRequired)
// Invoke(a);
// else
// a();
// }catch{}
//});
}
catch (Exception exception)
{
}
}
else
{
var doneProgress = (int)(e.ClipRectangle.Width * ((double)Value / Maximum));
e.Graphics.FillRectangle(SkinManager.ColorScheme.PrimaryBrush, 0, 0, doneProgress, e.ClipRectangle.Height);
e.Graphics.FillRectangle(SkinManager.GetDisabledOrHintBrush(), doneProgress, 0, e.ClipRectangle.Width, e.ClipRectangle.Height);
}
}
}
}