百分比进度控件

此篇博客详细介绍了PercentageProgressExt类,它扩展了Control基类,专注于GDI绘图实现的不同百分比显示类型,如环形、扇形、圆弧和方形条形。它还涵盖了属性设置、事件处理和自定义绘图方法。
摘要由CSDN通过智能技术生成

该控件是继承于 Control 基类开发的。这个控件主要就是GDI绘图方面。

百分比显示类型

 

   protected override void OnPaint(PaintEventArgs e)
  2         {
  3             base.OnPaint(e);
  4 
  5             Graphics g = e.Graphics;
  6             Rectangle rect = new Rectangle((int)this.ClientRectangle.X + ((int)this.ClientRectangle.Width / 2 - this.ArcRadius) + 1, (int)this.ClientRectangle.Y + ((int)this.ClientRectangle.Height / 2 - this.ArcRadius) + 1, this.ArcRadius * 2 - 2, this.ArcRadius * 2 - 2);
  7 
  8             #region 环形
  9             if (this.Type == PercentageType.Annulus)
 10             {
 11                 g.SmoothingMode = SmoothingMode.AntiAlias;
 12                 #region 背景
 13                 Pen arcback_pen = new Pen(this.ArcBackColor, this.ArcThickness);
 14                 g.DrawArc(arcback_pen, ControlCommom.TransformRectangleByPen(rect, this.ArcThickness), 270, 360);
 15                 arcback_pen.Dispose();
 16                 #endregion
 17 
 18                 if (this.ArcAnnulusBackColor != Color.Empty)
 19                 {
 20                     SolidBrush arcannulusback_sb = new SolidBrush(this.ArcAnnulusBackColor);
 21                     g.FillPie(arcannulusback_sb, rect.X + this.ArcThickness / 2, rect.Y + this.ArcThickness / 2, rect.Width - this.ArcThickness, rect.Height - this.ArcThickness, 270, 360);
 22                     arcannulusback_sb.Dispose();
 23                 }
 24 
 25                 #region 百分比值
 26                 Pen arc_pen = new Pen(this.ArcColor, this.ArcThickness);
 27                 if (this.ArcRound)
 28                 {
 29                     arc_pen.StartCap = LineCap.Round;
 30                     arc_pen.EndCap = LineCap.Round;
 31                 }
 32                 g.DrawArc(arc_pen, ControlCommom.TransformRectangleByPen(rect, this.ArcThickness), 270, 360 * this.Value);
 33                 arc_pen.Dispose();
 34                 #endregion
 35 
 36                 #region 文本
 37                 this.DrawValueText(g, rect);
 38                 #endregion
 39 
 40             }
 41             #endregion
 42             #region 扇形
 43             else if (this.Type == PercentageType.Sector)
 44             {
 45                 g.SmoothingMode = SmoothingMode.AntiAlias;
 46                 #region 背景
 47                 SolidBrush arcback_sb = new SolidBrush(this.ArcBackColor);
 48                 g.FillPie(arcback_sb, rect.X, rect.Y, rect.Width, rect.Height, 270, 360);
 49                 arcback_sb.Dispose();
 50                 #endregion
 51 
 52                 if (this.ArcAnnulusBackColor != Color.Empty)
 53                 {
 54                     SolidBrush arcannulusback_sb = new SolidBrush(this.ArcAnnulusBackColor);
 55                     g.FillPie(arcannulusback_sb, rect.X + this.ArcThickness / 2, rect.Y + this.ArcThickness / 2, rect.Width - this.ArcThickness, rect.Height - this.ArcThickness, 270, 360);
 56                     arcannulusback_sb.Dispose();
 57                 }
 58 
 59                 #region 百分比值
 60                 SolidBrush arc_sb = new SolidBrush(this.ArcColor);
 61                 g.FillPie(arc_sb, rect.X, rect.Y, rect.Width, rect.Height, 270, 360 * this.Value);
 62                 arc_sb.Dispose();
 63                 #endregion
 64 
 65                 #region 文本
 66                 this.DrawValueText(g, rect);
 67                 #endregion
 68 
 69             }
 70             #endregion
 71             #region 圆弧
 72             else if (this.Type == PercentageType.Arc)
 73             {
 74                 g.SmoothingMode = SmoothingMode.AntiAlias;
 75                 int start = this.ArcAngle == 360 ? 270 : 180 + (180 - this.ArcAngle) / 2;
 76 
 77                 #region 背景
 78                 Pen arcback_pen = new Pen(this.ArcBackColor, this.ArcThickness);
 79                 g.DrawArc(arcback_pen, ControlCommom.TransformRectangleByPen(rect, this.ArcThickness), start, this.ArcAngle);
 80                 arcback_pen.Dispose();
 81                 #endregion
 82 
 83                 #region 百分比值
 84                 Pen arc_pen = new Pen(this.ArcColor, this.ArcThickness);
 85                 if (this.ArcRound)
 86                 {
 87                     arc_pen.EndCap = LineCap.Round;
 88                 }
 89                 g.DrawArc(arc_pen, ControlCommom.TransformRectangleByPen(rect, this.ArcThickness), start, this.ArcAngle * this.Value);
 90                 arc_pen.Dispose();
 91                 #endregion
 92 
 93                 #region 文本
 94                 this.DrawValueText(g, rect);
 95                 #endregion
 96 
 97             }
 98             #endregion
 99             #region 方形
100             else if (this.Type == PercentageType.Quadrangle)
101             {
102                 RectangleF rectf = new RectangleF();
103                 rectf.Width = 10 + this.SquareWidth * 10;
104                 rectf.Height = 10 + this.SquareWidth * 10;
105                 rectf.X = this.ClientRectangle.X + (this.ClientRectangle.Width - rectf.Width) / 2;
106                 rectf.Y = this.ClientRectangle.Bottom - rectf.Height;
107 
108                 #region 背景
109                 SolidBrush arcback_sb = new SolidBrush(this.ArcBackColor);
110                 g.FillRectangle(arcback_sb, rectf);
111                 arcback_sb.Dispose();
112                 #endregion
113 
114                 #region 百分比值
115                 SolidBrush arc_sb = new SolidBrush(this.ArcColor);
116                 string str = this.Value.ToString("F4");
117                 int index = str.IndexOf('.');
118                 int row = int.Parse(str.Substring(index + 1, 1));
119                 RectangleF row_rectf = new RectangleF();
120                 row_rectf.Width = rectf.Width;
121                 row_rectf.Height = row * 1 + row * this.SquareWidth;
122                 row_rectf.X = rectf.X;
123                 row_rectf.Y = rectf.Bottom - row_rectf.Height;
124                 g.FillRectangle(arc_sb, row_rectf);
125 
126                 int col = int.Parse(str.Substring(index + 2, 1));
127                 if (col > 0)
128                 {
129                     RectangleF col_rectf = new RectangleF();
130                     col_rectf.Width = col * 1 + col * this.SquareWidth;
131                     col_rectf.Height = this.SquareWidth + 1;
132                     col_rectf.X = rectf.X;
133                     col_rectf.Y = row_rectf.Y - col_rectf.Height;
134                     g.FillRectangle(arc_sb, col_rectf);
135                 }
136 
137                 arc_sb.Dispose();
138                 #endregion
139 
140                 #region 边框
141                 Pen arcbackborder_pen = new Pen(this.SquareBorderColor, 1);
142                 for (int i = 0; i < 11; i++)//行
143                 {
144                     g.DrawLine(arcbackborder_pen, rectf.X, rectf.Y + i * 1 + i * this.SquareWidth, rectf.Right, rectf.Y + i * 1 + i * this.SquareWidth);
145                 }
146                 for (int i = 0; i < 11; i++)//列
147                 {
148                     g.DrawLine(arcbackborder_pen, rectf.X + i * 1 + i * this.SquareWidth, rectf.Y, rectf.X + i * 1 + i * this.SquareWidth, rectf.Bottom);
149                 }
150                 arcbackborder_pen.Dispose();
151                 #endregion
152 
153                 #region 百分比值文本
154                 string value_str = (this.Value * 100).ToString("F0") + "%";
155                 StringFormat value_sf = new StringFormat();
156                 value_sf.FormatFlags = StringFormatFlags.NoWrap;
157                 value_sf.Alignment = StringAlignment.Center;
158                 value_sf.Trimming = StringTrimming.None;
159                 Size value_size = g.MeasureString(value_str, this.ValueFont, new Size(), value_sf).ToSize();
160                 SolidBrush value_sb = new SolidBrush(this.ValueColor);
161                 g.DrawString(value_str, this.ValueFont, value_sb, new RectangleF(rectf.X + (rectf.Width - value_size.Width) / 2, rectf.Y - value_size.Height, value_size.Width, value_size.Height), value_sf);
162                 value_sb.Dispose();
163                 value_sf.Dispose();
164                 #endregion
165 
166             }
167             #endregion
168             #region 条形
169             else if (this.Type == PercentageType.Bar)
170             {
171                 if (this.ArcRound)
172                 {
173                     g.SmoothingMode = SmoothingMode.AntiAlias;
174                 }
175                 RectangleF rectf = new RectangleF();
176                 rectf.Width = this.ClientRectangle.Width - (this.ArcRound ? this.ArcThickness : 0);
177                 rectf.Height = this.ArcThickness;
178                 rectf.X = this.ClientRectangle.X + (this.ArcRound ? this.ArcThickness / 2 : 0);
179                 rectf.Y = this.ClientRectangle.Bottom - rectf.Height;
180 
181                 #region 背景
182                 Pen arcback_pen = new Pen(this.ArcBackColor, this.ArcThickness);
183                 if (this.ArcRound)
184                 {
185                     arcback_pen.StartCap = LineCap.Round;
186                     arcback_pen.EndCap = LineCap.Round;
187                 }
188                 g.DrawLine(arcback_pen, rectf.X, rectf.Bottom - rectf.Height / 2, rectf.Right, rectf.Bottom - rectf.Height / 2);
189                 arcback_pen.Dispose();
190                 #endregion
191 
192                 #region 百分比值
193                 Pen arc_pen = new Pen(this.ArcColor, this.ArcThickness);
194                 if (this.ArcRound)
195                 {
196                     arc_pen.StartCap = LineCap.Round;
197                     arc_pen.EndCap = LineCap.Round;
198                 }
199                 g.DrawLine(arc_pen, rectf.X, rectf.Bottom - rectf.Height / 2, (int)(rectf.Right * this.Value), rectf.Bottom - rectf.Height / 2);
200                 arc_pen.Dispose();
201                 #endregion
202 
203                 #region 百分比值文本
204                 string value_str = (this.Value * 100).ToString("F0") + "%";
205                 StringFormat value_sf = new StringFormat();
206                 value_sf.FormatFlags = StringFormatFlags.NoWrap;
207                 value_sf.Alignment = StringAlignment.Center;
208                 value_sf.Trimming = StringTrimming.None;
209                 Size value_size = g.MeasureString(value_str, this.ValueFont, new Size(), value_sf).ToSize();
210                 SolidBrush value_sb = new SolidBrush(this.ValueColor);
211                 g.DrawString(value_str, this.ValueFont, value_sb, new RectangleF(rectf.X + (rectf.Width - value_size.Width) / 2, rectf.Y - value_size.Height, value_size.Width, value_size.Height), value_sf);
212                 value_sb.Dispose();
213                 value_sf.Dispose();
214                 #endregion
215 
216             }
217             #endregion
218 
219         }

