/// <summary> /// 百分比进度控件 /// </summary> [ToolboxItem(true)] [DefaultProperty("Value")] [Description("百分比进度控件")] public partial class ProgressExt : Control { #region private int gridsOpacity = 255; /// <summary> /// 网格透明度 /// </summary> [DefaultValue(255)] [Description("网格透明度")] public int GridsOpacity { get { return this.gridsOpacity; } set { if (this.gridsOpacity == value || value < 0 || value > 255) return; this.gridsOpacity = value; this.Invalidate(); } } private Orientation orientation = Orientation.Horizontal; /// <summary> /// 控件方向 /// </summary> [DefaultValue(Orientation.Horizontal)] [Description("控件方向")] public Orientation Orientation { get { return this.orientation; } set { if (this.orientation == value) return; this.orientation = value; this.Invalidate(); } } private int gridsDistance = 6; /// <summary> /// 网格距离 /// </summary> [DefaultValue(6)] [Description("网格距离")] public int GridsDistance { get { return this.gridsDistance; } set { if (this.gridsDistance == value || value < 1) return; this.gridsDistance = value; this.Invalidate(); } } private Color gridsColor = Color.YellowGreen; /// <summary> /// 网格颜色 /// </summary> [DefaultValue(typeof(Color), "YellowGreen")] [Description("网格颜色")] public Color GridsColor { get { return this.gridsColor; } set { if (this.gridsColor == value) return; this.gridsColor = value; this.Invalidate(); } } private bool valueShow = false; /// <summary> /// 是否显示进度值 /// </summary> [DefaultValue(false)] [Description("是否显示进度值")] public bool ValueShow { get { return this.valueShow; } set { if (this.valueShow == value) return; this.valueShow = value; this.Invalidate(); } } private float value = 0.0f; /// <summary> /// 进度值(0-1) /// </summary> [DefaultValue(0.0f)] [Description("进度值")] public float Value { get { return this.value; } set { if (this.value == value) return; if (value > 1) value = 1; if (value < 0) value = 0; this.value = value; this.Invalidate(); } } private ProgressColorType progressColorType = ProgressColorType.Level; /// <summary> /// 进度颜色类型 /// </summary> [DefaultValue(ProgressColorType.Level)] [Description("进度颜色类型")] public ProgressColorType ProgressColorType { get { return this.progressColorType; } set { if (this.progressColorType == value) return; this.progressColorType = value; this.Invalidate(); } } private ProgressColorGroupCollection progressColorGroupCollection; /// <summary> /// 颜色级别配置集合 /// </summary> [Description("颜色级别配置集合")] [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] public ProgressColorGroupCollection ProgressColorGroupItems { get { if (this.progressColorGroupCollection == null) this.progressColorGroupCollection = new ProgressColorGroupCollection(this); return this.progressColorGroupCollection; } } private Color gradientStartColor = Color.Yellow; /// <summary> /// 渐变开始颜色 /// </summary> [DefaultValue(typeof(Color), "Yellow")] [Description("渐变开始颜色")] public Color GradientStartColor { get { return this.gradientStartColor; } set { if (this.gradientStartColor == value) return; this.gradientStartColor = value; this.Invalidate(); } } private Color gradientEndColor = Color.YellowGreen; /// <summary> /// 渐变结束颜色 /// </summary> [DefaultValue(typeof(Color), "YellowGreen")] [Description("渐变结束颜色")] public Color GradientEndColor { get { return this.gradientEndColor; } set { if (this.gradientEndColor == value) return; this.gradientEndColor = value; this.Invalidate(); } } protected override Size DefaultSize { get { return new Size(150, 35); ; } } [Editor(typeof(ColorEditorExt), typeof(System.Drawing.Design.UITypeEditor))] public override Color BackColor { get { return base.BackColor; } set { base.BackColor = value; } } #endregion public ProgressExt() { SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.OptimizedDoubleBuffer, true); SetStyle(ControlStyles.ResizeRedraw, true); SetStyle(ControlStyles.SupportsTransparentBackColor, true); InitializeComponent(); } protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) { base.OnPaint(e); if (e.ClipRectangle.Width < 2 || e.ClipRectangle.Height < 2) return; Graphics g = e.Graphics; g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; SizeF text_sizef = g.MeasureString("100%", this.Font); RectangleF rectf = new RectangleF(0, 0, e.ClipRectangle.Width - 1, e.ClipRectangle.Height - 1); PointF text_point = new PointF(0, 0); if (this.ValueShow) { if (this.Orientation == Orientation.Horizontal) { rectf = new RectangleF(0, 0, e.ClipRectangle.Width - text_sizef.Width - 1, e.ClipRectangle.Height - 1); text_point = new PointF(rectf.Width, (rectf.Height - text_sizef.Height) / 2); } else { rectf = new RectangleF(0, text_sizef.Height, e.ClipRectangle.Width - 1, e.ClipRectangle.Height - text_sizef.Height - 1); text_point = new PointF((rectf.Width - text_sizef.Width) / 2, 0); } } Pen grids_pen = new Pen(Color.FromArgb(this.GridsOpacity, this.gridsColor)); #region 生成画笔 Brush grids_sb = null; if (this.ProgressColorType == ProgressColorType.Level) { Color color = Color.FromArgb(255 - this.BackColor.R, 255 - this.BackColor.G, 255 - this.BackColor.B); for (int i = this.ProgressColorGroupItems.Count - 1; i > -1; i--) { if (this.Value >= this.ProgressColorGroupItems[i].Interval) { color = this.ProgressColorGroupItems[i].Color; break; } } grids_sb = new SolidBrush(Color.FromArgb(this.GridsOpacity, color)); } else { grids_sb = new LinearGradientBrush(rectf, Color.FromArgb(this.GridsOpacity, this.GradientStartColor), Color.FromArgb(this.GridsOpacity, this.GradientEndColor), this.Orientation == Orientation.Horizontal ? 0.0f : -90.0f, true); } #endregion if (this.Orientation == Orientation.Horizontal) { float realityValue = rectf.Width * this.Value; g.FillRectangle(grids_sb, 0.0f, 0.0f, realityValue, rectf.Height); g.DrawLine(grids_pen, 0, rectf.Height / 2, rectf.Width, rectf.Height / 2); int count = (int)Math.Ceiling(rectf.Width / (float)this.GridsDistance); for (int i = 0; i < count; i++) { float x = this.GridsDistance * i; g.DrawLine(grids_pen, x, 0, x, rectf.Height); } } else { float realityValue = rectf.Height * this.value; g.FillRectangle(grids_sb, 0.0f, rectf.Bottom - realityValue, rectf.Width, realityValue);//进度值背景 g.DrawLine(grids_pen, rectf.Width / 2, rectf.Y, rectf.Width / 2, rectf.Bottom);//中线 int count = (int)Math.Ceiling(rectf.Height / (float)this.GridsDistance); for (int i = count - 1; i > -1; i--) { int y = (int)(rectf.Bottom - this.GridsDistance * i); g.DrawLine(grids_pen, 0, y, rectf.Width, y); } } g.DrawRectangle(grids_pen, rectf.X, rectf.Y, rectf.Width, rectf.Height);//外边框 #region 进度值 if (this.ValueShow) { SolidBrush text_sb = new SolidBrush(Color.FromArgb(this.GridsOpacity, this.ForeColor)); string str = ((int)(this.Value * 100)).ToString().PadLeft(3, ‘ ‘) + "%"; g.DrawString(str, this.Font, text_sb, text_point.X, text_point.Y); } #endregion grids_sb.Dispose(); grids_pen.Dispose(); } /// <summary> /// 颜色级别配置集合 /// </summary> [Description("颜色级别配置集合")] [Editor(typeof(CollectionEditorExt), typeof(UITypeEditor))] public sealed class ProgressColorGroupCollection : IList, ICollection, IEnumerable { private ArrayList groupParamList = new ArrayList(); private ProgressExt owner; public ProgressColorGroupCollection(ProgressExt owner) { this.owner = owner; } #region IEnumerable public IEnumerator GetEnumerator() { ProgressColorGroup[] listArray = new ProgressColorGroup[this.groupParamList.Count]; for (int index = 0; index < listArray.Length; ++index) listArray[index] = (ProgressColorGroup)this.groupParamList[index]; return listArray.GetEnumerator(); } #endregion #region ICollection public void CopyTo(Array array, int index) { for (int i = 0; i < this.Count; i++) array.SetValue(this.groupParamList[i], i + index); } public int Count { get { return this.groupParamList.Count; } } public bool IsSynchronized { get { return false; } } public object SyncRoot { get { return (object)this; } } #endregion #region IList public int Add(object value) { ProgressColorGroup groupParam = (ProgressColorGroup)value; this.groupParamList.Add(groupParam); this.owner.Invalidate(); return this.Count - 1; } public void Clear() { this.groupParamList.Clear(); this.owner.Invalidate(); } public bool Contains(object value) { return this.IndexOf(value) != -1; } public int IndexOf(object value) { return this.groupParamList.IndexOf(value); } public void Insert(int index, object value) { throw new NotImplementedException(); } public bool IsFixedSize { get { return false; } } public bool IsReadOnly { get { return false; } } public void Remove(object value) { if (!(value is ProgressColorGroup)) return; this.groupParamList.Remove((ProgressColorGroup)value); this.owner.Invalidate(); } public void RemoveAt(int index) { this.groupParamList.RemoveAt(index); this.owner.Invalidate(); } public ProgressColorGroup this[int index] { get { return (ProgressColorGroup)this.groupParamList[index]; } set { this.groupParamList[index] = (ProgressColorGroup)value; this.owner.Invalidate(); } } object IList.this[int index] { get { return (object)this.groupParamList[index]; } set { this.groupParamList[index] = (ProgressColorGroup)value; this.owner.Invalidate(); } } #endregion } } /// <summary> /// 颜色级别配置 /// </summary> [Description("颜色级别配置")] public class ProgressColorGroup { /// <summary> /// 从该值开始的背景颜色(0-1) /// </summary> [Description("从该值开始的背景颜色(0-1)")] public float Interval { get; set; } /// <summary> /// 背景颜色 /// </summary> [Description("背景颜色")] public Color Color { get; set; } } /// <summary> /// 进度颜色类型 /// </summary> [Description("进度颜色类型")] public enum ProgressColorType { /// <summary> /// 渐变 /// </summary> Gradient, /// <summary> /// 级别 /// </summary> Level }
源码下载:百分比进度条控件.zip