1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Text;
7
using System.Windows.Forms;
8
using System.Collections;
9
10
namespace TreeDrag
11

{
12
public partial class Form1 : Form
13
{
14
private Point Position = new Point(0, 0);
15
public Form1()
16
{
17
InitializeComponent();
18
this.treeView1.ExpandAll();
19
}
20
21
private void Form1_Load(object sender, EventArgs e)
22
{
23
this.listView1.AllowDrop = true;
24
this.treeView1.AllowDrop = true;
25
ListViewItem listViewItem1 = new ListViewItem("Item01");
26
ListViewItem listViewItem2 = new ListViewItem("Item02");
27
listView1.Items.Add(listViewItem1);
28
listView1.Items.Add(listViewItem2);
29
}
30
31
private void listView1_ItemDrag(object sender, ItemDragEventArgs e)
32
{
33
//判断是否是鼠标右键按动
34
if (e.Button == MouseButtons.Right)
35
return;
36
int nTotalSelected = listView1.SelectedIndices.Count;
37
//判断组件中是否存在项目
38
if (nTotalSelected <= 0)
39
return;
40
IEnumerator selCol = listView1.SelectedItems.GetEnumerator();
41
selCol.MoveNext();
42
ListViewItem lvi = (ListViewItem)selCol.Current;
43
string mDir = "";
44
for (int i = 0; i < lvi.SubItems.Count; i++)
45
mDir += lvi.SubItems[i].Text + " ,";
46
string str = mDir.Substring(0, mDir.Length - 1);
47
if (str == "")
48
return;
49
//对组件中的字符串开始拖放操作
50
listView1.DoDragDrop(str, DragDropEffects.Copy | DragDropEffects.Move);
51
52
}
53
54
private void treeView1_DragEnter(object sender, DragEventArgs e)
55
{
56
if (e.Data.GetDataPresent("System.String"))
57
{
58
e.Effect = DragDropEffects.Copy;
59
}
60
else if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode"))
61
{
62
e.Effect = DragDropEffects.Move;
63
}
64
else
65
{
66
e.Effect = DragDropEffects.None;
67
}
68
69
}
70
71
private void treeView1_DragDrop(object sender, DragEventArgs e)
72
{
73
//获得拖放中的节点
74
TreeNode moveNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");
75
string s = (string)e.Data.GetData("System.String");
76
if (s != null)
77
{
78
s = s.Substring(s.IndexOf(":") + 1).Trim();
79
}
80
//根据鼠标坐标确定要移动到的目标节点
81
Point pt;
82
TreeNode targeNode;
83
pt = ((TreeView)(sender)).PointToClient(new Point(e.X,e.Y));
84
targeNode = treeView1.GetNodeAt(pt);
85
86
//如果目标节点无子节点则添加为同级节点,反之添加到下级节点的未端
87
if (targeNode.Nodes.Count == 0)
88
{
89
if (moveNode != null)
90
{
91
targeNode.Parent.Nodes.Insert(targeNode.Index, (TreeNode)moveNode.Clone());
92
}
93
else
94
{
95
TreeNode DragNode = new TreeNode(s);
96
targeNode.Nodes.Insert(targeNode.Index + 1, DragNode);
97
}
98
}
99
else
100
{
101
targeNode.Nodes.Insert(targeNode.Nodes .Count, (TreeNode)moveNode.Clone());
102
}
103
//展开目标节点,便于显示拖放效果
104
targeNode.Expand();
105
106
//移除拖放的节点
107
moveNode.Remove();
108
109
110
}
111
112
private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
113
{
114
if (e.Button == MouseButtons .Left )
115
{
116
DoDragDrop(e.Item, DragDropEffects.Move);
117
}
118
}
119
120
private void listView1_DragDrop(object sender, DragEventArgs e)
121
{
122
string dummy = "temp";
123
//获得进行"Drag"操作中拖动的字符串
124
string s = (string)e.Data.GetData(dummy.GetType());
125
s = s.Substring(s.IndexOf(":") + 1).Trim();
126
Position.X = e.X;
127
Position.Y = e.Y;
128
Position = listView1.PointToClient(Position);
129
//在目标组件中加入以此字符串为标题的项目
130
listView1.Items.Add(new ListViewItem(s, 0));
131
}
132
133
134
135
private void button6_Click(object sender, EventArgs e)
136
{
137
foreach (TreeNode node in this.treeView1.Nodes)
138
{
139
ClearNode(node);
140
FindNode(node, this.textBox1.Text);
141
}
142
}
143
public TreeNode FindNode(TreeNode tnParent, string strValue)
144
{
145
146
if (tnParent == null) return null;
147
148
for (int i = 0; i < tnParent.Text.Length - strValue.Length + 1; i++)
149
{
150
if (tnParent.Text.Substring(i, strValue.Length) == strValue && (!tnParent.Checked))
151
{
152
tnParent.BackColor = Color.Yellow;
153
}
154
}
155
TreeNode tnRet = null;
156
157
foreach (TreeNode tn in tnParent.Nodes)
158
{
159
160
tnRet = FindNode(tn, strValue);
161
162
if (tnRet != null)
163
tnRet.BackColor = Color.Yellow;
164
165
}
166
return tnRet;
167
168
169
}
170
171
public TreeNode ClearNode(TreeNode tnParent)
172
{
173
174
if (tnParent == null) return null;
175
tnParent.BackColor = Color.Empty;
176
TreeNode tnRet = null;
177
foreach (TreeNode tn in tnParent.Nodes)
178
{
179
tnRet = ClearNode(tn);
180
if (tnRet != null)
181
tnRet.BackColor = Color.Empty;
182
}
183
return tnRet;
184
}
185
}
186
}
187

2

3

4

5

6

7

8

9

10

11



12

13



14

15

16



17

18

19

20

21

22



23

24

25

26

27

28

29

30

31

32



33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55



56

57



58

59

60

61



62

63

64

65



66

67

68

69

70

71

72



73

74

75

76

77



78

79

80

81

82

83

84

85

86

87

88



89

90



91

92

93

94



95

96

97

98

99

100



101

102

103

104

105

106

107

108

109

110

111

112

113



114

115



116

117

118

119

120

121



122

123

124

125

126

127

128

129

130

131

132

133

134

135

136



137

138



139

140

141

142

143

144



145

146

147

148

149



150

151



152

153

154

155

156

157

158



159

160

161

162

163

164

165

166

167

168

169

170

171

172



173

174

175

176

177

178



179

180

181

182

183

184

185

186

187