新增的类如下

新增属性如下

 /// <summary>
    /// 百分比进度控件
    /// </summary>
    [ToolboxItem(true)]
    [Description("百分比进度控件")]
    [DefaultProperty("Value")]
    [DefaultEvent("ValueChanged")]
    [Designer(typeof(DottedLineBorderExtDesigner))]
    public class PercentageProgressExt : Control
    {
        #region 新增事件

        public delegate void ValueChangedEventHandler(object sender, ValueChangedEventArgs e);

        private event ValueChangedEventHandler valueChanged;
        /// <summary>
        /// 百分比值更改事件
        /// </summary>
        [Description("百分比值更改事件")]
        public event ValueChangedEventHandler ValueChanged
        {
            add { this.valueChanged += value; }
            remove { this.valueChanged -= value; }
        }

        #endregion

        #region 停用事件

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler PaddingChanged
        {
            add { base.PaddingChanged += value; }
            remove { base.PaddingChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler TabIndexChanged
        {
            add { base.TabIndexChanged += value; }
            remove { base.TabIndexChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler TabStopChanged
        {
            add { base.TabStopChanged += value; }
            remove { base.TabStopChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler TextChanged
        {
            add { base.TextChanged += value; }
            remove { base.TextChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler FontChanged
        {
            add { base.FontChanged += value; }
            remove { base.FontChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler ForeColorChanged
        {
            add { base.ForeColorChanged += value; }
            remove { base.ForeColorChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler RightToLeftChanged
        {
            add { base.RightToLeftChanged += value; }
            remove { base.RightToLeftChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler ImeModeChanged
        {
            add { base.ImeModeChanged += value; }
            remove { base.ImeModeChanged -= value; }
        }

        #endregion

        #region 新增属性

        private PercentageType type = PercentageType.Annulus;
        /// <summary>
        /// 百分比显示类型
        /// </summary>
        [DefaultValue(PercentageType.Annulus)]
        [Description("百分比显示类型")]
        public PercentageType Type
        {
            get { return this.type; }
            set
            {
                if (this.type == value)
                    return;
                this.type = value;
                this.Invalidate();
            }
        }

        private int arcRadius = 50;
        /// <summary>
        /// 圆形半径
        /// </summary>
        [DefaultValue(50)]
        [Description("圆形半径")]
        public int ArcRadius
        {
            get { return this.arcRadius; }
            set
            {
                if (this.arcRadius == value || value < 0)
                    return;
                this.arcRadius = value;
                this.Invalidate();
            }
        }

        private int arcAngle = 360;
        /// <summary>
        /// 圆弧弧度大小
        /// </summary>
        [DefaultValue(360)]
        [Description("圆弧弧度大小")]
        public int ArcAngle
        {
            get { return this.arcAngle; }
            set
            {
                if (this.arcAngle == value || value < 0 || value > 360)
                    return;
                this.arcAngle = value;
                this.Invalidate();
            }
        }

        private int arcThickness = 10;
        /// <summary>
        /// 弧线大小
        /// </summary>
        [DefaultValue(10)]
        [Description("弧线大小")]
        public int ArcThickness
        {
            get { return this.arcThickness; }
            set
            {
                if (this.arcThickness == value || value < 0 || value > this.ArcRadius)
                    return;
                this.arcThickness = value;
                this.Invalidate();
            }
        }

        private bool arcRound = false;
        /// <summary>
        /// 弧线是否为圆角
        /// </summary>
        [DefaultValue(false)]
        [Description("弧线是否为圆角")]
        public bool ArcRound
        {
            get { return this.arcRound; }
            set
            {
                if (this.arcRound == value)
                    return;
                this.arcRound = value;
                this.Invalidate();
            }
        }

        private Color arcAnnulusBackColor = Color.Empty;
        /// <summary>
        /// 环形背景颜色(仅限于Annulus、Sector)
        /// </summary>
        [DefaultValue(typeof(Color), "Empty")]
        [Description("环形背景颜色(仅限于Annulus、Sector)")]
        [Editor(typeof(ColorEditorExt), typeof(UITypeEditor))]
        public Color ArcAnnulusBackColor
        {
            get { return this.arcAnnulusBackColor; }
            set
            {
                if (this.arcAnnulusBackColor == value)
                    return;
                this.arcAnnulusBackColor = value;
                this.Invalidate();
            }
        }

        private Color arcBackColor = Color.FromArgb(112, 220, 220, 220);
        /// <summary>
        /// 弧线背景颜色
        /// </summary>
        [DefaultValue(typeof(Color), "112, 220, 220, 220")]
        [Description("弧线背景颜色")]
        [Editor(typeof(ColorEditorExt), typeof(UITypeEditor))]
        public Color ArcBackColor
        {
            get { return this.arcBackColor; }
            set
            {
                if (this.arcBackColor == value)
                    return;
                this.arcBackColor = value;
                this.Invalidate();
            }
        }

        private Color arcColor = Color.FromArgb(227, 240, 128, 128);
        /// <summary>
        /// 弧线颜色
        /// </summary>
        [DefaultValue(typeof(Color), "227, 240, 128, 128")]
        [Description("弧线颜色")]
        [Editor(typeof(ColorEditorExt), typeof(UITypeEditor))]
        public Color ArcColor
        {
            get { return this.arcColor; }
            set
            {
                if (this.arcColor == value)
                    return;
                this.arcColor = value;
                this.Invalidate();
            }
        }

        #region 方形

        private int squareWidth = 10;
        /// <summary>
        /// 方形小格大小
        /// </summary>
        [DefaultValue(10)]
        [Description("方形小格大小")]
        public int SquareWidth
        {
            get { return this.squareWidth; }
            set
            {
                if (this.squareWidth == value || value < 0)
                    return;
                this.squareWidth = value;
                this.Invalidate();
            }
        }

        private Color squareBorderColor = Color.FromArgb(149, 255, 255, 255);
        /// <summary>
        /// 方形小格边框颜色
        /// </summary>
        [DefaultValue(typeof(Color), "149, 255, 255, 255")]
        [Description("方形小格边框颜色")]
        [Editor(typeof(ColorEditorExt), typeof(UITypeEditor))]
        public Color SquareBorderColor
        {
            get { return this.squareBorderColor; }
            set
            {
                if (this.squareBorderColor == value)
                    return;
                this.squareBorderColor = value;
                this.Invalidate();
            }
        }

        #endregion

        #region 值文本

        private float value = 0f;
        /// <summary>
        /// 百分比值(0-1)
        /// </summary>
        [DefaultValue(0f)]
        [Description("百分比值(0-1)")]
        public float Value
        {
            get { return this.value; }
            set
            {
                if (this.value == value || value < 0 || value > 1)
                    return;
                this.value = value;
                this.Invalidate();

                this.OnValueChanged(new ValueChangedEventArgs() { Value = this.value });
            }
        }

        private Font valueFont = new Font("宋体", 15);
        /// <summary>
        /// 百分比值字体
        /// </summary>
        [DefaultValue(typeof(Font), "宋体, 15pt")]
        [Description("百分比值字体")]
        public Font ValueFont
        {
            get { return this.valueFont; }
            set
            {
                if (this.valueFont == value)
                    return;
                this.valueFont = value;
                this.Invalidate();
            }
        }

        private Color valueColor = Color.FromArgb(227, 255, 255, 255);
        /// <summary>
        /// 百分比值颜色
        /// </summary>
        [DefaultValue(typeof(Color), "227, 255, 255, 255")]
        [Description("百分比值颜色")]
        [Editor(typeof(ColorEditorExt), typeof(UITypeEditor))]
        public Color ValueColor
        {
            get { return this.valueColor; }
            set
            {
                if (this.valueColor == value)
                    return;
                this.valueColor = value;
                this.Invalidate();
            }
        }

        #endregion

        #endregion

        #region 重写属性

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        protected new bool DesignMode
        {
            get
            {
                if (this.GetService(typeof(IDesignerHost)) != null || System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Designtime)
                {
                    return true;   //界面设计模式
                }
                else
                {
                    return false;//运行时模式
                }
            }
        }

        protected override Size DefaultSize
        {
            get
            {
                return new Size(100, 100);
            }
        }

        protected override ImeMode DefaultImeMode
        {
            get
            {
                return System.Windows.Forms.ImeMode.Disable;
            }
        }

        #endregion

        #region 停用属性

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new Padding Padding
        {
            get
            {
                return base.Padding;
            }
            set
            {
                base.Padding = value;
            }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new int TabIndex
        {
            get { return 0; }
            set { base.TabIndex = 0; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new bool TabStop
        {
            get { return false; }
            set { base.TabStop = false; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                base.Text = value;
            }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public override Font Font
        {
            get
            {
                return base.Font;
            }
            set
            {
                base.Font = value;
            }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public override Color ForeColor
        {
            get
            {
                return base.ForeColor;
            }
            set
            {
                base.ForeColor = value;
            }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public override RightToLeft RightToLeft
        {
            get
            {
                return base.RightToLeft;
            }
            set
            {
                base.RightToLeft = value;
            }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new ImeMode ImeMode
        {
            get
            {
                return base.ImeMode;
            }
            set
            {
                base.ImeMode = value;
            }
        }

        #endregion

        public PercentageProgressExt()
        {
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.ResizeRedraw, true);
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        }

        #region 重写

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            Graphics g = e.Graphics;

            int scale_arcRadius =(int)( this.ArcRadius * DotsPerInchHelper.DPIScale.XScale);
            int scale_arcThickness = (int)(this.ArcThickness * DotsPerInchHelper.DPIScale.XScale);
            int scale_squareWidth = (int)(this.SquareWidth * DotsPerInchHelper.DPIScale.XScale);
            Rectangle rect = new Rectangle((int)this.ClientRectangle.X + ((int)this.ClientRectangle.Width / 2 - scale_arcRadius) + 1, (int)this.ClientRectangle.Y + ((int)this.ClientRectangle.Height / 2 - scale_arcRadius) + 1, scale_arcRadius * 2 - 2, scale_arcRadius * 2 - 2);

            #region 环形
            if (this.Type == PercentageType.Annulus)
            {
                g.SmoothingMode = SmoothingMode.AntiAlias;
                #region 背景
                Pen arcback_pen = new Pen(this.ArcBackColor, scale_arcThickness);
                g.DrawArc(arcback_pen, ControlCommom.TransformRectangleByPen(rect, scale_arcThickness), 270, 360);
                arcback_pen.Dispose();
                #endregion

                if (this.ArcAnnulusBackColor != Color.Empty)
                {
                    SolidBrush arcannulusback_sb = new SolidBrush(this.ArcAnnulusBackColor);
                    g.FillPie(arcannulusback_sb, rect.X + scale_arcThickness / 2, rect.Y + scale_arcThickness / 2, rect.Width - scale_arcThickness, rect.Height - scale_arcThickness, 270, 360);
                    arcannulusback_sb.Dispose();
                }

                #region 百分比值
                Pen arc_pen = new Pen(this.ArcColor, scale_arcThickness);
                if (this.ArcRound)
                {
                    arc_pen.StartCap = LineCap.Round;
                    arc_pen.EndCap = LineCap.Round;
                }
                g.DrawArc(arc_pen, ControlCommom.TransformRectangleByPen(rect, scale_arcThickness), 270, 360 * this.Value);
                arc_pen.Dispose();
                #endregion

                #region 文本
                this.DrawValueText(g, rect);
                #endregion

            }
            #endregion
            #region 扇形
            else if (this.Type == PercentageType.Sector)
            {
                g.SmoothingMode = SmoothingMode.AntiAlias;
                #region 背景
                SolidBrush arcback_sb = new SolidBrush(this.ArcBackColor);
                g.FillPie(arcback_sb, rect.X, rect.Y, rect.Width, rect.Height, 270, 360);
                arcback_sb.Dispose();
                #endregion

                if (this.ArcAnnulusBackColor != Color.Empty)
                {
                    SolidBrush arcannulusback_sb = new SolidBrush(this.ArcAnnulusBackColor);
                    g.FillPie(arcannulusback_sb, rect.X + scale_arcThickness / 2, rect.Y + scale_arcThickness / 2, rect.Width - scale_arcThickness, rect.Height - scale_arcThickness, 270, 360);
                    arcannulusback_sb.Dispose();
                }

                #region 百分比值
                SolidBrush arc_sb = new SolidBrush(this.ArcColor);
                g.FillPie(arc_sb, rect.X, rect.Y, rect.Width, rect.Height, 270, 360 * this.Value);
                arc_sb.Dispose();
                #endregion

                #region 文本
                this.DrawValueText(g, rect);
                #endregion

            }
            #endregion
            #region 圆弧
            else if (this.Type == PercentageType.Arc)
            {
                g.SmoothingMode = SmoothingMode.AntiAlias;
                int start = this.ArcAngle == 360 ? 270 : 180 + (180 - this.ArcAngle) / 2;

                #region 背景
                Pen arcback_pen = new Pen(this.ArcBackColor, scale_arcThickness);
                g.DrawArc(arcback_pen, ControlCommom.TransformRectangleByPen(rect, scale_arcThickness), start, this.ArcAngle);
                arcback_pen.Dispose();
                #endregion

                #region 百分比值
                Pen arc_pen = new Pen(this.ArcColor, scale_arcThickness);
                if (this.ArcRound)
                {
                    arc_pen.EndCap = LineCap.Round;
                }
                g.DrawArc(arc_pen, ControlCommom.TransformRectangleByPen(rect, scale_arcThickness), start, this.ArcAngle * this.Value);
                arc_pen.Dispose();
                #endregion

                #region 文本
                this.DrawValueText(g, rect);
                #endregion

            }
            #endregion
            #region 方形
            else if (this.Type == PercentageType.Quadrangle)
            {
                RectangleF rectf = new RectangleF();
                rectf.Width = 10 + scale_squareWidth * 10;
                rectf.Height = 10 + scale_squareWidth * 10;
                rectf.X = this.ClientRectangle.X + (this.ClientRectangle.Width - rectf.Width) / 2;
                rectf.Y = this.ClientRectangle.Bottom - rectf.Height;

                #region 背景
                SolidBrush arcback_sb = new SolidBrush(this.ArcBackColor);
                g.FillRectangle(arcback_sb, rectf);
                arcback_sb.Dispose();
                #endregion

                #region 百分比值
                SolidBrush arc_sb = new SolidBrush(this.ArcColor);
                string str = this.Value.ToString("F4");
                int index = str.IndexOf('.');
                int row = int.Parse(str.Substring(index + 1, 1));
                RectangleF row_rectf = new RectangleF();
                row_rectf.Width = rectf.Width;
                row_rectf.Height = row * 1 + row * scale_squareWidth;
                row_rectf.X = rectf.X;
                row_rectf.Y = rectf.Bottom - row_rectf.Height;
                g.FillRectangle(arc_sb, row_rectf);

                int col = int.Parse(str.Substring(index + 2, 1));
                if (col > 0)
                {
                    RectangleF col_rectf = new RectangleF();
                    col_rectf.Width = col * 1 + col * scale_squareWidth;
                    col_rectf.Height = scale_squareWidth + 1;
                    col_rectf.X = rectf.X;
                    col_rectf.Y = row_rectf.Y - col_rectf.Height;
                    g.FillRectangle(arc_sb, col_rectf);
                }

                arc_sb.Dispose();
                #endregion

                #region 边框
                Pen arcbackborder_pen = new Pen(this.SquareBorderColor, 1);
                for (int i = 0; i < 11; i++)//行
                {
                    g.DrawLine(arcbackborder_pen, rectf.X, rectf.Y + i * 1 + i * scale_squareWidth, rectf.Right, rectf.Y + i * 1 + i * scale_squareWidth);
                }
                for (int i = 0; i < 11; i++)//列
                {
                    g.DrawLine(arcbackborder_pen, rectf.X + i * 1 + i * scale_squareWidth, rectf.Y, rectf.X + i * 1 + i * scale_squareWidth, rectf.Bottom);
                }
                arcbackborder_pen.Dispose();
                #endregion

                #region 百分比值文本
                string value_str = (this.Value * 100).ToString("F0") + "%";
                StringFormat value_sf = new StringFormat();
                value_sf.FormatFlags = StringFormatFlags.NoWrap;
                value_sf.Alignment = StringAlignment.Center;
                value_sf.Trimming = StringTrimming.None;
                Size value_size = g.MeasureString(value_str, this.ValueFont, new Size(), value_sf).ToSize();
                SolidBrush value_sb = new SolidBrush(this.ValueColor);
                g.DrawString(value_str, this.ValueFont, value_sb, new RectangleF(rectf.X + (rectf.Width - value_size.Width) / 2, rectf.Y - value_size.Height, value_size.Width, value_size.Height), value_sf);
                value_sb.Dispose();
                value_sf.Dispose();
                #endregion

            }
            #endregion
            #region 条形
            else if (this.Type == PercentageType.Bar)
            {
                if (this.ArcRound)
                {
                    g.SmoothingMode = SmoothingMode.AntiAlias;
                }
                RectangleF rectf = new RectangleF();
                rectf.Width = this.ClientRectangle.Width - (this.ArcRound ? scale_arcThickness : 0);
                rectf.Height = scale_arcThickness;
                rectf.X = this.ClientRectangle.X + (this.ArcRound ? scale_arcThickness / 2 : 0);
                rectf.Y = this.ClientRectangle.Bottom - rectf.Height;

                #region 背景
                Pen arcback_pen = new Pen(this.ArcBackColor, scale_arcThickness);
                if (this.ArcRound)
                {
                    arcback_pen.StartCap = LineCap.Round;
                    arcback_pen.EndCap = LineCap.Round;
                }
                g.DrawLine(arcback_pen, rectf.X, rectf.Bottom - rectf.Height / 2, rectf.Right, rectf.Bottom - rectf.Height / 2);
                arcback_pen.Dispose();
                #endregion

                #region 百分比值
                Pen arc_pen = new Pen(this.ArcColor, scale_arcThickness);
                if (this.ArcRound)
                {
                    arc_pen.StartCap = LineCap.Round;
                    arc_pen.EndCap = LineCap.Round;
                }
                g.DrawLine(arc_pen, rectf.X, rectf.Bottom - rectf.Height / 2, (int)(rectf.Right * this.Value), rectf.Bottom - rectf.Height / 2);
                arc_pen.Dispose();
                #endregion

                #region 百分比值文本
                string value_str = (this.Value * 100).ToString("F0") + "%";
                StringFormat value_sf = new StringFormat();
                value_sf.FormatFlags = StringFormatFlags.NoWrap;
                value_sf.Alignment = StringAlignment.Center;
                value_sf.Trimming = StringTrimming.None;
                Size value_size = g.MeasureString(value_str, this.ValueFont, new Size(), value_sf).ToSize();
                SolidBrush value_sb = new SolidBrush(this.ValueColor);
                g.DrawString(value_str, this.ValueFont, value_sb, new RectangleF(rectf.X + (rectf.Width - value_size.Width) / 2, rectf.Y - value_size.Height, value_size.Width, value_size.Height), value_sf);
                value_sb.Dispose();
                value_sf.Dispose();
                #endregion

            }
            #endregion

        }

        #endregion

        #region 虚方法

        protected virtual void OnValueChanged(ValueChangedEventArgs e)
        {
            if (this.valueChanged != null)
            {
                this.valueChanged(this, e);
            }
        }

        #endregion

        #region 私有方法

        /// <summary>
        /// 绘制百分值文本
        /// </summary>
        /// <param name="g"></param>
        /// <param name="rect"></param>
        private void DrawValueText(Graphics g, RectangleF rectf)
        {
            string value_str = (this.Value * 100).ToString("F0") + "%";
            StringFormat value_sf = new StringFormat();
            value_sf.FormatFlags = StringFormatFlags.NoWrap;
            value_sf.Alignment = StringAlignment.Center;
            value_sf.Trimming = StringTrimming.None;
            Size value_size = g.MeasureString(value_str, this.ValueFont, new Size(), value_sf).ToSize();
            SolidBrush value_sb = new SolidBrush(this.ValueColor);
            g.DrawString(value_str, this.ValueFont, value_sb, new RectangleF(rectf.X + (rectf.Width - value_size.Width) / 2f, rectf.Y + (rectf.Height - value_size.Height) / 2f, value_size.Width, value_size.Height), value_sf);
            value_sb.Dispose();
            value_sf.Dispose();
        }

        #endregion

        #region 类

        /// <summary>
        /// 百分比值更改事件参数
        /// </summary>
        [Description("百分比值更改事件参数")]
        public class ValueChangedEventArgs : EventArgs
        {
            /// <summary>
            /// 百分比值
            /// </summary>
            [Description("百分比值")]
            public float Value { get; set; }
        }

        #endregion

        #region 枚举

        /// <summary>
        /// 百分比显示类型
        /// </summary>
        [Description("百分比显示类型")]
        public enum PercentageType
        {
            /// <summary>
            /// 环形
            /// </summary>
            Annulus,
            /// <summary>
            /// 圆弧
            /// </summary>
            Arc,
            /// <summary>
            /// 扇形
            /// </summary>
            Sector,
            /// <summary>
            /// 方形
            /// </summary>
            Quadrangle,
            /// <summary>
            ///  条形
            /// </summary>
            Bar
        }

        #endregion

    }

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

落尘521

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值