zoukankan
html css js c++ java
一个 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;
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(
this
,
new
TreeViewExEventArgs(xx));
}
}
private
void
OnNodeShowToolTip(TreeNode xx, ToolTip yy)
{
if
(
this
.NodeShowToolTip
!=
null
)
{
if
((xx
!=
null
)
&&
(
this
.toolTip
!=
null
))
{
this
.NodeShowToolTip(
this
,
new
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);
}
Windows 窗体设计器生成的代码
#region
Windows 窗体设计器生成的代码
/**/
///
<summary>
///
设计器支持所需的方法 - 不要使用代码编辑器修改
///
此方法的内容。
///
</summary>
private
void
InitializeComponent()
{
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
);
}
#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)
{
}
}
}
查看全文
相关阅读:
LINUX 内核守护进程
LINUX 内核 API
LINUX IO 图解
doxygen
xtrace
Dapper-translation 分布式监控系统
矩表
最流行的5个前端框架对比
2017年前端框架、类库、工具大比拼
Top 10 JavaScript编辑器,你在用哪个?
原文地址:https://www.cnblogs.com/Microshaoft/p/148479.html
最新文章
获取Executor提交的并发执行的任务返回结果的两种方式/ExecutorCompletionService使用
showplan_text查询计划查询 sql执行顺序 时间 IO
CGI-- FASTCGI
Nginx FastCGI的运行原理
flashsim配置2015最新版本
NANDFlashSim
go实现 raft Paxos 算法
go标准库DOC与 raft
gnu--libc
SQL Server on Linux
热门文章
kaleidoscope-llvm
jack-compiler
LLVM
build-your-own-lisp
ANSI C grammar (Lex) ANSI C grammar (Yacc)
RednaxelaFX:软件工程师、主攻高级编程语言虚拟机的设计与实现
深入浅出编译原理
java之jvm学习笔记十三(jvm基本结构) 通俗易懂的JVM 文件,没有之一
stanford CS DB 课程 数据库系统实现
LINUX 内核文档 DOC
Copyright © 2011-2022 走看看