//
playyuer@Microshaoft.com invent
//
一个 TreeView 的派生类: TreeViewEx 实现 NodeShowToolTip、NodeDoubleClick 事件
//
1.实现了 NodeShowToolTip 事件,结合键盘 Ctrl 键显示及设置 ToolTipText
//
2.实现了 NodeDoubleClick 事件,可在调用中只响应"叶子"节点
//
3.点击 TreeView 空白处不选中任何节点
//
Class1.cs
namespace
Microshaoft
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
using System;
using System.Windows.Forms;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
public class TreeViewEx : TreeView
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//委托类型
public delegate void TreeViewExEventHandler(object sender, TreeViewExEventArgs e);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
//事件
public event TreeViewExEventHandler NodeDoubleClick;
public event TreeViewExEventHandler NodeShowToolTip;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
private ToolTip toolTip;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
public TreeViewEx()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
toolTip = new ToolTip();
this.toolTip.InitialDelay = 300;
this.toolTip.ReshowDelay = 0;
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
private void OnNodeDoubleClick(TreeNode xx)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this.NodeDoubleClick != null)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.NodeDoubleClick(this, new TreeViewExEventArgs(xx));
}
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
private void OnNodeShowToolTip(TreeNode xx, ToolTip yy)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this.NodeShowToolTip != null)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if ((xx != null) && (this.toolTip != null))
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.NodeShowToolTip(this, new TreeViewExEventArgs(xx, this.toolTip));
}
}
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
protected override void OnDoubleClick(EventArgs e)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this.SelectedNode != null)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.OnNodeDoubleClick(this.SelectedNode);
}
base.OnDoubleClick(e);
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
protected override void OnClick(EventArgs e)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this.GetNodeAt(TreeView.MousePosition.X, TreeView.MousePosition.Y) == null)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.SelectedNode = null;
}
base.OnClick(e);
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
protected override void OnMouseDown(MouseEventArgs e)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this.GetNodeAt(e.X, e.Y) == null)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.SelectedNode = null;
}
base.OnMouseDown(e);
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
//用于保存上次节点,以判断是否更新 ToolTiptext
private TreeNode LastTreeNode;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
protected override void OnMouseMove(MouseEventArgs e)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Cursor = Cursors.Default;
TreeNode treeNode;
treeNode = this.GetNodeAt(e.X, e.Y);
if (treeNode != null)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if ((Control.ModifierKeys & Keys.Control) != 0)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Cursor = Cursors.Hand;
if (LastTreeNode == null | treeNode != LastTreeNode)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
LastTreeNode = treeNode;
//this.toolTip.Active = false;
this.OnNodeShowToolTip(treeNode, this.toolTip);
this.toolTip.Active = true;
Console.WriteLine(treeNode.Text);
}
}
else
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Cursor = Cursors.Default;
this.toolTip.Active = false;
}
}
else
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Cursor = Cursors.Default;
this.toolTip.Active = false;
}
base.OnMouseMove(e);
}
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
public class TreeViewExEventArgs : EventArgs
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private string m_ToolTipText;
private ToolTip m_NodeToolTip;
private TreeNode m_SelectedNode;
private TreeNode m_Node;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
public TreeViewExEventArgs(TreeNode SelectedNode)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.m_SelectedNode = SelectedNode;
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
public TreeViewExEventArgs(TreeNode Node, ToolTip NodeToolTip)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.m_NodeToolTip = NodeToolTip;
this.m_Node = Node;
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
public TreeViewExEventArgs(TreeNode Node, string ToolTipText)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.m_ToolTipText = ToolTipText;
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
public TreeNode SelectedNode
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.m_SelectedNode;
}
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
public string ToolTipText
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.ToolTipText;
}
set
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
m_ToolTipText = value;
}
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
public TreeNode Node
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.m_Node;
}
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
public ToolTip NodeToolTip
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.m_NodeToolTip;
}
}
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
public class TreeNodeEx : TreeNode
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public int GetLevel()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int i = 0;
TreeNode xx = this.Parent;
while ((xx = xx.Parent) != null)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
i++;
}
return i;
}
}
}
![None.gif](/Images/OutliningIndicators/None.gif)
//
using System;
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
//
测试====================
namespace
WindowsApplication1
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
using Microshaoft;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : Form
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private Button button1;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private Container components = null;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
public Form1()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose(bool disposing)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (disposing)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (components != null)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.button1 = new Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new Point(96, 112);
this.button1.Name = "button1";
this.button1.Size = new Size(88, 32);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new Size(6, 14);
this.ClientSize = new Size(292, 273);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new EventHandler(this.Form1_Load);
this.ResumeLayout(false);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
#endregion
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
private static void Main()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Application.Run(new Form1());
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
//=================================================================
//调用示例
private TreeViewEx treeViewEx1;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
private void Form1_Load(object sender, EventArgs e)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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[]
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
new TreeNode("Root",
new TreeNode[]
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
new TreeNode("a"), new TreeNode("b")
}
),
new TreeNode("Root1",
new TreeNode[]
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
new TreeNode("a1"), new TreeNode("b1")
}
)
}
);
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
public void treeViewEx1_NodeShowToolTip(object sender, TreeViewExEventArgs e)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
e.NodeToolTip.SetToolTip(this.treeViewEx1, "ToolTipText: [" + e.Node.Text + "]");
//e.NodeToolTip.SetToolTip (this.treeViewEx1,"ToolTipText: [" + e.Node.Text + "]" );
//Console.WriteLine(e.Node.Text);
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
private void treeViewEx1_NodeDoubleClick(object sender, TreeViewExEventArgs e)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (e.SelectedNode.GetNodeCount(true) == 0)
MessageBox.Show(e.SelectedNode.Text);
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
private void button1_Click(object sender, EventArgs e)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
}
}
转载于:https://www.cnblogs.com/Microshaoft/archive/2005/05/01/148479.html