📄 progressbarex.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using UtilityLibrary.Win32;
using UtilityLibrary.General;
namespace UtilityLibrary.WinControls
{
// I put the delegate and the event handler
// outside the class so that the user does not have
// to prefix the progressbar class name to use this properties or delegate
// Putting them inside the class just make them hard to use
// Enumeration of property change events
public enum ProgressBarProperty
{
BackgroundColor,
ForegroundColor,
BorderColor,
Border3D,
EnableBorder3D,
Value,
Step,
Minimun,
Maximun,
Smooth,
ShowProgressText,
BackgroundBitmap,
ForegroundBitmap,
ProgressTextHiglightColor,
ProgressTextColor,
GradientStartColor,
GradientMiddleColor,
GradientEndColor,
}
// Declare the property change event signature
public delegate void ProgressBarPropertyChangedHandler(ProgressBarEx pogressBar, ProgressBarProperty prop);
/// <summary>
/// Summary description for FlatProgressBar.
/// </summary>
public class ProgressBarEx : System.Windows.Forms.Control
{
// We need to know how we are going to draw the progress bar
// this won't come from the user setting a flag but how the
// progress bar is constructed
private enum ProgressBarType { Standard, Bitmap, Gradient }
// Public events
public event ProgressBarPropertyChangedHandler PropertyChanged;
Color backgroundColor;
Color foregroundColor;
Color borderColor;
int _value = 0;
int step = 1;
int min = 0;
int max = 100;
bool smooth = false;
Border3DStyle border3D = Border3DStyle.Flat;
bool enableBorder3D = false;
bool showProgressText = false;
Color progressTextHiglightColor = Color.Empty;
Color progressTextColor = Color.Empty;
ProgressBarType barType = ProgressBarType.Standard;
Bitmap foregroundBitmap = null;
Bitmap backgroundBitmap = null;
Color gradientStartColor = Color.Empty;
Color gradientMiddleColor = Color.Empty;
Color gradientEndColor = Color.Empty;
// Default contructor to draw a "Standard" progress Bar
public ProgressBarEx()
{
InitializeProgressControl(ProgressBarType.Standard, ColorUtil.VSNetControlColor,
ColorUtil.VSNetBorderColor, SystemColors.Highlight, null, null, Color.Empty, Color.Empty, Color.Empty);
}
public ProgressBarEx(Bitmap foregroundBitmap, Bitmap backgroundBitmap)
{
InitializeProgressControl(ProgressBarType.Bitmap, ColorUtil.VSNetControlColor,
ColorUtil.VSNetBorderColor, ColorUtil.VSNetBorderColor,
foregroundBitmap, backgroundBitmap, Color.Empty, Color.Empty, Color.Empty);
}
public ProgressBarEx(Bitmap foregroundBitmap)
{
InitializeProgressControl(ProgressBarType.Bitmap, ColorUtil.VSNetControlColor,
ColorUtil.VSNetBorderColor,ColorUtil.VSNetBorderColor,
foregroundBitmap, null, Color.Empty, Color.Empty, Color.Empty);
}
public ProgressBarEx(Color gradientStartColor, Color gradientEndColor)
{
InitializeProgressControl(ProgressBarType.Gradient, ColorUtil.VSNetControlColor,
ColorUtil.VSNetBorderColor, ColorUtil.VSNetBorderColor,
foregroundBitmap, null, gradientStartColor, Color.Empty, gradientEndColor);
}
public ProgressBarEx(Color gradientStartColor, Color gradientMiddleColor, Color gradientEndColor)
{
InitializeProgressControl(ProgressBarType.Gradient, ColorUtil.VSNetControlColor,
ColorUtil.VSNetBorderColor, ColorUtil.VSNetBorderColor,
foregroundBitmap, null, gradientStartColor, gradientMiddleColor, gradientEndColor);
}
void InitializeProgressControl(ProgressBarType barType, Color backgroundColor,
Color foregroundColor, Color borderColor, Bitmap foregroundBitmap, Bitmap backgroundBitmap, Color gradientStartColor,
Color gradientMiddleColor, Color gradientEndColor)
{
// Setup Double buffering
SetStyle(ControlStyles.AllPaintingInWmPaint|ControlStyles.UserPaint|ControlStyles.DoubleBuffer, true);
this.barType = barType;
this.backgroundColor = backgroundColor;
this.foregroundColor = foregroundColor;
this.borderColor = borderColor;
this.foregroundBitmap = foregroundBitmap;
this.backgroundBitmap = backgroundBitmap;
this.gradientStartColor = gradientStartColor;
this.gradientMiddleColor = gradientMiddleColor;
this.gradientEndColor = gradientEndColor;
}
public Color BackgroundColor
{
set
{
if ( backgroundColor != value )
{
backgroundColor = value;
FirePropertyChange(ProgressBarProperty.BackgroundColor);
}
}
get { return backgroundColor; }
}
public Color ForegroundColor
{
set
{
if ( foregroundColor != value )
{
foregroundColor = value;
FirePropertyChange(ProgressBarProperty.ForegroundColor);
}
}
get { return foregroundColor; }
}
public Color BorderColor
{
set
{
if ( borderColor != value )
{
borderColor = value;
FirePropertyChange(ProgressBarProperty.BorderColor);
}
}
get { return borderColor; }
}
public int Value
{
set
{
if ( _value != value )
{
if ( !(value <= max && value >= min) )
{
// Throw exception to indicate out of range condition
string message = "ProgressBarEx Value: " + value.ToString()
+ " is out of range. It needs to be between " +
min.ToString() + " and " + max.ToString();
ArgumentOutOfRangeException outRangeException = new ArgumentOutOfRangeException("Value", message);
throw(outRangeException);
}
_value = value;
FirePropertyChange(ProgressBarProperty.Value);
}
}
get { return _value; }
}
public new Size Size
{
set
{
// Make sure width and height dimensions are always
// an even number so that we can do round math
// when we draw the progress bar segments
Size newSize = value;
if ( newSize.Width % 2 != 0) newSize.Width++;
if ( newSize.Height % 2 != 0) newSize.Height++;
base.Size = newSize;
}
get { return base.Size; }
}
public int Step
{
set
{
if ( step != value )
{
step = value;
FirePropertyChange(ProgressBarProperty.Step);
}
}
get { return step; }
}
public int Minimum
{
set
{
if ( min != value )
{
if ( value >= max )
{
// Throw exception to indicate out of range condition
string message = "ProgressBarEx Minimum Value: "
+ value.ToString() + " is out of range. It needs to be less than " +
"Maximun value: " + max.ToString();
ArgumentOutOfRangeException outRangeException = new ArgumentOutOfRangeException("Value", message);
throw(outRangeException);
}
min = value;
FirePropertyChange(ProgressBarProperty.Minimun);
}
}
get { return min; }
}
public int Maximum
{
set
{
if ( max != value )
{
if ( value <= min )
{
// Throw exception to indicate out of range condition
string message = "ProgressBarEx Maximum Value: " + value.ToString()
+ " is out of range. It needs to be greater than " +
"Minimum value: " + min.ToString();
ArgumentOutOfRangeException outRangeException = new ArgumentOutOfRangeException("Value", message);
throw(outRangeException);
}
max = value;
FirePropertyChange(ProgressBarProperty.Maximun);
}
}
get { return max; }
}
public bool Smooth
{
set
{
if ( smooth != value )
{
smooth = value;
FirePropertyChange(ProgressBarProperty.Smooth);
}
}
get { return smooth; }
}
public Border3DStyle Border3D
{
set
{
if ( border3D != value )
{
border3D = value;
FirePropertyChange(ProgressBarProperty.Border3D);
}
}
get { return border3D; }
}
public bool EnableBorder3D
{
set
{
if ( enableBorder3D != value )
{
enableBorder3D = value;
FirePropertyChange(ProgressBarProperty.Border3D);
}
}
get { return enableBorder3D; }
}
public bool ShowProgressText
{
set
{
if ( showProgressText != value )
{
showProgressText = value;
FirePropertyChange(ProgressBarProperty.ShowProgressText);
}
}
get { return showProgressText; }
}
public Color ProgressTextHiglightColor
{
set
{
if ( progressTextHiglightColor != value )
{
progressTextHiglightColor = value;
FirePropertyChange(ProgressBarProperty.ProgressTextHiglightColor);
}
}
get { return progressTextHiglightColor; }
}
public Color ProgressTextColor
{
set
{
if ( progressTextColor != value )
{
progressTextColor = value;
FirePropertyChange(ProgressBarProperty.ProgressTextColor);
}
}
get { return progressTextColor; }
}
public Bitmap ForegroundBitmap
{
set
{
if ( foregroundBitmap != value )
{
foregroundBitmap = value;
FirePropertyChange(ProgressBarProperty.ForegroundBitmap);
}
}
get { return foregroundBitmap; }
}
public Bitmap BackgroundBitmap
{
set
{
if ( backgroundBitmap != value )
{
backgroundBitmap = value;
FirePropertyChange(ProgressBarProperty.BackgroundBitmap);
}
}
get { return backgroundBitmap; }
}
public Color GradientStartColor
{
set
{
if ( gradientStartColor != value )
{
gradientStartColor = value;
FirePropertyChange(ProgressBarProperty.GradientStartColor);
}
}
get { return gradientStartColor; }
}
public Color GradientMiddleColor
{
set
{
if ( gradientMiddleColor != value )
{
gradientMiddleColor = value;
FirePropertyChange(ProgressBarProperty.GradientMiddleColor);
}
}
get { return gradientMiddleColor; }
}
public Color GradientEndColor
{
set
{
if ( gradientEndColor != value )
{
gradientEndColor = value;
FirePropertyChange(ProgressBarProperty.GradientEndColor);
}
}
get { return gradientEndColor; }
}
public void PerformStep()
{
if ( _value < max )
_value += step;
if ( _value > max )
_value = max;
FirePropertyChange(ProgressBarProperty.Step);
}
void FirePropertyChange(ProgressBarProperty property)
{
// Fire event if we need to
if (PropertyChanged != null)
PropertyChanged(this, property);
// Force a repaint of the control
Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -