【WinForm】简单的实现进度条的变色功能

【WinForm】简单的实现进度条的变色功能
  1 using System.Drawing;
  2 using System.Linq;
  3 using System.Windows.Forms;
  4 using System.ComponentModel;
  5 using System.Runtime.InteropServices;
  6 using ScanningGangClient.Components.AimProgressBar;
  7 
  8 namespace ScanningGangClient.Components.Control
  9 {
 10     public delegate string ProgressBarTextFormat();
 11     public class AimProgressBar : ProgressBar
 12     {
 13        
 14         [Localizable(true)]
 15         public bool ValueShowText { get; set; } = false;
 16 
 17         [Localizable(true)]
 18         public bool ValueFollow { get; set; } = false;
 19 
 20         [Localizable(true)]
 21         public Color ProgressColor { get; set; } = Color.Green;
 22 
 23         [Localizable(true)]
 24         public Color FontColor { get; set; } = Color.Black;
 25 
 26         [AmbientValue(null)]
 27         [DispId(-512)]
 28         [Localizable(true)]
 29         public Font FontStyle { get; set; } = DefaultFont;
 30 
 31         [Localizable(true)]
 32         public Point FontPoint { get; set; } = new Point(0,0);
 33 
 34         [Localizable(true)]
 35         public ProgressDirection ProgressDirection { get; set; } = ProgressDirection.Incremental;
 36 
 37         [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
 38         [Localizable(true)]
 39         [MergableProperty(false)]
 40         public StageRogList ProgressStageRog { get; set; } = new StageRogList();
 41 
 42         public AimProgressBar()
 43         {
 44             this.SetStyle(ControlStyles.UserPaint, true);
 45         }
 46 
 47         private ProgressBarTextFormat progressBarText;
 48         public void SetTextFormat(ProgressBarTextFormat progressBarText)
 49         {
 50             this.progressBarText = progressBarText;
 51         }
 52 
 53         protected override void OnPaint(PaintEventArgs e)
 54         {
 55             Graphics graphics = e.Graphics;
 56             Rectangle rec = e.ClipRectangle;
 57             rec.Width = (int)(rec.Width * ((double)Value / Maximum)) - 4;
 58             if (ProgressBarRenderer.IsSupported)
 59                 ProgressBarRenderer.DrawHorizontalBar(graphics, e.ClipRectangle);
 60             rec.Height = rec.Height - 4;
 61             Brush brush = new SolidBrush(ProgressColor);
 62             if (ProgressStageRog != null && ProgressStageRog.Count > 0)
 63             {
 64                 try
 65                 {
 66                     StageRog stageRog;
 67                     if (ProgressDirection.Incremental == this.ProgressDirection)
 68                     {
 69                         stageRog = ProgressStageRog.Where(s => s.NumberValue <= (Value + Minimum)).Last();
 70                     }
 71                     else
 72                     {
 73                         stageRog = ProgressStageRog.Where(s => s.NumberValue >= (Value + Minimum)).First();
 74                     }
 75 
 76                     if (stageRog != null)
 77                     {
 78                         brush = new SolidBrush(stageRog.ProgressColor);
 79                     }
 80                 }
 81                 catch { }
 82                 
 83             }
 84 
 85             graphics.FillRectangle(brush, 2, 2, rec.Width, rec.Height);
 86 
 87 
 88             if (ValueShowText)
 89             {
 90                 int X = 0;
 91                 int Y = 0;
 92                 string text = progressBarText?.Invoke();
 93                 if (text == null || string.Empty.Equals(text))
 94                 {
 95                     text = Value.ToString();
 96                 }
 97                 if (ValueFollow)
 98                 {
 99                     X = rec.Width - (text.Length * 6);
100                     if (X <= 0) { X = 2; }
101                     Y = (int)rec.Height / 2 / 2;
102                 }
103                 else
104                 {
105                     X = FontPoint.X;
106                     Y = FontPoint.Y;
107                 }
108                 graphics.DrawString(text, FontStyle, new SolidBrush(FontColor),X,Y );
109             }
110 
111         }
112     }
113 }
AimProgressBar 【WinForm】简单的实现进度条的变色功能
 1 using System.Drawing;
 2 
 3 namespace ScanningGangClient.Components.AimProgressBar
 4 {
 5     public class StageRog
 6     {
 7         public StageRog() { }
 8         public StageRog(int NumberValue, Color ProgressColor) : this()
 9         {
10             this.NumberValue = NumberValue;
11             this.ProgressColor = ProgressColor;
12         }
13 
14         public int NumberValue { get; set; }
15         public Color ProgressColor { get; set; }
16     }
17 }
StageRog 【WinForm】简单的实现进度条的变色功能
 1 using System.Collections;
 2 using System.Collections.ObjectModel;
 3 using System.Reflection;
 4 
 5 namespace ScanningGangClient.Components.AimProgressBar
 6 {
 7     [DefaultMember("Item")]
 8     public class StageRogList : Collection<StageRog>, IList, ICollection, IEnumerable
 9     {
10         public void AddRange(StageRogList value)
11         {
12             for (int i = 0; i < value.Count; i++)
13             {
14                 this.Add(value[i]);
15             }
16         }
17     }
18 }
StageRogList 【WinForm】简单的实现进度条的变色功能
 1 namespace ScanningGangClient.Components.AimProgressBar
 2 {
 3     public enum ProgressDirection
 4     {
 5         /// <summary>
 6         /// 增长的,显示小数
 7         /// </summary>
 8         Incremental,
 9         /// <summary>
10         /// 减少的,显示大数
11         /// </summary>
12         Diminishing
13     }
14 }
View Code

 AimProgressBar 为组件,可以拖拽的那种。

增加了ValueShowText(显示进度文本)、ValueFollow(进度文本跟随进度条)、ProgressColor(进度条颜色)、ProgressDirection(Value到达参数颜色)、FontColor(字体哑颜色)、FontStyle(字体)、FontPoint(字体固定坐标)、ProgressDirection(Value到达参数颜色验证方式)属性,可以直接通过VS直接勾勾选选。

同时增加了 SetTextFormat(显示文本自定义)方法

纵向没弄,有兴趣的可以自行更改

上一篇:Winform中生成自动控件


下一篇:C# WinForm设置控件居中