自定义垂直进度条

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace VerticalProgressBar
{

    /// <summary>
    /// 枚举型进度条样式。
    /// </summary>
    public enum Styles
    {
        Classic,
        Solid
    }

    /// <summary>
    /// 枚举型进度条边框样式。
    /// </summary>
    public enum BorderStyles
    {
        Classic,
        None
    }

    /// <summary>
    /// 描画一格垂直进度条控件。
    /// </summary>
    [Description("Vertical Progress Bar")]
    [ToolboxBitmap(typeof(ProgressBar))]
    [Browsable(false)]
    public partial class VerticalProgressBar : UserControl
    {

        #region 属性默认值

       

        /// <summary>
        ///  当前值
        /// </summary>
        private int m_Value = 50;

      
        /// <summary>
        ///  最小值
        /// </summary>
        private int m_Minimum = 0;

      
        /// <summary>
        ///  最大值
        /// </summary>
        private int m_Maximum = 100;

      
        /// <summary>
        /// 步长
        /// </summary>
        private int m_Step = 10;

      
        /// <summary>
        ///  进度条样式
        /// </summary>
        private Styles m_Style = Styles.Classic;

        /// <summary>
        ///  边框样式
        /// </summary>
        private BorderStyles m_BorderStyle = BorderStyles.Classic;
       
        /// <summary>
        ///  进度条颜色
        /// </summary>      
        private Color m_Color = Color.Blue;
      
        /// <summary>
        /// 是否显示百分比
        /// </summary>         
        private bool m_Percent;

        #endregion


        #region 属性

        /// <summary>
        /// 初始化进度条。
        /// </summary>
        public VerticalProgressBar()
        {
            InitializeComponent();

            //设置样式。
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);

            //设置名称。
            this.Name = "VerticalProgressBar";

            //设置大小。
            this.Size = new Size(10, 120);
        }

        /// <summary>
        /// 百分比。
        /// </summary>
        [Description("VerticalProgressBar Percent")]
        [Category("VerticalProgressBar")]
        [RefreshProperties(RefreshProperties.All)]
        public bool Percent
        {
            get
            {
                return m_Percent;
            }
            set
            {
                m_Percent = value;
                Invalidate();
            }
        }


        /// <summary>
        /// 最大值。
        /// </summary>
        [Description("VerticalProgressBar Maximum Value")]
        [Category("VerticalProgressBar")]
        [RefreshProperties(RefreshProperties.All)]
        public int Maximum
        {
            get
            {
                return m_Maximum;
            }
            set
            {
                m_Maximum = value;
                if (m_Maximum < m_Minimum)
                {
                    m_Minimum = m_Maximum;
                }
                if (m_Maximum < m_Value)
                {
                    m_Value = m_Maximum;
                }
                Invalidate();
            }
        }


        /// <summary>
        /// 最小值。
        /// </summary>
        [Description("VerticalProgressBar Minimum Value")]
        [Category("VerticalProgressBar")]
        [RefreshProperties(RefreshProperties.All)]
        public int Minimum
        {
            get
            {
                return m_Minimum;
            }
            set
            {
                m_Minimum = value;
                if (m_Minimum > m_Maximum)
                {
                    m_Maximum = m_Minimum;
                }
                if (m_Minimum > m_Value)
                {
                    m_Value = m_Minimum;
                }
                Invalidate();
            }
        }

        /// <summary>
        /// 步长。
        /// </summary>
        [Description("VerticalProgressBar Step")]
        [Category("VerticalProgressBar")]
        [RefreshProperties(RefreshProperties.All)]
        public int Step
        {
            get
            {
                return m_Step;
            }
            set
            {
                m_Step = value;
            }
        }

        /// <summary>
        /// 当前值。
        /// </summary>
        [Description("VerticalProgressBar Current Value")]
        [Category("VerticalProgressBar")]
        public int Value
        {
            get
            {
                return m_Value;
            }
            set
            {
                m_Value = value;
                if (m_Value > m_Maximum)
                {
                    m_Value = m_Maximum;
                }
                if (m_Value < m_Minimum)
                {
                    m_Value = m_Minimum;
                }
                Invalidate();
            }
        }


        /// <summary>
        /// 进度条颜色。
        /// </summary>
        [Description("VerticalProgressBar Color")]
        [Category("VerticalProgressBar")]
        [RefreshProperties(RefreshProperties.All)]
        public System.Drawing.Color Color
        {
            get
            {
                return m_Color;
            }
            set
            {
                m_Color = value;
                Invalidate();
            }
        }


        /// <summary>
        /// 进度条边框颜色。
        /// </summary>
        [Description("VerticalProgressBar Border Style")]
        [Category("VerticalProgressBar")]
        public new BorderStyles BorderStyle
        {
            get
            {
                return m_BorderStyle;
            }
            set
            {
                m_BorderStyle = value;

                //重画无效区域。
                Invalidate();
            }
        }


        /// <summary>
        /// 进度条样式。
        /// </summary>
        [Description("VerticalProgressBar Style")]
        [Category("VerticalProgressBar")]
        public Styles Style
        {
            get
            {
                return m_Style;
            }
            set
            {
                m_Style = value;
                Invalidate();
            }
        }


        /// <summary>
        /// 执行步长。
        /// </summary>
        public void PerformStep()
        {
            m_Value += m_Step;

            if (m_Value > m_Maximum)
            {
                m_Value = m_Maximum;
            }
            if (m_Value < m_Minimum)
            {
                m_Value = m_Minimum;
            }

            Invalidate();
            return;
        }

        /// <summary>
        /// 改变进度。
        /// </summary>
        /// <param name="value"></param>
        public void Increment(int value)
        {
            m_Value += value;

            if (m_Value > m_Maximum)
            {
                m_Value = m_Maximum;
            }
            if (m_Value < m_Minimum)
            {
                m_Value = m_Minimum;
            }

            Invalidate();
            return;
        }

        #endregion


        /// <summary>
        /// 描画进度条边框。
        /// </summary>
        /// <param name="dc"></param>
        private void drawBorder(Graphics dc)
        {
            if (m_BorderStyle == BorderStyles.Classic)
            {
                Color darkColor = ControlPaint.Dark(this.BackColor);

                Pen p = new Pen(darkColor, 1);

                //左侧线。
                dc.DrawLine(p, this.Width, 0, 0, 0);

                //上部线。
                dc.DrawLine(p, 0, 0, 0, this.Height);  
            

                //底部线。
                dc.DrawLine(p, 0, this.Height - 1, this.Width, this.Height - 1);
                //dc.DrawLine(p, 0, 10, 10, 10);

                //右侧线。
                dc.DrawLine(p, this.Width - 1, this.Height, this.Width - 1, 0);
            }
        }


        /// <summary>
        /// 描画进度条主体。
        /// </summary>
        /// <param name="dc">Graphics对象</param>
        private void drawBar(Graphics dc)
        {
            if (m_Minimum == m_Maximum || (m_Value - m_Minimum) == 0)
                return;

            int width;   // the bar width
            int height;   // the bar height
            int x;      // the bottom-left x pos of the bar
            int y;    // the bottom-left y pos of the bar

            if (m_BorderStyle == BorderStyles.None)
            {
                width = this.Width;
                x = 0;
                y = this.Height;
            }
            else
            {
                if (this.Width > 4 || this.Height > 2)
                {
                    width = this.Width - 4;
                    x = 2;
                    y = this.Height - 1;
                }
                else
                {
                    //不描画。
                    return;
                }
            }

            //进度条高度。
            height = (m_Value - m_Minimum) * this.Height / (m_Maximum - m_Minimum);

            if (m_Style == Styles.Solid)
            {
                drawSolidBar(dc, x, y, width, height);
            }

            if (m_Style == Styles.Classic)
            {
                drawClassicBar(dc, x, y, width, height);
            }
        }


        /// <summary>
        /// 描画进度条样式为Solid
        /// </summary>
        /// <param name="dc">Graphics对象</param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        private void drawSolidBar(Graphics dc, int x, int y, int width, int height)
        {
            dc.FillRectangle(new SolidBrush(m_Color), x, y - height, width, height);
        }


        /// <summary>
        /// 描画进度条样式为Classic
        /// </summary>
        /// <param name="dc">Graphics 对象</param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        private void drawClassicBar(Graphics dc, int x, int y, int width, int height)
        {
            int valuepos_y = y - height;    // The pos y of value

            int blockheight = width * 3 / 4;        // The height of the block

            if (blockheight <= -1) return; // make sure blockheight is larger than -1 in order not to have the infinite loop.

            for (int currentpos = y; currentpos > valuepos_y; currentpos -= blockheight + 1)
            {
                dc.FillRectangle(new SolidBrush(m_Color), x, currentpos - blockheight, width, blockheight);
            }
        }


        /// <summary>
        /// 开始描画。
        /// </summary>
        /// <param name="e">PaintEventArgs 对象</param>
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics dc = e.Graphics;

            //描画进度条。
            drawBar(dc);

            if (this.Percent == true)
            {
                DrawStringRectangleFFormat(e);
            }

            //描画边框。
            drawBorder(dc);

            base.OnPaint(e);
        }


        /// <summary>
        /// 描画字体。
        /// </summary>
        /// <param name="e">PaintEventArgs 对象</param>
        public void DrawStringRectangleFFormat(PaintEventArgs e)
        {

            //要描画的字符串。
            String drawString = this.m_Value.ToString() + "%";

            // 字体大小样式。
            Font drawFont = new Font("Arial", 8);

            //字体颜色。
            SolidBrush drawBrush = new SolidBrush(Color.Red);

            //描画区域。
            RectangleF drawRect = new RectangleF(this.Width / 20, this.Height / 2, this.Width, this.Height / 2);

            //字体格式。
            StringFormat drawFormat = new StringFormat();
            drawFormat.Alignment = StringAlignment.Center;

            //描画字体。
            e.Graphics.DrawString(drawString, drawFont, drawBrush, drawRect, drawFormat);
        }


        /// <summary>
        /// 改变大小
        /// </summary>
        /// <param name="e">EventArgs 对象</param>
        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            Invalidate();
        }

       
      
    }
}

 

posted on 2008-10-20 11:59  踏燕归来  阅读(401)  评论(0编辑  收藏  举报