C# 自定义VScrollBar控件

   原生态的VScrollBar真是太丑了,如下图,其实有个简单的方法去实现同样的功能和样式

  

 自己做的VSrollBar

  



 下面就说代码了

  SAScoll.cs

  

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SchoolApplication.Utils;
using SchoolApplication.Data;


namespace SchoolApplication.Views
{
    public delegate void SASValueChangeDelegate(SAScoll sender, int value);

    public partial class SAScoll : UserControl
    {
        bool IsVerical = true;

        public SAScoll(bool isVer)
        {
            InitializeComponent();
            IsVerical = isVer;
        }

        public SAScoll()
        {
            InitializeComponent();
        }

       public event SASValueChangeDelegate SASValueChange;

        public int MinValue = 0;

        public int MaxValue = 100;

        public int CurrentValue = 20;

        int barHeight = 20;

        Rectangle rect = new Rectangle(); 

        bool OnDraw = false;

        private void SAScoll_Load(object sender, EventArgs e)
        {
            rect = new Rectangle(0, 0, this.Width, barHeight);
            this.MouseDown += SAScoll_MouseDown;
            this.MouseMove += SAScoll_MouseMove;
            this.MouseUp += SAScoll_MouseUp;
            BackColor = PubData.BgColor;
        }

        void SAScoll_MouseUp(object sender, MouseEventArgs e)
        {
            OnDraw = false;
        }

        public Brush FillBrush = Brushes.DeepSkyBlue;

        private void SAScoll_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = this.CreateGraphics();
            Pen pen = new Pen(Brushes.DeepSkyBlue);
            pen.Width = 0;
            g.DrawRectangle(pen, rect);
            //g.FillEllipse(Brushes.LightGray, rect);
            g.FillRectangle(FillBrush, rect);
            // 这里可以画矩形,椭圆,都可以
        }

        int SAValue = 0;

        void SAScoll_MouseMove(object sender, MouseEventArgs e)
        {
            if (OnDraw)
            {

                if (IsVerical == true)
                {
                    #region isVer  = true
                    rect = new Rectangle(new Point(0, e.Location.Y), new Size(this.Width, barHeight));
                    if (rect.Y + rect.Height >= this.Height)
                    {
                        rect.Y = this.Height - barHeight;
                    }
                    else if (rect.Y < 0)
                    {
                        rect.Y = 0;
                    }


                    SAValue = (int)((rect.Y + rect.Height / 2) * ((double)MaxValue / (double)this.Height));

                    if (rect.Y == 0)
                    {
                        SAValue = 0;
                    }

                    if (rect.Y == this.Height - barHeight)
                    {
                        SAValue = MaxValue;
                    }

                    #endregion
                }
                else
                {
                    rect = new Rectangle(new Point(e.Location.X,0), new Size( barHeight,this.Height));
                    if (rect.X + rect.Width >= this.Width)
                    {
                        rect.X = this.Width - barHeight;
                    }
                    else if (rect.X < 0)
                    {
                        rect.X = 0;
                    }

                     
                    SAValue = (int)((rect.X + rect.Width / 2) * ((double)MaxValue / (double)this.Width));

                    if (rect.X == 0)
                    {
                        SAValue = 0;
                    }

                    if (rect.X == this.Width - barHeight)
                    {
                        SAValue = MaxValue;
                    }
                }


                //更新绘制
                Invalidate();

                if (SASValueChange != null)
                {
                    SASValueChange(this, SAValue);
                }
            } 
        }

        void SAScoll_MouseDown(object sender, MouseEventArgs e)
        {
            OnDraw = true;
        }
    }
}


Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text; 
using System.Windows.Forms;
 
namespace SchoolApplication.Forms
{
    public partial class Test : Form
    {
        public Test()
        {
            InitializeComponent();
        }
        private void Test_Load(object sender, EventArgs e)
        {

            SAScoll SAS = new SAScoll();
            SAS.Size = new Size(30, 500);
            SAS.Location = new Point(100, 20);
            this.Controls.Add(SAS);
            SAS.SASValueChange += SAS_SASValueChange;
        }

        void SAS_SASValueChange(object sender, int value)
        {
            Text = "值"+value;
        }
    }
}



  







猜你喜欢

转载自blog.csdn.net/taoerit/article/details/80992502