1. 程式人生 > >C#自定義進度條,可用於音樂播放器進度調整,音量調整等功能

C#自定義進度條,可用於音樂播放器進度調整,音量調整等功能

平時在做c#專案時偶爾會碰到要使用進度條的情況,但c#自帶的進度條外觀往往不合我們心意,這就需要我們自己動手來只做一款自己的進度條。先上圖:

      

外觀雖然簡單,但感覺比c#自帶的好看多了。

繪製這樣一個進度條需要兩個基本控制元件,兩個Label,一個picturebox,下面是myProgressBar類的程式碼

 public class myProgressBar
    {
        public PictureBox picFocus;
        public Label lbltop;
        public Label lblbottom;
        int value = 0;
        int x1;
        int y1;
        int h1;
        int w1;
        Form f1;

        public myProgressBar(Form f, int x, int y, int width, int high)
        {
            //繪製進度條,用兩個label以及一個picturebox組合為進度條
            lblbottom = new Label();
            lbltop = new Label();
            lblbottom.BackColor = Color.Gray;
            lbltop.BackColor = Color.Yellow;

            //進度條上的游標
            picFocus = new PictureBox();
            picFocus.Size = new Size(high+6, high+6 );
            picFocus.Location = new Point(x + value, y - 3);
            picFocus.BackgroundImage = Image.FromFile("yellowyuan.png");
            picFocus.BackgroundImageLayout = ImageLayout.Stretch;
            picFocus.BackColor = Color.Transparent;

            lblbottom.SetBounds(x, y, width, high);
            lbltop.Size = new Size(value, high);
            lblbottom.Controls.Add(lbltop);
            f.Controls.Add(picFocus);
            f.Controls.Add(lblbottom);

            x1 = x;
            y1 = y;
            h1 = high;
            w1 = width;
            f1 = f;

        }
        public void updateFocus(int value)
        {
            picFocus.Location = new Point(x1 + value, y1 - 3);
            lbltop.Size = new Size(value, h1);
        }


        //進度條拖動
        Point clickPoint;
        public bool isDrag;
        internal void picFocus_MouseDown(object sender, MouseEventArgs e)
        {
                isDrag = true;
                clickPoint = new Point(e.X, e.Y);
        }

        int offsetX;
        internal void picFocus_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDrag)
            {
                offsetX = e.X - clickPoint.X;

                //若進度條沒有超出可拖動範圍
                if (picFocus.Location.X >= lblbottom.Location.X && picFocus.Location.X <= lblbottom.Location.X + w1 - picFocus.Width)
                {
                    picFocus.Location = new Point(picFocus.Location.X + offsetX, picFocus.Location.Y);
                }
                //若進度條超出最小範圍,將他置於最小位置
                if (picFocus.Location.X < lblbottom.Location.X)
                {
                    picFocus.Location = new Point(lblbottom.Location.X, picFocus.Location.Y);
                }
                //若進度條超出最大範圍,將他置於最大位置
                if (picFocus.Location.X > lblbottom.Location.X + w1 - picFocus.Width)
                {
                    picFocus.Location = new Point(lblbottom.Location.X + w1 - picFocus.Width, picFocus.Location.Y);
                }
                //改變lbltop長度
                int value = picFocus.Location.X - lblbottom.Location.X;
                this.updateFocus(value);
            }
        }

        internal void picFocus_MouseUp(object sender, MouseEventArgs e)
        {
            isDrag = false;
        }

然後在Form中新增load事件,並填寫如下程式碼:

 private void Form1_Load(object sender, EventArgs e)
        {
            //生成進度條
            myProgressBar mbp = new myProgressBar(this,200,200,200,8);
            this.Controls.Add(mbp.picFocus);
            this.Controls.Add(mbp.lblbottom);
            //註冊進度條拖動事件
            mbp.picFocus.MouseDown += new MouseEventHandler(mbp.picFocus_MouseDown);
            mbp.picFocus.MouseMove += new MouseEventHandler(mbp.picFocus_MouseMove);
            mbp.picFocus.MouseUp += new MouseEventHandler(mbp.picFocus_MouseUp);

        }

OK,大功告成!