Code
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Linq;
7
using System.Text;
8
using System.Windows.Forms;
9![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
namespace MyTree
11![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
12
public partial class Form2 : Form
13![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
14
// Node being dragged
15
private TreeNode dragNode = null;
16![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
// Temporary drop node for selection
18
private TreeNode tempDropNode = null;
19![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
// Timer for scrolling
21
private Timer timer = new Timer();
22![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
public Form2()
24![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
25
InitializeComponent();
26
27
//bind data to the treeView
28
BindTreeView mt = new BindTreeView();
29
mt.SetSqlconnstr("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\App_Data\\MemuTreeDB.mdf;Integrated Security=True;User Instance=True");
30
mt.GetDataSet("tbTree");
31
mt.BindTV(treeView1);
32![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
this.treeView1.ExpandAll();
34![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
timer.Interval = 200;
36
timer.Tick += new EventHandler(timer_Tick);
37
}
38![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
private void treeView_ItemDrag(object sender, System.Windows.Forms.ItemDragEventArgs e)
40![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
41
// Get drag node and select it
42
this.dragNode = (TreeNode)e.Item;
43
this.treeView1.SelectedNode = this.dragNode;
44![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**/
45
// Reset image list used for drag image
46
this.imageListDrag.Images.Clear();
47
this.imageListDrag.ImageSize = new Size(this.dragNode.Bounds.Size.Width + this.treeView1.Indent, this.dragNode.Bounds.Height);
48![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
// Create new bitmap
50
// This bitmap will contain the tree node image to be dragged
51
Bitmap bmp = new Bitmap(this.dragNode.Bounds.Width + this.treeView1.Indent, this.dragNode.Bounds.Height);
52![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
// Get graphics from bitmap
54
Graphics gfx = Graphics.FromImage(bmp);
55![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
56
// Draw node icon into the bitmap
57
gfx.DrawImage(this.imageListTreeView.Images[0], 0, 0);
58![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
59
// Draw node label into bitmap
60
gfx.DrawString(this.dragNode.Text,
61
this.treeView1.Font,
62
new SolidBrush(this.treeView1.ForeColor),
63
(float)this.treeView1.Indent, 1.0f);
64![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
65
// Add bitmap to imagelist
66
this.imageListDrag.Images.Add(bmp);
67![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
68
// Get mouse position in client coordinates
69
Point p = this.treeView1.PointToClient(Control.MousePosition);
70![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
71
// Compute delta between mouse position and node bounds
72
int dx = p.X + this.treeView1.Indent - this.dragNode.Bounds.Left;
73
int dy = p.Y - this.dragNode.Bounds.Top;
74![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
75
// Begin dragging image
76
if (DragHelper.ImageList_BeginDrag(this.imageListDrag.Handle, 0, dx, dy))
77![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
78
// Begin dragging
79
this.treeView1.DoDragDrop(bmp, DragDropEffects.Move);
80
// End dragging image
81
DragHelper.ImageList_EndDrag();
82
}
83![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
84
}
85![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
86
private void treeView1_DragOver(object sender, System.Windows.Forms.DragEventArgs e)
87![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
88
// Compute drag position and move image
89
Point formP = this.PointToClient(new Point(e.X, e.Y));
90
DragHelper.ImageList_DragMove(formP.X - this.treeView1.Left, formP.Y - this.treeView1.Top);
91![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
92
// Get actual drop node
93
TreeNode dropNode = this.treeView1.GetNodeAt(this.treeView1.PointToClient(new Point(e.X, e.Y)));
94
if (dropNode == null)
95![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
96
e.Effect = DragDropEffects.None;
97
return;
98
}
99![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
100
e.Effect = DragDropEffects.Move;
101![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
102
// if mouse is on a new node select it
103
if (this.tempDropNode != dropNode)
104![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
105
DragHelper.ImageList_DragShowNolock(false);
106
this.treeView1.SelectedNode = dropNode;
107
DragHelper.ImageList_DragShowNolock(true);
108
tempDropNode = dropNode;
109
}
110![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
111
// Avoid that drop node is child of drag node
112
TreeNode tmpNode = dropNode;
113
while (tmpNode.Parent != null)
114![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
115
if (tmpNode.Parent == this.dragNode) e.Effect = DragDropEffects.None;
116
tmpNode = tmpNode.Parent;
117
}
118
}
119![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
120
private void treeView1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
121![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
122
// Unlock updates
123
DragHelper.ImageList_DragLeave(this.treeView1.Handle);
124![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
125
// Get drop node
126
TreeNode dropNode = this.treeView1.GetNodeAt(this.treeView1.PointToClient(new Point(e.X, e.Y)));
127![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
128
// If drop node isn't equal to drag node, add drag node as child of drop node
129
if (this.dragNode != dropNode)
130![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
131
// Remove drag node from parent
132
if (this.dragNode.Parent == null)
133![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
134
this.treeView1.Nodes.Remove(this.dragNode);
135
}
136
else
137![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
138
this.dragNode.Parent.Nodes.Remove(this.dragNode);
139
}
140![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
141
// Add drag node to drop node
142
dropNode.Nodes.Add(this.dragNode);
143
dropNode.ExpandAll();
144![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
145
// Set drag node to null
146
this.dragNode = null;
147![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
148
// Disable scroll timer
149
this.timer.Enabled = false;
150
}
151
}
152![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
153
private void treeView1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
154![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
155
DragHelper.ImageList_DragEnter(this.treeView1.Handle, e.X - this.treeView1.Left,
156
e.Y - this.treeView1.Top);
157![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
158
// Enable timer for scrolling dragged item
159
this.timer.Enabled = true;
160
}
161![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
162
private void treeView1_DragLeave(object sender, System.EventArgs e)
163![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
164
DragHelper.ImageList_DragLeave(this.treeView1.Handle);
165![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
166
// Disable timer for scrolling dragged item
167
this.timer.Enabled = false;
168
}
169![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
170
private void treeView1_GiveFeedback(object sender, System.Windows.Forms.GiveFeedbackEventArgs e)
171![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
172
if (e.Effect == DragDropEffects.Move)
173![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
174
// Show pointer cursor while dragging
175
e.UseDefaultCursors = false;
176
this.treeView1.Cursor = Cursors.Default;
177
}
178
else e.UseDefaultCursors = true;
179![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
180
}
181![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
182
private void timer_Tick(object sender, EventArgs e)
183![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
184
// get node at mouse position
185
Point pt = PointToClient(Control.MousePosition);
186
TreeNode node = this.treeView1.GetNodeAt(pt);
187![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
188
if (node == null) return;
189![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
190
// if mouse is near to the top, scroll up
191
if (pt.Y < 30)
192![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
193
// set actual node to the upper one
194
if (node.PrevVisibleNode != null)
195![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
196
node = node.PrevVisibleNode;
197![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
198
// hide drag image
199
DragHelper.ImageList_DragShowNolock(false);
200
// scroll and refresh
201
node.EnsureVisible();
202
this.treeView1.Refresh();
203
// show drag image
204
DragHelper.ImageList_DragShowNolock(true);
205![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
206
}
207
}
208
// if mouse is near to the bottom, scroll down
209
else if (pt.Y > this.treeView1.Size.Height - 30)
210![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
211
if (node.NextVisibleNode != null)
212![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
213
node = node.NextVisibleNode;
214![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
215
DragHelper.ImageList_DragShowNolock(false);
216
node.EnsureVisible();
217
this.treeView1.Refresh();
218
DragHelper.ImageList_DragShowNolock(true);
219
}
220
}
221
}
222![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
223
private void Form2_Load(object sender, EventArgs e)
224![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
225![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
226
}
227
}
228
}
229
DragerHepler类:
Code
1
using System;
2
using System.Runtime.InteropServices;
3![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
namespace MyTree
5![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
6
public class DragHelper
7![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
8
[DllImport("comctl32.dll")]
9
public static extern bool InitCommonControls();
10![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
[DllImport("comctl32.dll", CharSet=CharSet.Auto)]
12
public static extern bool ImageList_BeginDrag(IntPtr himlTrack, int
13
iTrack, int dxHotspot, int dyHotspot);
14![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
[DllImport("comctl32.dll", CharSet=CharSet.Auto)]
16
public static extern bool ImageList_DragMove(int x, int y);
17![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
[DllImport("comctl32.dll", CharSet=CharSet.Auto)]
19
public static extern void ImageList_EndDrag();
20![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
[DllImport("comctl32.dll", CharSet=CharSet.Auto)]
22
public static extern bool ImageList_DragEnter(IntPtr hwndLock, int x, int y);
23![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
[DllImport("comctl32.dll", CharSet=CharSet.Auto)]
25
public static extern bool ImageList_DragLeave(IntPtr hwndLock);
26![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
[DllImport("comctl32.dll", CharSet=CharSet.Auto)]
28
public static extern bool ImageList_DragShowNolock(bool fShow);
29![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
static DragHelper()
31![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
32
InitCommonControls();
33
}
34
}
35
}
36
能力有限,希望高手们不吝赐教!^_^
转载于:https://www.cnblogs.com/Jackey_Chen/archive/2008/12/18/1357717.html