186 lines
5.3 KiB
C#
186 lines
5.3 KiB
C#
using System;
|
|
using System.ComponentModel;
|
|
using System.Drawing;
|
|
using System.Windows.Forms;
|
|
|
|
namespace Xylem.Common.Ui.GenesisToolBox.Ctls
|
|
{
|
|
public enum ProgressBarDisplayMode
|
|
{
|
|
NoText,
|
|
Percentage,
|
|
CurrProgress,
|
|
CustomText,
|
|
TextAndPercentage,
|
|
TextAndCurrProgress
|
|
}
|
|
|
|
public class TextProgressBar : ProgressBar
|
|
{
|
|
[Description("Font of the text on ProgressBar"), Category("Additional Options")]
|
|
public Font TextFont { get; set; } = new Font(FontFamily.GenericSerif, 11, FontStyle.Bold | FontStyle.Italic);
|
|
|
|
private SolidBrush _textColourBrush = (SolidBrush)Brushes.Black;
|
|
[Category("Additional Options")]
|
|
public Color TextColor
|
|
{
|
|
get
|
|
{
|
|
return _textColourBrush.Color;
|
|
}
|
|
set
|
|
{
|
|
_textColourBrush.Dispose();
|
|
_textColourBrush = new SolidBrush(value);
|
|
}
|
|
}
|
|
|
|
private SolidBrush _progressColourBrush = (SolidBrush)Brushes.LightGreen;
|
|
[Category("Additional Options"), Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
|
|
public Color ProgressColor
|
|
{
|
|
get
|
|
{
|
|
return _progressColourBrush.Color;
|
|
}
|
|
set
|
|
{
|
|
_progressColourBrush.Dispose();
|
|
_progressColourBrush = new SolidBrush(value);
|
|
}
|
|
}
|
|
|
|
private ProgressBarDisplayMode _visualMode = ProgressBarDisplayMode.CurrProgress;
|
|
[Category("Additional Options"), Browsable(true)]
|
|
public ProgressBarDisplayMode VisualMode
|
|
{
|
|
get
|
|
{
|
|
return _visualMode;
|
|
}
|
|
set
|
|
{
|
|
_visualMode = value;
|
|
Invalidate();//redraw component after change value from VS Properties section
|
|
}
|
|
}
|
|
|
|
private String _text = string.Empty;
|
|
|
|
[Description("If it's empty, % will be shown"), Category("Additional Options"), Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
|
|
public String CustomText
|
|
{
|
|
get
|
|
{
|
|
return _text;
|
|
}
|
|
set
|
|
{
|
|
_text = value;
|
|
Invalidate();//redraw component after change value from VS Properties section
|
|
}
|
|
}
|
|
|
|
private String _textToDraw
|
|
{
|
|
get
|
|
{
|
|
String text = CustomText;
|
|
|
|
switch (VisualMode)
|
|
{
|
|
case (ProgressBarDisplayMode.Percentage):
|
|
text = _percentageStr;
|
|
break;
|
|
case (ProgressBarDisplayMode.CurrProgress):
|
|
text = _currProgressStr;
|
|
break;
|
|
case (ProgressBarDisplayMode.TextAndCurrProgress):
|
|
text = $"{CustomText}: {_currProgressStr}";
|
|
break;
|
|
case (ProgressBarDisplayMode.TextAndPercentage):
|
|
text = $"{CustomText}: {_percentageStr}";
|
|
break;
|
|
}
|
|
|
|
return text;
|
|
}
|
|
set { }
|
|
}
|
|
|
|
private String _percentageStr { get { return $"{(Int32)((Single)Value - Minimum) / ((Single)Maximum - Minimum) * 100 } %"; } }
|
|
|
|
private String _currProgressStr
|
|
{
|
|
get
|
|
{
|
|
return $"{Value}/{Maximum}";
|
|
}
|
|
}
|
|
|
|
public TextProgressBar()
|
|
{
|
|
Value = Minimum;
|
|
FixComponentBlinking();
|
|
}
|
|
|
|
private void FixComponentBlinking()
|
|
{
|
|
SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
|
|
}
|
|
|
|
protected override void OnPaint(PaintEventArgs e)
|
|
{
|
|
Graphics g = e.Graphics;
|
|
|
|
DrawProgressBar(g);
|
|
|
|
DrawStringIfNeeded(g);
|
|
}
|
|
|
|
private void DrawProgressBar(Graphics g)
|
|
{
|
|
Rectangle rect = ClientRectangle;
|
|
|
|
ProgressBarRenderer.DrawHorizontalBar(g, rect);
|
|
|
|
rect.Inflate(-3, -3);
|
|
|
|
if (Value > 0)
|
|
{
|
|
Rectangle clip = new Rectangle(rect.X, rect.Y, (Int32)Math.Round(((Single)Value / Maximum) * rect.Width), rect.Height);
|
|
try
|
|
{
|
|
g.FillRectangle(_progressColourBrush, clip);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
private void DrawStringIfNeeded(Graphics g)
|
|
{
|
|
if (VisualMode != ProgressBarDisplayMode.NoText)
|
|
{
|
|
|
|
String text = _textToDraw;
|
|
|
|
SizeF len = g.MeasureString(text, TextFont);
|
|
|
|
Point location = new Point(((Width / 2) - (Int32)len.Width / 2), ((Height / 2) - (Int32)len.Height / 2));
|
|
|
|
g.DrawString(text, TextFont, (Brush)_textColourBrush, location);
|
|
}
|
|
}
|
|
|
|
public new void Dispose()
|
|
{
|
|
_textColourBrush.Dispose();
|
|
_progressColourBrush.Dispose();
|
|
base.Dispose();
|
|
}
|
|
}
|
|
} |