• TreeView自定义滑动条


    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 treeview

    {

        public partial class Form_TreeView : Form

        {

            int treeViewLocationY = 0;//定高变量

            int moveDistance = 0 , moveArea = 0; //移动参数

            int B_Height = 0 , H_Height = 0; //滑动条高度

            int one_Nodes = 0, two_Nodes = 0, three_Nodes = 0, four_Nodes = 0;//节点数

            bool mouse_Press = false; //鼠标按下

            bool mouse_Wheel = false; //滑轮是否滚动

            Point mouseOff; //存放当前鼠标位置

            Point treeViewLocation; //初始位置

            public Form_TreeView()

            {

                InitializeComponent();

                treeViewLocation = this.treeView.Location;

                B_Height = ScrollBar_panel.Height;

                ScrollHard_panel.Location = new Point(0, 0);

                Set_Scroll();

                //鼠标事件

                ScrollHard_panel.MouseWheel += new MouseEventHandler(OnMouseWheel);

                ScrollHard_panel.MouseDown += new MouseEventHandler(ScrollHard_panel_MouseDown);

                ScrollHard_panel.MouseUp += new MouseEventHandler(ScrollHard_panel_MouseUp);

                ScrollHard_panel.MouseMove += new MouseEventHandler(ScrollHard_panel_MouseMove);

                ScrollHard_panel.MouseLeave += ScrollHard_panel_MouseLeave;

                timer1.Start();

            }

            //鼠标按下

            private void ScrollHard_panel_MouseDown(object sender, MouseEventArgs e)

            {

                if (e.Button == MouseButtons.Left) //鼠标左键

                {

                    treeViewLocationY = this.treeView.Location.Y;

                    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 > moveArea) { set_y = moveArea; } //超范围

                    else { ScrollHard_panel.Location = new Point(ScrollHard_panel.Location.X, set_y); } //滚动块的定位   

                    float num = (e.Y - mouseOff.Y) / (float)moveArea * (treeView.Height - ScrollBar_panel.Height);

                     

                    if (ScrollHard_panel.Location.Y <= 1)

                    {

                        treeView.Location = treeViewLocation;

                    }

                    else if (ScrollHard_panel.Location.Y >= moveArea-1)

                    {

                        treeView.Location = new Point(this.treeView.Location.X, -(treeView.Height - ScrollBar_panel.Height));

                    }

                    else 

                    {

                        treeView.Location = new Point(treeView.Location.X, this.treeView.Location.Y - (int)num);

                    }

                }

            }

            //定位TreeView

            private void TreeViewLocation()

            {

                if (ScrollHard_panel.Location.Y <= 1)

                {

                    treeView.Location = treeViewLocation;

                }

                else if (ScrollHard_panel.Location.Y >= moveArea - 1)

                {

                    treeView.Location = new Point(treeView.Location.X, -(treeView.Height - ScrollBar_panel.Height));

                }

                else

                {

                    treeView.Location = new Point(treeView.Location.X, treeViewLocationY - moveDistance);

                }

            }

            //鼠标滑轮事件

            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 > moveArea) { set_y = moveArea; } //超范围

                    }

                    ScrollHard_panel.Location = new Point(0, set_y); //滚动块的定位

                    moveDistance = (int)(set_y / (float)moveArea * (treeView.Height - ScrollBar_panel.Height));

                    TreeViewLocation();

                }

            }

            private void top_btn_Click(object sender, EventArgs e)

            {

                int set_y = ScrollHard_panel.Location.Y - 10; //每次移动10

                if (set_y < 0) { set_y = 0; } //超范围

                ScrollHard_panel.Location = new Point(0, set_y); //滚动块的定位

                moveDistance = (int)(set_y / (float)moveArea * (treeView.Height - ScrollBar_panel.Height));

                TreeViewLocation();

            }

            private void down_btn_Click(object sender, EventArgs e)

            {

                int set_y = ScrollHard_panel.Location.Y + 10; //每次移动10

                if (set_y > moveArea) { set_y = moveArea; } //超范围

                ScrollHard_panel.Location = new Point(0, set_y); //滚动块的定位

                moveDistance = (int)(set_y / (float)moveArea * (treeView.Height - ScrollBar_panel.Height));

                TreeViewLocation();

            }

            //建立滚动控件

            private void Set_Scroll()

            {

                H_Height = B_Height * B_Height / GetNodesHeight();

                if (H_Height >= B_Height)

                {

                    ScrollHard_panel.Visible = false;

                    ScrollBar_panel.Visible = false;

                }

                else

                {

                    ScrollHard_panel.Visible = true;

                    ScrollBar_panel.Visible = true;

                    ScrollHard_panel.Size = new Size(ScrollBar_panel.Width, H_Height);

                }

                moveArea = ScrollBar_panel.Height - ScrollHard_panel.Height;

            }

            //获取treeview动态高度

            private int GetNodesHeight()

            {

                one_Nodes = 0; two_Nodes = 0; three_Nodes = 0; four_Nodes = 0;  //归0

                one_Nodes = this.treeView.Nodes.Count;

                for (int i = 0; i < this.treeView.Nodes.Count; i++)

                {

                    if (this.treeView.Nodes[i].IsExpanded)

                    {

                        two_Nodes += this.treeView.Nodes[i].Nodes.Count;

                        for (int j = 0; j < this.treeView.Nodes[i].Nodes.Count; j++)

                        {

                            if (this.treeView.Nodes[i].Nodes[j].IsExpanded)

                            {

                                three_Nodes += this.treeView.Nodes[i].Nodes[j].Nodes.Count;

                                for (int k = 0; k < this.treeView.Nodes[i].Nodes[j].Nodes.Count; k++)

                                {

                                    if (this.treeView.Nodes[i].Nodes[j].Nodes[k].IsExpanded)

                                    {

                                        four_Nodes += this.treeView.Nodes[i].Nodes[j].Nodes[k].Nodes.Count;

                                    }

                                }

                            }

                        }

                    }

                }

                this.treeView.Height = (one_Nodes + two_Nodes + three_Nodes + four_Nodes) * 20; 

                return this.treeView.Height;

            }

            //展开节点

            private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)

            {

                Set_Scroll();

            }

            //折叠节点

            private void treeView1_BeforeCollapse(object sender, TreeViewCancelEventArgs e)

            {

                Set_Scroll();

            }

            private void timer1_Tick(object sender, EventArgs e)

            {

                label1.Text = (treeView.Height - ScrollBar_panel.Height).ToString() + "===" + GetNodesHeight().ToString();

                label2.Text = moveDistance.ToString();

                label3.Text = one_Nodes + "===" + two_Nodes + "===" + three_Nodes + "===" + four_Nodes;

            }

        }

    支持个人观看使用,如商用或转载,请告知! -----萧朗(QQ:453929789 Email:xiaolang_xl@sina.com)
  • 相关阅读:
    Leetcode 290 Word Pattern
    Leetcode 205 Isomorphic Strings
    Leetcode 345 Reverse Vowels in a String
    Leetcode 151 Reverse Words in a String
    Leetcode 344 Reverse String
    Leetcode 383 Ransom Note
    leetcode 387 First Unique Character in a String
    反码补码和位运算
    SpringBoot进阶
    布隆过滤器
  • 原文地址:https://www.cnblogs.com/XiaoLang0/p/12470094.html
Copyright © 2020-2023  润新知