一个 TreeView 的派生类: TreeViewEx 实现 NodeShowToolTip、NodeDoubleClick 事件

//  [email protected] invent
// 一个 TreeView 的派生类: TreeViewEx 实现 NodeShowToolTip、NodeDoubleClick 事件
// 1.实现了 NodeShowToolTip 事件,结合键盘 Ctrl 键显示及设置 ToolTipText
// 2.实现了 NodeDoubleClick 事件,可在调用中只响应"叶子"节点
// 3.点击 TreeView 空白处不选中任何节点
// Class1.cs

namespace  Microshaoft
{
    
using System;
    
using System.Windows.Forms;

    
public class TreeViewEx : TreeView
    
{
        
//委托类型
        public delegate void TreeViewExEventHandler(object sender, TreeViewExEventArgs e);

        
//事件
        public event TreeViewExEventHandler NodeDoubleClick;
        
public event TreeViewExEventHandler NodeShowToolTip;

        
private ToolTip toolTip;

        
public TreeViewEx()
        
{
            toolTip 
= new ToolTip();
            
this.toolTip.InitialDelay = 300;
            
this.toolTip.ReshowDelay = 0;
        }


        
private void OnNodeDoubleClick(TreeNode xx)
        
{
            
if (this.NodeDoubleClick != null)
            
{
                
this.NodeDoubleClick(thisnew TreeViewExEventArgs(xx));
            }

        }


        
private void OnNodeShowToolTip(TreeNode xx, ToolTip yy)
        
{
            
if (this.NodeShowToolTip != null)
            
{
                
if ((xx != null&& (this.toolTip != null))
                
{
                    
this.NodeShowToolTip(thisnew TreeViewExEventArgs(xx, this.toolTip));
                }

            }

        }


        
protected override void OnDoubleClick(EventArgs e)
        
{
            
if (this.SelectedNode != null)
            
{
                
this.OnNodeDoubleClick(this.SelectedNode);
            }

            
base.OnDoubleClick(e);
        }


        
protected override void OnClick(EventArgs e)
        
{
            
if (this.GetNodeAt(TreeView.MousePosition.X, TreeView.MousePosition.Y) == null)
            
{
                
this.SelectedNode = null;
            }

            
base.OnClick(e);
        }


        
protected override void OnMouseDown(MouseEventArgs e)
        
{
            
if (this.GetNodeAt(e.X, e.Y) == null)
            
{
                
this.SelectedNode = null;
            }

            
base.OnMouseDown(e);
        }


        
//用于保存上次节点,以判断是否更新 ToolTiptext
        private TreeNode LastTreeNode;

        
protected override void OnMouseMove(MouseEventArgs e)
        
{
            
this.Cursor = Cursors.Default;
            TreeNode treeNode;
            treeNode 
= this.GetNodeAt(e.X, e.Y);
            
if (treeNode != null)
            
{
                
if ((Control.ModifierKeys & Keys.Control) != 0)
                
{
                    
this.Cursor = Cursors.Hand;
                    
if (LastTreeNode == null | treeNode != LastTreeNode)
                    
{
                        LastTreeNode 
= treeNode;
                        
//this.toolTip.Active = false;
                        this.OnNodeShowToolTip(treeNode, this.toolTip);
                        
this.toolTip.Active = true;
                        Console.WriteLine(treeNode.Text);
                    }

                }

                
else
                
{
                    
this.Cursor = Cursors.Default;
                    
this.toolTip.Active = false;
                }

            }

            
else
            
{
                
this.Cursor = Cursors.Default;
                
this.toolTip.Active = false;
            }

            
base.OnMouseMove(e);
        }

    }


    
public class TreeViewExEventArgs : EventArgs
    
{
        
private string m_ToolTipText;
        
private ToolTip m_NodeToolTip;
        
private TreeNode m_SelectedNode;
        
private TreeNode m_Node;

        
public TreeViewExEventArgs(TreeNode SelectedNode)
        
{
            
this.m_SelectedNode = SelectedNode;
        }


        
public TreeViewExEventArgs(TreeNode Node, ToolTip NodeToolTip)
        
{
            
this.m_NodeToolTip = NodeToolTip;
            
this.m_Node = Node;
        }


        
public TreeViewExEventArgs(TreeNode Node, string ToolTipText)
        
{
            
this.m_ToolTipText = ToolTipText;
        }


        
public TreeNode SelectedNode
        
{
            
get
            
{
                
return this.m_SelectedNode;
            }

        }


        
public string ToolTipText
        
{
            
get
            
{
                
return this.ToolTipText;
            }

            
set
            
{
                m_ToolTipText 
= value;
            }

        }


        
public TreeNode Node
        
{
            
get
            
{
                
return this.m_Node;
            }

        }


        
public ToolTip NodeToolTip
        
{
            
get
            
{
                
return this.m_NodeToolTip;
            }

        }

    }


    
public class TreeNodeEx : TreeNode
    
{
        
public int GetLevel()
        
{
            
int i = 0;
            TreeNode xx 
= this.Parent;
            
while ((xx = xx.Parent) != null)
            
{
                i
++;
            }

            
return i;
        }

    }

}


// using System;


// 测试====================

namespace  WindowsApplication1
{
    
using System;
    
using System.Drawing;
    
using System.ComponentModel;
    
using System.Windows.Forms;

    
using Microshaoft;

    
/// <summary>
    
/// Form1 的摘要说明。
    
/// </summary>

