ListView,TreeView实现拖动排序

直接上代码~

1
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
Option Explicit
Dim m_dragItem As ListItem
Dim m_dragNode As Node
'----------------------------------------------------------------
Private Sub Form_Load()
Dim i As Integer
'建列表
LvwPerson.ColumnHeaders.Clear
LvwPerson.ListItems.Clear
LvwPerson.ColumnHeaders.add , , "aaaa"
LvwPerson.ColumnHeaders.add , , "bbbb"
LvwPerson.ColumnHeaders.add , , "cccc"
For i = 1 To 100
LvwPerson.ListItems.add , , "aaaaaaaaaaaaaa" & CStr(i)
LvwPerson.ListItems.Item(i).SubItems(1) = "aaaa"
LvwPerson.ListItems.Item(i).SubItems(2) = "aaaa"
Next i
'建树
TreeView1.Nodes.add , , "Root", "root"
For i = 1 To 100
TreeView1.Nodes.add "Root", tvwChild, "key=" & CStr(i), "key==" & CStr(i)
Next
End Sub
'-----------------------------------------------------------------------------
Private Sub LvwPerson_DragOver(Source As Control, x As Single, y As Single, State As Integer)
Dim li As ListItem
Set li = LvwPerson.HitTest(x, y)
If Not li Is Nothing Then
li.EnsureVisible
LvwPerson.DropHighlight = li
End If
End Sub
Private Sub LvwPerson_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
If Button = 1 Then
Set m_dragItem = LvwPerson.HitTest(x, y)
End If
End Sub
Private Sub LvwPerson_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
If Button = 1 Then
If Not m_dragItem Is Nothing Then
LvwPerson.DragIcon = m_dragItem.CreateDragImage
LvwPerson.Drag vbBeginDrag
End If
End If
End Sub
Private Sub lvwperson_DragDrop(Source As Control, x As Single, y As Single)
Dim li As ListItem
Dim addli As ListItem
Dim i As Integer
Set li = LvwPerson.HitTest(x, y)
If (Not li Is Nothing) And (Not m_dragItem Is Nothing) Then
If li.index <> m_dragItem.index Then
If li.index > m_dragItem.index Then
LvwPerson.ListItems.Remove m_dragItem.index
Set addli = LvwPerson.ListItems.add(li.index + 1, m_dragItem.Key, m_dragItem.Text)
Else
LvwPerson.ListItems.Remove m_dragItem.index
Set addli = LvwPerson.ListItems.add(li.index, m_dragItem.Key, m_dragItem.Text)
End If
For i = 1 To m_dragItem.ListSubItems.Count
addli.SubItems(i) = m_dragItem.ListSubItems(i).Text
Next i
End If
End If

LvwPerson.DropHighlight = Nothing
Set m_dragItem = Nothing
End Sub
''''''-------------------------------------------------------------------------
Private Sub TreeView1_DragDrop(Source As Control, x As Single, y As Single)
Dim nd As Node
Dim addnd As Node
Set nd = TreeView1.HitTest(x, y)
If (Not nd Is Nothing) And (Not m_dragNode Is Nothing) Then
If nd.index <> m_dragNode.index Then
TreeView1.Nodes.Remove m_dragNode.Key
TreeView1.Nodes.add nd, tvwNext, m_dragNode.Key, m_dragNode.Text
End If
End If
TreeView1.DropHighlight = Nothing
Set m_dragNode = Nothing
End Sub
Private Sub TreeView1_DragOver(Source As Control, x As Single, y As Single, State As Integer)
Dim nd As Node
Dim ndPre As Node
Set nd = TreeView1.HitTest(x, y)
If Not nd Is Nothing Then
nd.EnsureVisible
Set ndPre = nd.Previous
If Not ndPre Is Nothing Then
ndPre.EnsureVisible
End If
Set TreeView1.DropHighlight = nd
Text1.Text = CStr(nd.Key)
End If
End Sub
Private Sub TreeView1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
If Button = 1 Then
Set m_dragNode = TreeView1.HitTest(x, y)
End If
End Sub
Private Sub TreeView1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
If Button = 1 Then
If Not m_dragNode Is Nothing Then
TreeView1.DragIcon = m_dragNode.CreateDragImage
TreeView1.Drag vbBeginDrag
End If
End If
End Sub