有顏色的豎直進度條

在豎直進度條的基礎上使用其他顏色覆蓋空白處,並顯示數值,代碼如下

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
        }
    }

效果如下:
在這裏插入圖片描述

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