Created
April 17, 2018 21:30
-
-
Save mjs3339/60f9d468128a04571f80c7926a3c024d to your computer and use it in GitHub Desktop.
C# ProgressBar with Gradient, Mouse Adjustable Percentage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ProgressBarEx : PictureBox | |
{ | |
public enum AniMode | |
{ | |
Bounce, | |
LeftToRight | |
} | |
/// <summary> | |
/// Specifies how the bar is drawn either simple = standard or complex = including a graph at percent | |
/// </summary> | |
public enum DrawingMode | |
{ | |
Simple = 0, | |
Complex | |
} | |
/// <summary> | |
/// Specifies how the title should be drawn | |
/// </summary> | |
public enum GradientDrawingMode | |
{ | |
Solid = 0, | |
Gradient | |
} | |
/// <summary> | |
/// Specifies how the percentage value should be drawn | |
/// </summary> | |
public enum PercentageDrawingMode | |
{ | |
None = 0, | |
CenterWindow, | |
CenterBar, | |
Movable, | |
Follow | |
} | |
/// <summary> | |
/// Specifies if any text is to be drawn | |
/// </summary> | |
public enum TextDrawingMode | |
{ | |
None = 0, | |
Standard, | |
AsPercentage | |
} | |
/// <summary> | |
/// Specifies how the title should be drawn | |
/// </summary> | |
public enum TitleDrawingMode | |
{ | |
None = 0, | |
UpperLeft | |
} | |
/// <summary> | |
/// Specifies how the title should be justified | |
/// </summary> | |
public enum TitleJustify | |
{ | |
Right = 0, | |
Left, | |
Center | |
} | |
private readonly float[] _GradientPositions; | |
private readonly Timers.Timer _timer = new Timers.Timer(); | |
private readonly ColorBlend gradientBlender; | |
private readonly ColorBlend gradientBlenderBG; | |
private readonly List<element> Values = new List<element>(); | |
private readonly SolidBrush writingBrush = new SolidBrush(Color.White); | |
private readonly List<float> xl = new List<float>(); | |
private int _AnimationSpeed = 100; | |
private float _AnimationStep = .02f; | |
public AniMode _AniMode = AniMode.Bounce; | |
private Color _borderColor = Color.Gray; | |
private bool _borderShow; | |
private int _borderWidth = 1; | |
private LinearGradientBrush _Drawer; | |
private LinearGradientBrush _DrawerBG; | |
private bool _EnableAnimation; | |
private bool _EnableForeColorEdit; | |
private bool _EnableMouseSet; | |
private bool _EnableProgressRectagle; | |
private Color _FontColor = Color.White; | |
private float _FontReduction = 1; | |
private LinearGradientMode _GradientDirection = | |
LinearGradientMode.Vertical; | |
private GradientDrawingMode _GradientDrawingModeBackground = | |
GradientDrawingMode.Gradient; | |
private GradientDrawingMode _GradientDrawingModeForeground = | |
GradientDrawingMode.Gradient; | |
private float[] _GradientLimit; | |
private float _limit = 100; | |
private bool _MouseDown; | |
private string _text = ""; | |
public List<string> _Title = new List<string>(); | |
private TitleDrawingMode _TitleDrawingMode = TitleDrawingMode.None; | |
private TitleJustify _TitleJustify = TitleJustify.Left; | |
private float _value; | |
private float anipos = 0.6f; | |
private IContainer components; | |
private direction dir = direction.up; | |
private Color drawingColor = Color.Lime; | |
private Color drawingColorBG = Color.LightGray; | |
private float drawingWidth; | |
private float EachStep; | |
private Pen FilledPlotPen; | |
private DrawingMode GraphDrawingMode = DrawingMode.Simple; | |
private float inc; | |
private float maxValue = 100; | |
private float minValue; | |
private PercentageDrawingMode percentageDrawingMode = | |
PercentageDrawingMode.CenterWindow; | |
private Pen PlotPen; | |
private float stepValue = 1; | |
private TextDrawingMode textDrawingMode = TextDrawingMode.Standard; | |
private Font writingFont; | |
private float x; | |
/// <summary> | |
/// Initialize new instance of the ProgressEx control | |
/// </summary> | |
public ProgressBarEx() | |
{ | |
InitializeComponent(); | |
//////////////////graph | |
PlotPen = new Pen(Color.Red, 1); | |
PlotPen.Brush = new SolidBrush(Color.FromArgb(255, PlotPen.Color)); | |
FilledPlotPen = new Pen(Color.Red, 1) {Brush = new SolidBrush(Color.FromArgb(255 / 2, PlotPen.Color))}; | |
////////////////// | |
_timer.Elapsed += _timer_Elapsed; | |
_timer.Interval = _AnimationSpeed; | |
_timer.Enabled = false; | |
_GradientLimit = new float[2]; | |
_GradientLimit[0] = .3f; | |
_GradientLimit[1] = .7f; | |
_GradientPositions = new float[3]; | |
_GradientPositions[0] = 0f; | |
_GradientPositions[1] = .6f; | |
_GradientPositions[2] = 1f; | |
gradientBlender = new ColorBlend(3) {Positions = _GradientPositions}; | |
gradientBlenderBG = new ColorBlend(3) {Positions = _GradientPositions}; | |
DrawingColor = Color.Lime; | |
drawingColorBG = Color.LightGray; | |
gradientBlender.Colors[0] = ControlPaint.Dark(DrawingColor); | |
gradientBlender.Colors[1] = ControlPaint.Light(DrawingColor); | |
gradientBlender.Colors[2] = ControlPaint.Dark(DrawingColor); | |
gradientBlenderBG.Colors[0] = ControlPaint.Dark(drawingColorBG); | |
gradientBlenderBG.Colors[1] = ControlPaint.Light(drawingColorBG); | |
gradientBlenderBG.Colors[2] = ControlPaint.Dark(drawingColorBG); | |
writingFont = Font; | |
_Drawer = new LinearGradientBrush(ClientRectangle, Color.Black, | |
Color.White, _GradientDirection); | |
_DrawerBG = new LinearGradientBrush(ClientRectangle, Color.Black, | |
Color.White, _GradientDirection); | |
Update(); | |
Invalidate(); | |
} | |
/// <summary> | |
/// Enable or disable the border | |
/// </summary> | |
[Description("Enable or disable the border")] | |
public bool BorderShow | |
{ | |
get => _borderShow; | |
set | |
{ | |
_borderShow = value; | |
Invalidate(); | |
} | |
} | |
/// <summary> | |
/// Sets the color of the border | |
/// </summary> | |
[Description("Sets the color of the border")] | |
public Color BorderColor | |
{ | |
get => _borderColor; | |
set | |
{ | |
_borderColor = value; | |
Invalidate(); | |
} | |
} | |
/// <summary> | |
/// Adjusts the border width | |
/// </summary> | |
[Description("Adjusts the border width")] | |
public int BorderWidth | |
{ | |
get => _borderWidth; | |
set | |
{ | |
_borderWidth = value; | |
Invalidate(); | |
} | |
} | |
/// <summary> | |
/// Set or Get the lower,center,upper bounds for the gradient brush | |
/// </summary> | |
[Category("Behavior")] | |
[Description( | |
"Set or Get the lower,center,upper bounds for the gradient brush")] | |
public float[] GradientPositions | |
{ | |
get => _GradientPositions; | |
set | |
{ | |
_GradientPositions[0] = value[0]; | |
_GradientPositions[1] = value[1]; | |
_GradientPositions[2] = value[2]; | |
gradientBlender.Positions = _GradientPositions; | |
gradientBlenderBG.Positions = _GradientPositions; | |
Invalidate(true); | |
} | |
} | |
/// <summary> | |
/// Set or Get the direction of the gradient | |
/// </summary> | |
[Category("Behavior")] | |
[Description("Set or Get the direction of the gradient")] | |
public LinearGradientMode GradientDirection | |
{ | |
get => _GradientDirection; | |
set | |
{ | |
_GradientDirection = value; | |
Invalidate(true); | |
} | |
} | |
/// <summary> | |
/// Set or Get the lower gradient animation limit | |
/// </summary> | |
[Category("Behavior")] | |
[Description("Set or Get the lower gradient animation limit")] | |
public float[] GradientLimit | |
{ | |
get => _GradientLimit; | |
set | |
{ | |
_GradientLimit = value; | |
Invalidate(true); | |
} | |
} | |
/// <summary> | |
/// Set or Get the animation mode | |
/// </summary> | |
[Category("Behavior")] | |
[Description("Set or Get the animation mode")] | |
public AniMode AnimationMode | |
{ | |
get => _AniMode; | |
set | |
{ | |
_AniMode = value; | |
Invalidate(true); | |
} | |
} | |
/// <summary> | |
/// Set or Get the step rate between gradient animation steps | |
/// </summary> | |
[Category("Behavior")] | |
[Description("Set or Get the step rate between gradient animation steps" | |
)] | |
public float AnimationStep | |
{ | |
get => _AnimationStep; | |
set | |
{ | |
_AnimationStep = value; | |
Invalidate(true); | |
} | |
} | |
/// <summary> | |
/// Set or Get the Animation speed | |
/// </summary> | |
[Category("Behavior")] | |
[Description("Set or Get the Animation speed")] | |
public int AnimationSpeed | |
{ | |
get => _AnimationSpeed; | |
set | |
{ | |
_AnimationSpeed = value; | |
_timer.Interval = _AnimationSpeed; | |
Invalidate(true); | |
} | |
} | |
/// <summary> | |
/// If Enabled draws a rectangle around the progress area | |
/// </summary> | |
[Category("Behavior")] | |
[Description("If Enabled draws a rectangle around the progress area")] | |
public bool EnableProgressRectagle | |
{ | |
get => _EnableProgressRectagle; | |
set | |
{ | |
_EnableProgressRectagle = value; | |
Invalidate(true); | |
} | |
} | |
/// <summary> | |
/// Enable gradient animation | |
/// </summary> | |
[Category("Behavior")] | |
[Description("Enable gradient animation")] | |
public bool EnableAnimation | |
{ | |
get => _EnableAnimation; | |
set | |
{ | |
if (value) | |
{ | |
gradientBlender.Positions[1] = .1f; | |
gradientBlenderBG.Positions[1] = .1f; | |
} | |
else | |
{ | |
gradientBlender.Positions[1] = .6f; | |
gradientBlenderBG.Positions[1] = .6f; | |
} | |
_EnableAnimation = value; | |
_timer.Enabled = value; | |
Invalidate(true); | |
} | |
} | |
/// <summary> | |
/// Enable Mouse to drag percentage | |
/// </summary> | |
[Category("Behavior")] | |
[Description("Enable Mouse to drag percentage")] | |
public bool EnableMouseSet | |
{ | |
get => _EnableMouseSet; | |
set | |
{ | |
_EnableMouseSet = value; | |
Cursor = _EnableMouseSet ? Cursors.VSplit : Cursors.Default; | |
Invalidate(true); | |
} | |
} | |
/// <summary> | |
/// Enable Edit of the forecolor, default is auto edit | |
/// </summary> | |
[Category("Behavior")] | |
[Description("Enable Edit of the forecolor, default is auto edit")] | |
public bool EnableForeColorEdit | |
{ | |
get => _EnableForeColorEdit; | |
set | |
{ | |
_EnableForeColorEdit = value; | |
Invalidate(true); | |
} | |
} | |
/// <summary> | |
/// Gets or Sets Font color for drawing text | |
/// </summary> | |
[Category("Behavior")] | |
[Description("Gets or Sets Font color for drawing text")] | |
public Color FontColor | |
{ | |
get => _FontColor; | |
set | |
{ | |
_FontColor = value; | |
Invalidate(true); | |
} | |
} | |
/// <summary> | |
/// Gets or Sets Gradient Mode to either solid or Gradient | |
/// </summary> | |
[Category("Behavior")] | |
[Description("Gets or Sets Gradient Mode to either solid or Gradient")] | |
public GradientDrawingMode GradientModeForeground | |
{ | |
get => _GradientDrawingModeForeground; | |
set | |
{ | |
_GradientDrawingModeForeground = value; | |
if (DesignMode) Invalidate(true); | |
} | |
} | |
/// <summary> | |
/// Gets or Sets Gradient Mode to either solid or Gradient | |
/// </summary> | |
[Category("Behavior")] | |
[Description("Gets or Sets Gradient Mode to either solid or Gradient")] | |
public GradientDrawingMode GradientModeBackground | |
{ | |
get => _GradientDrawingModeBackground; | |
set | |
{ | |
_GradientDrawingModeBackground = value; | |
if (DesignMode) Invalidate(true); | |
} | |
} | |
/// <summary> | |
/// Get or Set the title justification | |
/// </summary> | |
[Category("Behavior")] | |
[Description("Get or Set the title justification")] | |
public TitleJustify TitleJustifification | |
{ | |
get => _TitleJustify; | |
set | |
{ | |
_TitleJustify = value; | |
Invalidate(true); | |
} | |
} | |
/// <summary> | |
/// Specify how to display the title value | |
/// </summary> | |
[Category("Behavior")] | |
[Description("Specify how to display the title value")] | |
public TitleDrawingMode TitleMode | |
{ | |
get => _TitleDrawingMode; | |
set | |
{ | |
_TitleDrawingMode = value; | |
Invalidate(true); | |
} | |
} | |
/// <summary> | |
/// Gets or Sets a value determine how to display Percentage value | |
/// </summary> | |
[Category("Behavior")] | |
[Description("Specify how to display the Percentage value")] | |
public PercentageDrawingMode PercentageMode | |
{ | |
get => percentageDrawingMode; | |
set | |
{ | |
percentageDrawingMode = value; | |
Invalidate(true); | |
} | |
} | |
/// <summary> | |
/// Gets or Sets a value determine how to display Percentage value | |
/// </summary> | |
[Category("Behavior")] | |
[Description("Specify if text is displayed")] | |
public TextDrawingMode TextMode | |
{ | |
get => textDrawingMode; | |
set | |
{ | |
textDrawingMode = value; | |
Invalidate(true); | |
} | |
} | |
/// <summary> | |
/// Gets or Sets a value determine how to display graph is or is not drawn | |
/// </summary> | |
[Category("Behavior")] | |
[Description( | |
"Gets or Sets a value determine how to display graph is or is not drawn" | |
)] | |
public DrawingMode GraphMode | |
{ | |
get => GraphDrawingMode; | |
set | |
{ | |
GraphDrawingMode = value; | |
if (DesignMode) Invalidate(true); | |
} | |
} | |
/// <summary> | |
/// Gets or Sets the color used to draw the Progress activities | |
/// </summary> | |
[Category("Appearance")] | |
[Description("Specify the color used to draw the progress activities")] | |
public Color DrawingColor | |
{ | |
get => drawingColor; | |
set | |
{ | |
drawingColor = value; | |
gradientBlender.Colors[0] = ControlPaint.Dark(value); | |
gradientBlender.Colors[1] = ControlPaint.Light(value); | |
gradientBlender.Colors[2] = ControlPaint.Dark(value); | |
Invalidate(true); | |
} | |
} | |
/// <summary> | |
/// Gets or Sets the color used to draw the Progress activities | |
/// </summary> | |
[Category("Appearance")] | |
[Description("Specify the color used to draw the progress activities")] | |
public Color DrawingColorBackground | |
{ | |
get => drawingColorBG; | |
set | |
{ | |
drawingColorBG = value; | |
gradientBlenderBG.Colors[0] = ControlPaint.Dark(value); | |
gradientBlenderBG.Colors[1] = ControlPaint.Light(value); | |
gradientBlenderBG.Colors[2] = ControlPaint.Dark(value); | |
Invalidate(true); | |
} | |
} | |
/// <summary> | |
/// Sets a limit for mouse scrolling | |
/// </summary> | |
[Category("Layout")] | |
[Description("Sets a limit for mouse scrolling")] | |
public float Limit | |
{ | |
get => _limit; | |
set | |
{ | |
_limit = value; | |
Invalidate(true); | |
} | |
} | |
/// <summary> | |
/// Gets or sets the maximum value of the range of the control. | |
/// </summary> | |
[Category("Layout")] | |
[Description("Specify the maximum value the progress can increased to")] | |
public float Maximum | |
{ | |
get => maxValue; | |
set | |
{ | |
maxValue = value; | |
Invalidate(true); | |
} | |
} | |
/// <summary> | |
/// Gets or sets the minimum value of the range of the control. | |
/// </summary> | |
[Category("Layout")] | |
public float Minimum | |
{ | |
get => minValue; | |
set | |
{ | |
minValue = value; | |
Invalidate(true); | |
} | |
} | |
/// <summary> | |
/// Sets the size reduction in font size i.e. .5 would be 1/2 the font size. | |
/// </summary> | |
[Category("Layout")] | |
[Description( | |
"Sets the size reduction in font size i.e. .5 would be 1/2 the font size." | |
)] | |
public float FontReduction | |
{ | |
get => _FontReduction; | |
set | |
{ | |
_FontReduction = value; | |
Invalidate(true); | |
} | |
} | |
/// <summary> | |
/// Gets or sets the amount by which a call to the System.Windows.Forms.ProgressBar. | |
/// StepForword method increases the current position of the progress bar. | |
/// </summary> | |
[Category("Layout")] | |
public float Step | |
{ | |
get => stepValue; | |
set | |
{ | |
stepValue = value; | |
Invalidate(true); | |
} | |
} | |
/// <summary> | |
/// Gets or sets the current position of the progress bar. | |
/// </summary> | |
/// <exception cref="System.ArgumentException"> | |
/// The value specified is greater than the value of | |
/// the System.Windows.Forms.ProgressBar.Maximum property. -or- The value specified is less | |
/// than the value of the System.Windows.Forms.ProgressBar.Minimum property | |
/// </exception> | |
[Category("Values")] | |
[Description("Gets or sets the current position of the progress bar.")] | |
public float Value | |
{ | |
get => _value; | |
set | |
{ | |
if (value > maxValue) | |
value = maxValue; | |
if (value < minValue) value = minValue; | |
_value = value; | |
Invalidate(true); | |
} | |
} | |
[Category("Text")] | |
[Description("Gets or sets the current text value")] | |
public string TextEx | |
{ | |
get => _text; | |
set | |
{ | |
_text = value; | |
Invalidate(true); | |
} | |
} | |
/// <summary> | |
/// Gets the Percent value the Progress activities reached | |
/// </summary> | |
public float Percent { get; private set; } | |
protected override void Dispose(bool disposing) | |
{ | |
if (disposing && components != null) components.Dispose(); | |
base.Dispose(disposing); | |
} | |
private void InitializeComponent() | |
{ | |
components = new Container(); | |
SuspendLayout(); | |
BackColor = Color.White; | |
Name = "ProgressBarEx"; | |
Size = new Drawing.Size(100, 21); | |
ResumeLayout(false); | |
} | |
protected override void OnMouseDown(MouseEventArgs e) | |
{ | |
if (!_EnableMouseSet) return; | |
Focus(); | |
base.OnMouseDown(e); | |
if (e.Button == MouseButtons.Left) MouseToPercent(e.X); | |
_MouseDown = true; | |
} | |
protected override void OnMouseUp(MouseEventArgs e) | |
{ | |
if (!_EnableMouseSet) return; | |
_MouseDown = false; | |
base.OnMouseUp(e); | |
} | |
protected override void OnMouseMove(MouseEventArgs e) | |
{ | |
if (!_EnableMouseSet) return; | |
if (_MouseDown) | |
{ | |
if (e.Button == MouseButtons.Left) MouseToPercent(e.X); | |
base.OnMouseMove(e); | |
} | |
} | |
private void MouseToPercent(int x) | |
{ | |
float i = x; | |
if (i > -1) | |
{ | |
float t = Width; | |
var p = i * 100 / t; | |
if (p < 100.01) | |
{ | |
if (p > _limit) p = _limit; | |
_value = p; | |
Invalidate(true); | |
} | |
} | |
} | |
private void _timer_Elapsed(object sender, ElapsedEventArgs e) | |
{ | |
if (_AniMode == AniMode.Bounce) | |
{ | |
if (anipos <= _GradientLimit[0]) dir = direction.up; | |
if (anipos >= _GradientLimit[1]) dir = direction.down; | |
if (dir == direction.down) anipos -= _AnimationStep; | |
if (dir == direction.up) anipos += _AnimationStep; | |
} | |
else if (_AniMode == AniMode.LeftToRight) | |
{ | |
anipos += _AnimationStep; | |
if (anipos >= _GradientLimit[1]) | |
anipos = _GradientLimit[0]; | |
} | |
gradientBlender.Positions[1] = anipos; | |
gradientBlenderBG.Positions[1] = anipos; | |
Invalidate(true); | |
} | |
///// <summary> | |
///// Clean up any resources being used. | |
///// </summary> | |
private static bool ColorHighOrLow(Color c) | |
{ | |
if (c.R == 255) return true; | |
if (c.G == 255) return true; | |
if (c.B == 255) return true; | |
var t = c.R + c.G + c.B; | |
if (t > 127 * 3) return true; | |
return false; | |
} | |
public void Reset() | |
{ | |
Values.Clear(); | |
xl.Clear(); | |
inc = 0; | |
x = 0; | |
EachStep = 0; | |
_value = 0; | |
} | |
public bool ComplexAdd(float gv, float pv) | |
{ | |
if (GraphMode == DrawingMode.Simple) | |
{ | |
Value = pv; | |
return true; | |
} | |
if (pv == 0 || EachStep == 0) | |
{ | |
EachStep = (float) Math.Round(100f / Width, 10); | |
inc = 0; | |
x = 0; | |
} | |
if (pv >= inc) | |
{ | |
AddElement(gv, pv); | |
x = x + 1; | |
inc = (float) Math.Round(pv + EachStep, 10); | |
Invalidate(true); | |
return true; | |
} | |
return false; | |
} | |
private void AddElement(float gv, float pv) | |
{ | |
var e = new element(); | |
var vector = MaxGraphValue(); | |
if (vector == 0) vector = gv; | |
e.y = | |
(float) | |
Math.Round(Math.Abs(gv) * ClientRectangle.Height / vector, 10); | |
e.GraphValue = gv; | |
e.ProgressValue = pv; | |
e.DrawingWidthAtPercent = x; | |
Values.Add(e); | |
_value = pv; | |
} | |
private float MaxGraphValue() | |
{ | |
return Values.Select(i => i.GraphValue).Concat(new float[] {0}).Max(); | |
} | |
private bool IsDuplicatex(IEnumerable<element> a, float x) | |
{ | |
return a.Any(v => (int) v.ProgressValue == (int) x); | |
} | |
protected override void OnPaint(PaintEventArgs e) | |
{ | |
base.OnPaint(e); | |
cPaint(e); | |
if (_borderShow) | |
e.Graphics.DrawRectangle( | |
new Pen(new SolidBrush(_borderColor), BorderWidth), | |
ClientRectangle.Left, | |
ClientRectangle.Top, | |
ClientRectangle.Width - 1, | |
ClientRectangle.Height - 1); | |
} | |
private void cPaint(PaintEventArgs e) | |
{ | |
if (!((_value >= minValue) & (_value <= maxValue))) return; | |
if (_EnableForeColorEdit == false) _FontColor = ColorHighOrLow(drawingColor) ? Color.Black : Color.White; | |
writingFont = Font; | |
drawingWidth = Width * _value / (maxValue - minValue); | |
Percent = _value / maxValue * 100; | |
if (_GradientDrawingModeBackground == | |
GradientDrawingMode.Gradient) | |
{ | |
_DrawerBG = new LinearGradientBrush(ClientRectangle, | |
Color.Black, Color.White, _GradientDirection) | |
{InterpolationColors = gradientBlenderBG}; | |
e.Graphics.FillRectangle(_DrawerBG, e.ClipRectangle); | |
} | |
if (_GradientDrawingModeForeground == | |
GradientDrawingMode.Gradient) | |
{ | |
_Drawer = new LinearGradientBrush(ClientRectangle, | |
Color.Black, Color.White, _GradientDirection) | |
{InterpolationColors = gradientBlender}; | |
e.Graphics.FillRectangle(_Drawer, 0, 0, drawingWidth, Height); | |
if (_EnableProgressRectagle) | |
{ | |
var RecPen = new Pen(Color.Black, 0f) {Brush = new SolidBrush(Color.FromArgb(240, Color.Black))}; | |
e.Graphics.DrawLine(RecPen, drawingWidth, 0f, | |
drawingWidth, Height); | |
} | |
} | |
else | |
{ | |
if (GraphDrawingMode == DrawingMode.Simple) | |
{ | |
var ProgressPen = new Pen(drawingColor) {Brush = new SolidBrush(Color.FromArgb(255, drawingColor))}; | |
e.Graphics.FillRectangle(ProgressPen.Brush, 0f, 0f, | |
drawingWidth, Height); | |
if (_EnableProgressRectagle) | |
{ | |
var RecPen = new Pen(Color.Black, 0f) {Brush = new SolidBrush(Color.FromArgb(240, Color.Black))}; | |
e.Graphics.DrawLine(RecPen, drawingWidth, 0f, | |
drawingWidth, Height); | |
} | |
} | |
else | |
{ | |
PlotGraph(e.Graphics); | |
} | |
} | |
if (textDrawingMode == TextDrawingMode.Standard || textDrawingMode == TextDrawingMode.AsPercentage) | |
using ( | |
writingFont = | |
new Font(writingFont.Name, | |
(float) (ClientRectangle.Height - 4) / 2 * | |
_FontReduction, | |
writingFont.Style, GraphicsUnit.Point)) | |
{ | |
var stringFormat = new StringFormat {Alignment = StringAlignment.Far}; | |
var WritingPen = new Pen(_FontColor) {Brush = new SolidBrush(Color.FromArgb(255, _FontColor))}; | |
if (percentageDrawingMode != PercentageDrawingMode.None) | |
{ | |
var st = ""; | |
switch (textDrawingMode) | |
{ | |
case TextDrawingMode.Standard: | |
st = $"{Percent:0.0}%"; | |
break; | |
case TextDrawingMode.AsPercentage: | |
st = $"{_text}"; | |
break; | |
} | |
var s = e.Graphics.MeasureString(st, writingFont); | |
if (percentageDrawingMode == | |
PercentageDrawingMode.Follow) | |
e.Graphics.DrawString(st, writingFont, | |
WritingPen.Brush, drawingWidth - writingFont.Size * st.Length, | |
e.ClipRectangle.Height / 2 - s.Height / 2); | |
else if (percentageDrawingMode == | |
PercentageDrawingMode.Movable) | |
e.Graphics.DrawString(st, writingFont, | |
WritingPen.Brush, drawingWidth, | |
e.ClipRectangle.Height / 2 - s.Height / 2); | |
else if (percentageDrawingMode == | |
PercentageDrawingMode.CenterWindow) | |
e.Graphics.DrawString(st, writingFont, | |
WritingPen.Brush, | |
ClientRectangle.Width / 2 - | |
writingFont.Size * st.Length / 2f, | |
ClientRectangle.Height / 2f - | |
writingFont.Height / 2f); | |
else if (percentageDrawingMode == | |
PercentageDrawingMode.CenterBar) | |
e.Graphics.DrawString(st, writingFont, | |
WritingPen.Brush, | |
drawingWidth / 2f - | |
writingFont.Size * st.Length / 2f, | |
ClientRectangle.Height / 2f - | |
writingFont.Height / 2f); | |
} | |
if (_Title.Count != 0) | |
if (_TitleDrawingMode != TitleDrawingMode.None) | |
{ | |
var s = e.Graphics.MeasureString("A", | |
writingFont); | |
if (_TitleDrawingMode == | |
TitleDrawingMode.UpperLeft) | |
try | |
{ | |
if (_TitleJustify == TitleJustify.Right) | |
{ | |
var maxw = | |
_Title.Select(T => (int) e.Graphics.MeasureString(T, writingFont).Width) | |
.Concat(new[] {0}) | |
.Max(); | |
for (var i = 0; | |
i < _Title.Count; | |
i++) | |
e.Graphics.DrawString( | |
_Title[i], writingFont, | |
WritingPen.Brush, maxw, | |
i * (s.Height - 4), | |
stringFormat); | |
} | |
if (_TitleJustify == TitleJustify.Center) | |
for (var i = 0; | |
i < _Title.Count; | |
i++) | |
{ | |
var tw = | |
(int) | |
e.Graphics.MeasureString | |
(_Title[i], | |
writingFont) | |
.Width; | |
e.Graphics.DrawString( | |
_Title[i], writingFont, | |
WritingPen.Brush, | |
e.ClipRectangle.Width / 2 + | |
tw / 2, i * (s.Height - 4), | |
stringFormat); | |
} | |
if (_TitleJustify == TitleJustify.Left) | |
for (var i = 0; | |
i < _Title.Count; | |
i++) | |
e.Graphics.DrawString( | |
_Title[i], writingFont, | |
WritingPen.Brush, 0f, | |
i * (s.Height - 4)); | |
} | |
catch | |
{ | |
} | |
} | |
} | |
} | |
private void PlotGraph(Graphics g) | |
{ | |
try | |
{ | |
PlotPen = new Pen(drawingColor, 1) {Brush = new SolidBrush(Color.FromArgb(255, drawingColor))}; | |
FilledPlotPen = new Pen(drawingColor, 1) {Brush = new SolidBrush(Color.FromArgb(255 / 2, PlotPen.Color))}; | |
foreach (var e in Values) | |
{ | |
g.DrawLine(FilledPlotPen, e.DrawingWidthAtPercent, | |
ClientRectangle.Top, e.DrawingWidthAtPercent, | |
ClientRectangle.Bottom); | |
g.DrawLine(PlotPen, e.DrawingWidthAtPercent, | |
ClientRectangle.Bottom, e.DrawingWidthAtPercent, | |
ClientRectangle.Height - e.y); | |
} | |
} | |
catch (Exception q) | |
{ | |
var s = q.Message; | |
} | |
} | |
/// <summary> | |
/// Increment the progress one step | |
/// </summary> | |
public void StepForword() | |
{ | |
if (_value + stepValue < maxValue) | |
{ | |
_value += stepValue; | |
Invalidate(true); | |
} | |
else | |
{ | |
_value = maxValue; | |
Invalidate(true); | |
} | |
} | |
/// <summary> | |
/// Decrement the progress one step | |
/// </summary> | |
public void StepBackword() | |
{ | |
if (_value - stepValue > minValue) | |
{ | |
_value -= stepValue; | |
Invalidate(true); | |
} | |
else | |
{ | |
_value = minValue; | |
Invalidate(true); | |
} | |
} | |
private enum direction | |
{ | |
up, | |
down | |
} | |
public class element | |
{ | |
public string Description; | |
public float DrawingWidthAtPercent; | |
public float GraphValue; | |
public float ProgressValue; | |
public float y; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment