C#winform自定義滾動條


1.控件

一個UserControl作為ScrollBg,一個panel作為ScrollBar

2.實現功能

(1)設置滾動條背景顏色和背景圖片

(2)設置滾動條滑塊的背景顏色和背景圖片

(3)鼠標左鍵拖動滑塊上下滑動

(4)鼠標進入和離開滑塊事件

(5)滾動鼠標中間滾輪事件

(6)鼠標左鍵點擊滾動條除去滑塊的位置,上下移動滑塊

(7)鼠標左鍵長時間按住滾動條除去滑塊的位置,上下移動滑塊

3.關鍵數據

 

滑塊大小:  BoxPanelHeight/ContentPanelHeight*ScrollBgHeight

ContenPanel縱坐標:  -ScrollBar.Top /( ScrollBgHeigh - ScrollBgHeight)* (ContentPanelHeight - BoxPanelHeight));

4.代碼

(1)自定義控件

public partial class ScrollBarBg : UserControl
    {
        #region private properties

        private Timer ScrollTimer = null;

        /// <summary>
        /// 框架面板高度
        /// </summary>
        private int BoxPanelHeight = 0;

        /// <summary>
        /// 內容面板高度
        /// </summary>
        private int ContentPanelHeight = 0;

        /// <summary>
        /// 滾動條滑塊鼠標左鍵是否按下
        /// </summary>
        private bool IsLeftKeyDown_ScrollBar = false;

        /// <summary>
        /// 滾動條背景板鼠標左鍵是否按下
        /// </summary>
        private bool IsLeftKeyDown_ScrollBg = false;

        /// <summary>
        /// 鼠標相對有屏幕的Y坐標
        /// </summary>
        private int MouseYToScreen = 0;

        #endregion

        public ScrollBarBg()
        {
            InitializeComponent();
            this.ScrollBar.Top = 0;
            this.MouseWheel += ScrollBar_MouseWheel;
            ScrollBar.MouseWheel += ScrollBar_MouseWheel;
            ScrollTimer = new Timer();
            ScrollTimer.Interval = 200;
            ScrollTimer.Tick += Timer_Tick;
        }

        #region public

        /// <summary>
        /// 滑塊高度
        /// </summary>
        public int ScrollBarHeight
        {
            get { return this.ScrollBar.Height; }
        }

        /// <summary>
        /// 滑塊在滾動條上的位置
        /// </summary>
        public int ScrollBarTop
        {
            set { ScrollBarAndContent(value); }
            get { return this.ScrollBar.Top; }
        }

        /// <summary>
        /// 滑塊背景顏色
        /// </summary>
        public Color ScrollBarBackColor
        {
            set { this.ScrollBar.BackColor = value; }
            get { return ScrollBar.BackColor; }
        }

        /// <summary>
        /// 滑塊背景圖片
        /// </summary>
        public Image ScrollBarBackgroundImage
        {
            set { this.ScrollBar.BackgroundImage = value; }
            get { return ScrollBar.BackgroundImage; }
        }

        /// <summary>
        /// 鼠標進入滾動條滑塊事件
        /// </summary>
        public event EventHandler ScrollBarMouseEnter = null;

        /// <summary>
        /// 鼠標離開滾動條滑塊事件
        /// </summary>
        public event EventHandler ScrollBarMouseLeave = null;

        /// <summary>
        /// 滑塊滾動發生
        /// int:內容面板相對於框架面板的高度
        /// </summary>
        public Action<int> ScrollBarScroll = null;

        /// <summary>
        /// 鼠標轉動滾輪滑塊滾動的單位高度,影響滾動速度
        /// </summary>
        private int mouseWheelUnitHeight = 10;
        public int MouseWheelUnitHeight
        {
            set { mouseWheelUnitHeight = value; }
            get { return mouseWheelUnitHeight; }
        }

        /// <summary>
        /// 長按滾動條背景框滑塊滾動的單位高度,影響滾動速度
        /// </summary>
        private int timerScrollUnitHeight = 40;
        public int TimerScrollUnitHeight
        {
            set { timerScrollUnitHeight = value; }
            get { return timerScrollUnitHeight; }
        }

        /// <summary>
        /// 設置滑塊高度
        /// </summary>
        /// <param name="panelHeight">面板高度</param>
        /// <param name="contentHeight">內容高度</param>
        public void SetScrollBarHeight(int boxPanelHeight, int contentPanelHeight)
        {
            BoxPanelHeight = boxPanelHeight;
            ContentPanelHeight = contentPanelHeight;
            ScrollBar.Height = (int)((double)boxPanelHeight / contentPanelHeight * this.Height);
            MouseWheel += ScrollBar_MouseWheel;
        }

        /// <summary>
        /// 內容面板鼠標滾動
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void ContentPanelMouseWheel(object sender, MouseEventArgs e)
        {
            ScrollBar_MouseWheel(sender, e);
        }

        #endregion

        #region private

        private void ScrollBar_MouseWheel(object sender, MouseEventArgs e)
        {
            int ScrollBarTop = e.Delta > 0 ? this.ScrollBar.Top - mouseWheelUnitHeight : this.ScrollBar.Top + mouseWheelUnitHeight;
            ScrollBarAndContent(ScrollBarTop);
        }

        private void ScrollBar_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                IsLeftKeyDown_ScrollBar = true;
                MouseYToScreen = Control.MousePosition.Y;
            }
        }

        private void ScrollBar_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
                IsLeftKeyDown_ScrollBar = false;
        }

        private void ScrollBar_MouseMove(object sender, MouseEventArgs e)
        {
            if (IsLeftKeyDown_ScrollBar)
            {
                int ScrollBarTop = this.ScrollBar.Top + Control.MousePosition.Y - MouseYToScreen;
                MouseYToScreen = Control.MousePosition.Y;

                ScrollBarAndContent(ScrollBarTop);
            }
        }

        private void ScrollBar_MouseEnter(object sender, EventArgs e)
        {
            if (ScrollBarMouseEnter != null)
                ScrollBarMouseEnter(sender, e);
        }

        private void ScrollBar_MouseLeave(object sender, EventArgs e)
        {
            if (ScrollBarMouseLeave != null)
                ScrollBarMouseLeave(sender, e);
        }

        private void ScrollBarBg_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                IsLeftKeyDown_ScrollBg = true;
                ScrollTimer.Start();
            }
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            if (!IsLeftKeyDown_ScrollBg)
                goto ret;

            int ScrollBarTop = this.ScrollBar.Top;
            int ScrollBarMiddlePointY = ScrollBarTop + ScrollBar.Height / 2;
            int MousePointToClientY = this.PointToClient(Control.MousePosition).Y;
            if (ScrollBarMiddlePointY > MousePointToClientY)
            {
                if (ScrollBarMiddlePointY - timerScrollUnitHeight < this.PointToClient(Control.MousePosition).Y)
                    goto ret;
                else
                    ScrollBarTop -= timerScrollUnitHeight;
            }
            else
            {
                if (ScrollBarMiddlePointY + timerScrollUnitHeight > this.PointToClient(Control.MousePosition).Y)
                    goto ret;
                else
                    ScrollBarTop += timerScrollUnitHeight;
            }

            this.BeginInvoke(new Action(() =>
            {
                ScrollBarAndContent(ScrollBarTop);
            }));
            return;

            ret:
            IsLeftKeyDown_ScrollBg = false;
            ScrollTimer.Stop();
        }

        private void ScrollBarBg_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && IsLeftKeyDown_ScrollBg)
            {
                IsLeftKeyDown_ScrollBg = false;
                int ScrollBarTop = this.ScrollBar.Top + ScrollBar.Height / 2 > e.Y ? this.ScrollBar.Top - timerScrollUnitHeight : this.ScrollBar.Top + timerScrollUnitHeight;
                ScrollBarAndContent(ScrollBarTop);
            }
        }

        private void ScrollBarControl_SizeChanged(object sender, EventArgs e)
        {
            this.ScrollBar.Width = this.Width;
        }

        private void ScrollBarAndContent(int ScrollBarTop)
        {
            if (ScrollBarTop <= 0)
                this.ScrollBar.Top = 0;
            else if (ScrollBarTop >= this.Height - this.ScrollBar.Height)
                this.ScrollBar.Top = this.Height - this.ScrollBar.Height;
            else
                this.ScrollBar.Top = ScrollBarTop;

            int Y = (int)(this.ScrollBar.Top / (double)(this.Height - this.ScrollBar.Height) * (ContentPanelHeight - BoxPanelHeight));
            if (ScrollBarScroll != null)
                ScrollBarScroll(-Y);
        }

        #endregion
    }
View Code

(2)使用

        private void Form2_Load(object sender, EventArgs e)
        {
            scrollBarBg1.ScrollBarScroll += ScrollBarScroll;
            pnContent.MouseWheel += PnContent_MouseWheel;

            scrollBarBg1.SetScrollBarHeight(this.pnBox.Height, this.pnContent.Height);
            scrollBarBg1.ScrollBarTop = 0;
        }

        private void PnContent_MouseWheel(object sender, MouseEventArgs e)
        {
            scrollBarBg1.ContentPanelMouseWheel(sender, e);
        }

        private void ScrollBarScroll(int y)
        {
            this.pnContent.Top = y;
        }
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM