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

// playyuer@Microshaoft.com invent
//一个 TreeView 的派生类: TreeViewEx 实现 NodeShowToolTip、NodeDoubleClick 事件
//1.实现了 NodeShowToolTip 事件,结合键盘 Ctrl 键显示及设置 ToolTipText
//2.实现了 NodeDoubleClick 事件,可在调用中只响应"叶子"节点
//3.点击 TreeView 空白处不选中任何节点
//Class1.cs
namespace Microshaoft
{
 using System;
 public class TreeViewEx : System.Windows.Forms.TreeView
 {
  public delegate void TreeViewExEventHandler(object sender,TreeViewExEventArgs e);
  public event TreeViewExEventHandler NodeDoubleClick;
  public event TreeViewExEventHandler NodeShowToolTip;

  private void OnNodeDoubleClick(System.Windows.Forms.TreeNode xx)
  {
   if(this.NodeDoubleClick != null)
   {
    this.NodeDoubleClick(this,new TreeViewExEventArgs(xx));
   }
  }
  private void OnNodeShowToolTip(System.Windows.Forms.TreeNode xx, System.Windows.Forms.ToolTip yy)
  {
   if(this.NodeShowToolTip != null)
   {
    if ((xx != null) && (this.toolTip !=null))
    {
     this.NodeShowToolTip(this,new TreeViewExEventArgs(xx,this.toolTip));
    }
   }
  }
  protected override void OnDoubleClick(System.EventArgs e)
  {
   if (this.SelectedNode != null)
   {
    this.OnNodeDoubleClick(this.SelectedNode);
   }
   base.OnDoubleClick(e);
  }
  private System.Windows.Forms.ToolTip toolTip;
  public TreeViewEx()
  {
   toolTip = new System.Windows.Forms.ToolTip();
   this.toolTip.InitialDelay = 300;
   this.toolTip.ReshowDelay = 0;
  }
  protected override void OnClick (System.EventArgs e)
  {
   if (this.GetNodeAt(System.Windows.Forms.TreeView.MousePosition.X,System.Windows.Forms.TreeView.MousePosition.Y) == null)
   {
    this.SelectedNode = null;
   }
   base.OnClick(e);
  }
  protected override void OnMouseDown (System.Windows.Forms.MouseEventArgs e)
  {
   if (this.GetNodeAt(e.X,e.Y) == null)
    this.SelectedNode = null;
   base.OnMouseDown(e);

  }
  private System.Windows.Forms.TreeNode LastTreeNode;
  protected override void OnMouseMove (System.Windows.Forms.MouseEventArgs e)
  {
   this.Cursor = System.Windows.Forms.Cursors.Default ;
   System.Windows.Forms.TreeNode treeNode;
   treeNode = this.GetNodeAt(e.X,e.Y) ;
   if(treeNode != null)
   {
    if( (System.Windows.Forms.Control.ModifierKeys & System.Windows.Forms.Keys.Control) != 0)
    {
     this.Cursor = System.Windows.Forms.Cursors.Hand ;
     if(treeNode != LastTreeNode | LastTreeNode ==null )
     {
      LastTreeNode = treeNode;
      this.toolTip.Active = false;
      this.OnNodeShowToolTip(treeNode,this.toolTip);
      this.toolTip.Active = true;
     }
    }
    else
    {
     this.Cursor = System.Windows.Forms.Cursors.Default ;
     this.toolTip.Active = false;
    }
   }
   else
   {
    this.Cursor = System.Windows.Forms.Cursors.Default ;
    this.toolTip.Active = false;
   }
   base.OnMouseMove(e);
  }
 }


 public class TreeViewExEventArgs : System.EventArgs
 {
  public TreeViewExEventArgs(System.Windows.Forms.TreeNode SelectedNode)
  {
   this.m_SelectedNode = SelectedNode;
  }
  public TreeViewExEventArgs(System.Windows.Forms.TreeNode Node,System.Windows.Forms.ToolTip NodeToolTip)
  {
   this.m_NodeToolTip = NodeToolTip;
   this.m_Node = Node;
  }
  public TreeViewExEventArgs(System.Windows.Forms.TreeNode Node,string ToolTipText)
  {
   this.m_ToolTipText = ToolTipText;
  }
  private string m_ToolTipText;
  private System.Windows.Forms.ToolTip m_NodeToolTip;
  private System.Windows.Forms.TreeNode m_SelectedNode;
  private System.Windows.Forms.TreeNode m_Node;
  public System.Windows.Forms.TreeNode SelectedNode
  {
   get
   {
    return this.m_SelectedNode;
   }
  }
  public string ToolTipText
  {
   get
   {
    return this.ToolTipText;
   }
   set{m_ToolTipText = value;}
  }
  public System.Windows.Forms.TreeNode Node
  {
   get
   {
    return this.m_Node;
   }
  }
  public System.Windows.Forms.ToolTip NodeToolTip
  {
   get
   {
    return this.m_NodeToolTip;
   }
  }
 }
 public class TreeNodeEx : System.Windows.Forms.TreeNode
 {
  public int GetLevel()
  {
   int i = 0;
   System.Windows.Forms.TreeNode xx =this.Parent;
   while ((xx = xx.Parent) != null) i++;
   return i;
  }
 }
}

//using System;


//测试====================
namespace WindowsApplication1
{

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microshaoft;
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.Button button1;
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.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 System.Windows.Forms.Button();
   this.SuspendLayout();
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point(96, 112);
   this.button1.Name = "button1";
   this.button1.Size = new System.Drawing.Size(88, 32);
   this.button1.TabIndex = 0;
   this.button1.Text = "button1";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(292, 273);
   this.Controls.Add(this.button1);
   this.Name = "Form1";
   this.Text = "Form1";
   this.Load += new System.EventHandler(this.Form1_Load);
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

//=================================================================
//调用示例
 private TreeViewEx treeViewEx1;
 private void Form1_Load(object sender, System.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 System.Windows.Forms.TreeNode[]{
                    new System.Windows.Forms.TreeNode("Root",
                    new System.Windows.Forms.TreeNode[]
                   {
                     new System.Windows.Forms.TreeNode("a"),new System.Windows.Forms.TreeNode("b")}
                    ),
                    new System.Windows.Forms.TreeNode("Root1",
                    new System.Windows.Forms.TreeNode[]
                   {
                     new System.Windows.Forms.TreeNode("a1"),new System.Windows.Forms.TreeNode("b1")}
                    )
                   }
   );
 }

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

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值