C# 双色ProgressBar
效果如图:
实现代码:
红色为FixValue 属性控制,绿色为Value控制。
代码:
public class NewProgressBar : ProgressBar { private int fixValue ; public int FixValue { get { return fixValue; } set { if (fixValue < Minimum) { //MessageBox.Show("属性值设置必须大于等于最小值!"); fixValue = Minimum; FixValue = Minimum; return; } fixValue = value; } } public NewProgressBar() { //this.SetStyle(ControlStyles.UserPaint, true); this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true); this.UpdateStyles(); } protected override void OnPaint(PaintEventArgs e) { if (Value >= fixValue) { Rectangle rec = e.ClipRectangle; rec.Width = (int)(rec.Width * ((double)(Value-Minimum) / (Maximum - Minimum))) - 4; if (ProgressBarRenderer.IsSupported) ProgressBarRenderer.DrawHorizontalBar(e.Graphics, e.ClipRectangle); rec.Height = rec.Height - 4; e.Graphics.FillRectangle(Brushes.Lime, 2, 2, rec.Width, rec.Height); } else { Rectangle rec = e.ClipRectangle; rec.Width = (int)(rec.Width * ((double)(fixValue-Minimum) / (Maximum - Minimum))) - 2; if (ProgressBarRenderer.IsSupported) ProgressBarRenderer.DrawHorizontalBar(e.Graphics, e.ClipRectangle); rec.Height = rec.Height - 4; Rectangle recv = e.ClipRectangle; recv.Width = (int)(rec.Width * ((double)(Value - Minimum) / (fixValue - Minimum))); if (ProgressBarRenderer.IsSupported) ProgressBarRenderer.DrawHorizontalBar(e.Graphics, e.ClipRectangle); recv.Height = recv.Height - 4; e.Graphics.FillRectangle(Brushes.Lime, 2, 2, recv.Width, recv.Height); e.Graphics.FillRectangle(Brushes.HotPink, recv.Width, 2, rec.Width - recv.Width, rec.Height); } } }