初學c# -- 學習筆記(五) winfrom自定義滾動條

找了些例子,要麼龐大、要麼搞個安裝組件什麼的,我要求能用就好了。實在找例子修改麻煩,就作了一個。其實實現挺簡單,就是panel或圖片什麼的跟着鼠標走就好了。spa

這裏panel本身能夠加背景圖或直接搞個圖就好了。爲了演示清楚,有個滾動條控件作對比,與自定義的同步。code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace customscroll
{
    public partial class Form1 : Form
    {
        int limt, set_x; //滾動位置最大值和固定的左右的位置
        bool mouse_Press = false; //鼠標按下
        bool mouse_Wheel = false; //滑輪是否滾動
        Point mouseOff; //存放當前鼠標位置

        public Form1()
        {
            InitializeComponent();
            //向panel填充一堆內容
            for (int i = 0; i < 25; i++)
            {
                Panel num_panel = new Panel();
                Label num = new Label();

                num.Text = i + ".";
                num.ForeColor = Color.FromArgb(255, 255, 255);
                num.Width = 30;
                num.Dock = DockStyle.Left;
                //設置鼠標滑輪事件,需將mouse_Wheel 值爲true,在move裏置,下面有,這裏就不寫了
                //num.MouseWheel += new MouseEventHandler(OnMouseWheel);

                num_panel.Name = "Panel_" + i;
                num_panel.Height = 35;
                num_panel.Margin = new Padding(0, 0, 0, 0);
                num_panel.BackColor = Color.SteelBlue;
                num_panel.BorderStyle = BorderStyle.Fixed3D;
                num_panel.Dock = DockStyle.Top;
                num_panel.BorderStyle = System.Windows.Forms.BorderStyle.None;
                num_panel.Controls.Add(num);
                //設置鼠標滑輪事件,需將mouse_Wheel 值爲true,在move裏置,下面有,這裏就不寫了
                //num_panel.MouseWheel += new MouseEventHandler(OnMouseWheel);
                Content_panel.Controls.Add(num_panel); //將內容裝入
                //設置鼠標滑輪事件,需將mouse_Wheel 值爲true,在move裏置,下面有,這裏就不寫了
                //Content_panel.MouseWheel += new MouseEventHandler(OnMouseWheel);
            }
            //裝內容panel自動大小
            Content_panel.AutoSize = true;

            set_x = ScrollHard_panel.Location.X; //固定左右位置爲當前位置
            limt = ScrollBar_panel.Height - ScrollHard_panel.Height; //滾動最大高度
            ScrollHard_panel.Location = new Point(set_x,0) ; //先將位置設置到最頂
            vScrollBar1.Maximum = limt; //放了個vScrollBar組件,演示用的,和自定義的同步

            //鼠標滑輪事件
            ScrollBar_panel.MouseWheel += new MouseEventHandler(OnMouseWheel);
            ScrollHard_panel.MouseWheel += new MouseEventHandler(OnMouseWheel);
            vScrollBar1.MouseWheel += new MouseEventHandler(OnMouseWheel);
        }

        //鼠標滑輪事件
        private void OnMouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            int set_y = 0;

            if (mouse_Wheel) //是否判斷鼠標滑輪
            {
                if (e.Delta > 0) //滑輪向上
                {
                    set_y = ScrollHard_panel.Location.Y - 10; //每次移動10
                    if (set_y < 0) { set_y = 0;  } //超範圍
                }
                if (e.Delta < 0)  //滑輪向下
                {
                    set_y = ScrollHard_panel.Location.Y + 10; //每次移動10
                    if (set_y > limt) { set_y = limt; } //超範圍
                }
                ScrollHard_panel.Location = new Point(set_x, set_y); //滾動塊的定位
                vScrollBar1.Value = set_y; //演示用的滾動條,和自定義的同步

                Content_panel.Top = -set_y; //裝內容的panel滾動顯示
            }

        }

        //自定義滾動「塊」框鼠標按下
        private void ScrollHard_panel_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left) //鼠標左鍵
            {
                mouseOff.Y = e.Y;  //取當前位置
                mouse_Press = true; //鼠標按下
            }
        }

        //自定義滾動「塊」鼠標放開
        private void ScrollHard_panel_MouseUp(object sender, MouseEventArgs e)
        {
            mouse_Press = false; //鼠標放開
        }

        //自定義滾動「塊」鼠標離開範圍
        private void ScrollHard_panel_MouseLeave(object sender, EventArgs e)
        {
            mouse_Wheel = false; //滑輪不可用
        }

        ////自定義滾動「塊」鼠標在範圍
        private void ScrollHard_panel_MouseMove(object sender, MouseEventArgs e)
        {
            mouse_Wheel = true; //能夠用滑輪
            if (mouse_Press) //鼠標按下狀態
            {
                int set_y = ScrollHard_panel.Top + e.Y - mouseOff.Y; //計算當前縱向座標
                if (set_y < 0) { set_y = 0; } //超範圍
                else if (set_y > limt) { set_y = limt; } //超範圍
                else { ScrollHard_panel.Location = new Point(set_x, set_y); } //滾動塊的定位
                vScrollBar1.Value = set_y; //演示的滾動條和自定義的同步
                Content_panel.Top = -set_y; //裝內容的panel滾動顯示
            }
        }

        //在滾動「框」範圍內
        private void ScrollBar_panel_MouseMove(object sender, MouseEventArgs e)
        {
            mouse_Wheel = true;  //能夠使用滑輪
        }

        //離開滾動「框」
        private void ScrollBar_panel_MouseLeave(object sender, EventArgs e)
        {
            mouse_Wheel = false; //不可以使用滑輪
        }

        //自定義滾動「框」鼠標放開
        private void ScrollBar_panel_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left) //鼠標左鍵
            {
                int set_y = e.Y; //當前縱座標
                if (set_y > limt) { set_y = limt; } //超範圍
                ScrollHard_panel.Location = new Point(set_x, set_y); //滾動塊定位
                vScrollBar1.Value = set_y; //演示的滾動條,和自定義的同步
                Content_panel.Top = -set_y;//裝內容的panel滾動顯示
                mouse_Press = false; //鼠標爲放開狀態
            }
        }

        //演示用的vScrollBar1組件,也能夠控制裝內容的panel滾動顯示
        private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
            Content_panel.Top = -vScrollBar1.Value;
        }
    }
}
相關文章
相關標籤/搜索