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

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

None.gif
namespace  Microshaoft
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
using System;
InBlock.gif    
using System.Windows.Forms;
InBlock.gif
InBlock.gif    
public class TreeViewEx : TreeView
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//委托类型
InBlock.gif
        public delegate void TreeViewExEventHandler(object sender, TreeViewExEventArgs e);
InBlock.gif
InBlock.gif        
//事件
InBlock.gif
        public event TreeViewExEventHandler NodeDoubleClick;
InBlock.gif        
public event TreeViewExEventHandler NodeShowToolTip;
InBlock.gif
InBlock.gif        
private ToolTip toolTip;
InBlock.gif
InBlock.gif        
public TreeViewEx()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            toolTip 
= new ToolTip();
InBlock.gif            
this.toolTip.InitialDelay = 300;
InBlock.gif            
this.toolTip.ReshowDelay = 0;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void OnNodeDoubleClick(TreeNode xx)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (this.NodeDoubleClick != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.NodeDoubleClick(thisnew TreeViewExEventArgs(xx));
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

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

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void OnDoubleClick(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (this.SelectedNode != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.OnNodeDoubleClick(this.SelectedNode);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.OnDoubleClick(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void OnClick(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (this.GetNodeAt(TreeView.MousePosition.X, TreeView.MousePosition.Y) == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.SelectedNode = null;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.OnClick(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void OnMouseDown(MouseEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (this.GetNodeAt(e.X, e.Y) == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.SelectedNode = null;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.OnMouseDown(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//用于保存上次节点,以判断是否更新 ToolTiptext
InBlock.gif
        private TreeNode LastTreeNode;
InBlock.gif
InBlock.gif        
protected override void OnMouseMove(MouseEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.Cursor = Cursors.Default;
InBlock.gif            TreeNode treeNode;
InBlock.gif            treeNode 
= this.GetNodeAt(e.X, e.Y);
InBlock.gif            
if (treeNode != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if ((Control.ModifierKeys & Keys.Control) != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
this.Cursor = Cursors.Hand;
InBlock.gif                    
if (LastTreeNode == null | treeNode != LastTreeNode)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        LastTreeNode 
= treeNode;
InBlock.gif                        
//this.toolTip.Active = false;
InBlock.gif
                        this.OnNodeShowToolTip(treeNode, this.toolTip);
InBlock.gif                        
this.toolTip.Active = true;
InBlock.gif                        Console.WriteLine(treeNode.Text);
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
this.Cursor = Cursors.Default;
InBlock.gif                    
this.toolTip.Active = false;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.Cursor = Cursors.Default;
InBlock.gif                
this.toolTip.Active = false;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.OnMouseMove(e);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public class TreeViewExEventArgs : EventArgs
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private string m_ToolTipText;
InBlock.gif        
private ToolTip m_NodeToolTip;
InBlock.gif        
private TreeNode m_SelectedNode;
InBlock.gif        
private TreeNode m_Node;
InBlock.gif
InBlock.gif        
public TreeViewExEventArgs(TreeNode SelectedNode)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.m_SelectedNode = SelectedNode;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public TreeViewExEventArgs(TreeNode Node, ToolTip NodeToolTip)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.m_NodeToolTip = NodeToolTip;
InBlock.gif            
this.m_Node = Node;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public TreeViewExEventArgs(TreeNode Node, string ToolTipText)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.m_ToolTipText = ToolTipText;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public TreeNode SelectedNode
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.m_SelectedNode;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string ToolTipText
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.ToolTipText;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                m_ToolTipText 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public TreeNode Node
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.m_Node;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public ToolTip NodeToolTip
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.m_NodeToolTip;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public class TreeNodeEx : TreeNode
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public int GetLevel()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int i = 0;
InBlock.gif            TreeNode xx 
= this.Parent;
InBlock.gif            
while ((xx = xx.Parent) != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                i
++;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return i;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
// using System;
None.gif
None.gif
None.gif
// 测试====================
None.gif

None.gif
namespace  WindowsApplication1
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
using System;
InBlock.gif    
using System.Drawing;
InBlock.gif    
using System.ComponentModel;
InBlock.gif    
using System.Windows.Forms;
InBlock.gif
InBlock.gif    
using Microshaoft;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Form1 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class Form1 : Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private Button button1;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 必需的设计器变量。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private Container components = null;
InBlock.gif
InBlock.gif        
public Form1()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// Windows 窗体设计器支持所必需的
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif
InBlock.gif            
//
InBlock.gif            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 清理所有正在使用的资源。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        protected override void Dispose(bool disposing)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (disposing)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (components != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    components.Dispose();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.Dispose(disposing);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

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

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 应用程序的主入口点。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [STAThread]
InBlock.gif        
private static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Application.Run(
new Form1());
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//=================================================================
InBlock.gif        
//调用示例
InBlock.gif
        private TreeViewEx treeViewEx1;
InBlock.gif
InBlock.gif        
private void Form1_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            treeViewEx1 
= new TreeViewEx();
InBlock.gif            
this.Controls.Add(treeViewEx1);
InBlock.gif            treeViewEx1.HideSelection 
= false;
InBlock.gif            treeViewEx1.NodeDoubleClick 
+= new TreeViewEx.TreeViewExEventHandler(treeViewEx1_NodeDoubleClick);
InBlock.gif            treeViewEx1.NodeShowToolTip 
+= new TreeViewEx.TreeViewExEventHandler(this.treeViewEx1_NodeShowToolTip);
InBlock.gif            treeViewEx1.Nodes.AddRange(
new TreeNode[]
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
new TreeNode("Root",
InBlock.gif                                 
new TreeNode[]
ExpandedSubBlockStart.gifContractedSubBlock.gif                                     
dot.gif{
InBlock.gif                                         
new TreeNode("a"), new TreeNode("b")
ExpandedSubBlockEnd.gif                                     }

InBlock.gif                        ),
InBlock.gif                    
new TreeNode("Root1",
InBlock.gif                                 
new TreeNode[]
ExpandedSubBlockStart.gifContractedSubBlock.gif                                     
dot.gif{
InBlock.gif                                         
new TreeNode("a1"), new TreeNode("b1")
ExpandedSubBlockEnd.gif                                     }

InBlock.gif                        )
ExpandedSubBlockEnd.gif                }

InBlock.gif                );
ExpandedSubBlockEnd.gif        }

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

InBlock.gif
InBlock.gif        
private void treeViewEx1_NodeDoubleClick(object sender, TreeViewExEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (e.SelectedNode.GetNodeCount(true== 0)
InBlock.gif                MessageBox.Show(e.SelectedNode.Text);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void button1_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/Microshaoft/archive/2005/05/01/148479.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值