有颜色的竖直进度条

在竖直进度条的基础上使用其他颜色覆盖空白处,并显示数值,代码如下

public VerticalProgressBar()
        {
            InitializeComponent();
            this.DoubleBuffered = true;//设置本窗体
            this.SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.DoubleBuffer, true); // 开启双缓冲,防止闪烁
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            Rectangle rect = new Rectangle(base.ClientRectangle.Location, new Size(base.Width, base.Height));
            ProgressBarRenderer.DrawVerticalBar(e.Graphics, rect);
            if (this.Value <= 10) //值小于等于10时显示红色
            {
                LinearGradientBrush lb = new LinearGradientBrush(new Rectangle(base.ClientRectangle.X, base.ClientRectangle.Y + base.ClientRectangle.Height - base.Height * base.Value / base.Maximum, base.Width, base.Height * base.Value / base.Maximum + 1), Color.Red, Color.Red, 0F);
                e.Graphics.FillRectangle(lb, new Rectangle(base.ClientRectangle.X, base.ClientRectangle.Y + base.ClientRectangle.Height - base.Height * base.Value / 100, base.Width, base.Height * base.Value / base.Maximum));
            }
            else if(this.Value>10 && this.Value<=20)//值大于10小于等于20时显示黄色
            {
                LinearGradientBrush lb = new LinearGradientBrush(new Rectangle(base.ClientRectangle.X, base.ClientRectangle.Y + base.ClientRectangle.Height - base.Height * base.Value / base.Maximum, base.Width, base.Height * base.Value / base.Maximum + 1), Color.Yellow, Color.Yellow, 0F);
                e.Graphics.FillRectangle(lb, new Rectangle(base.ClientRectangle.X, base.ClientRectangle.Y + base.ClientRectangle.Height - base.Height * base.Value / 100, base.Width, base.Height * base.Value / base.Maximum));
            }
            else //显示绿色
            {
                LinearGradientBrush lb = new LinearGradientBrush(new Rectangle(base.ClientRectangle.X, base.ClientRectangle.Y + base.ClientRectangle.Height - base.Height * base.Value / base.Maximum, base.Width, base.Height * base.Value / base.Maximum + 1), Color.Green, Color.Green, 0F);
                e.Graphics.FillRectangle(lb, new Rectangle(base.ClientRectangle.X, base.ClientRectangle.Y + base.ClientRectangle.Height - base.Height * base.Value / 100, base.Width, base.Height * base.Value / base.Maximum));
            }

            #region 空白处使用黑色覆盖
            LinearGradientBrush lb1 = new LinearGradientBrush(new Rectangle(base.ClientRectangle.X, base.ClientRectangle.Y, base.Width, base.Height - base.Height * base.Value / base.Maximum - 1), Color.Black, Color.Black, 0F);
            e.Graphics.FillRectangle(lb1, new Rectangle(base.ClientRectangle.X, base.ClientRectangle.Y, base.Width, base.Height * (100 - base.Value) / 100));
            #endregion

            #region 显示数值
            string text = string.Format("{0}%", Value); ;
            using (var font = new Font(FontFamily.GenericMonospace, 11))
            {
                SizeF sz = g.MeasureString(text, font);
                if (Value < 100)
                {
                    var location = new PointF(rect.Width / 2 - sz.Width / 2, base.ClientRectangle.Y + base.ClientRectangle.Height - base.Height * base.Value / 100 - sz.Height + 2);
                    g.DrawString(text, font, Brushes.White, location);
                }
                else
                {
                    var location = new PointF(rect.Width / 2 - sz.Width / 2, base.ClientRectangle.Y + base.ClientRectangle.Height - base.Height * base.Value / 100 + sz.Height / 4);
                    g.DrawString(text, font, Brushes.White, location);
                }
            } 
            #endregion
        }
    }

效果如下:
在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章