    public class Form1 : Form
    
{
        
private Button button1;

        
/// <summary>
        
/// 必需的设计器变量。
        
/// </summary>

        private Container components = null;

        
public Form1()
        
{
            
//
            
// Windows 窗体设计器支持所必需的
            
//
            InitializeComponent();

            
//
            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            
//
        }


        
/// <summary>
        
/// 清理所有正在使用的资源。
        
/// </summary>

        protected override void Dispose(bool disposing)
        
{
            
if (disposing)
            
{
                
if (components != null)
                
{
                    components.Dispose();
                }

            }

            
base.Dispose(disposing);
        }


        
#region Windows 窗体设计器生成的代码

        
/// <summary>
        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
        
/// 此方法的内容。
        
/// </summary>

        private void InitializeComponent()
        
{
            
this.button1 = new Button();
            
this.SuspendLayout();
            
// 
            
// button1
            
// 
            this.button1.Location = new Point(96112);
            
this.button1.Name = "button1";
            
this.button1.Size = new Size(8832);
            
this.button1.TabIndex = 0;
            
this.button1.Text = "button1";
            
this.button1.Click += new EventHandler(this.button1_Click);
            
// 
            
// Form1
            
// 
            this.AutoScaleBaseSize = new Size(614);
            
this.ClientSize = new Size(292273);
            
this.Controls.Add(this.button1);
            
this.Name = "Form1";
            
this.Text = "Form1";
            
this.Load += new EventHandler(this.Form1_Load);
            
this.ResumeLayout(false);

        }


        
#endregion


        
/// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>

        [STAThread]
        
private static void Main()
        
{
            Application.Run(
new Form1());
        }


        
//=================================================================
        
//调用示例
        private TreeViewEx treeViewEx1;

        
private void Form1_Load(object sender, EventArgs e)
        
{
            treeViewEx1 
= new TreeViewEx();
            
this.Controls.Add(treeViewEx1);
            treeViewEx1.HideSelection 
= false;
            treeViewEx1.NodeDoubleClick 
+= new TreeViewEx.TreeViewExEventHandler(treeViewEx1_NodeDoubleClick);
            treeViewEx1.NodeShowToolTip 
+= new TreeViewEx.TreeViewExEventHandler(this.treeViewEx1_NodeShowToolTip);
            treeViewEx1.Nodes.AddRange(
new TreeNode[]
                
{
                    
new TreeNode("Root",
                                 
new TreeNode[]
                                     
{
                                         
new TreeNode("a"), new TreeNode("b")
                                     }

                        ),
                    
new TreeNode("Root1",
                                 
new TreeNode[]
                                     
{
                                         
new TreeNode("a1"), new TreeNode("b1")
                                     }

                        )
                }

                );
        }


        
public void treeViewEx1_NodeShowToolTip(object sender, TreeViewExEventArgs e)
        
{
            e.NodeToolTip.SetToolTip(
this.treeViewEx1, "ToolTipText: [" + e.Node.Text + "]");
            
//e.NodeToolTip.SetToolTip (this.treeViewEx1,"ToolTipText: [" + e.Node.Text + "]" );
            
//Console.WriteLine(e.Node.Text);
        }


        
private void treeViewEx1_NodeDoubleClick(object sender, TreeViewExEventArgs e)
        
{
            
if (e.SelectedNode.GetNodeCount(true== 0)
                MessageBox.Show(e.SelectedNode.Text);
        }


        
private void button1_Click(object sender, EventArgs e)
        
{
        }

    }

}

你可能感兴趣的:(treeview)