看看这段代码有没有用:Option ExplicitDim m_DragItem As ListItem
Dim m_DragX As Long
Dim m_DragY As LongPrivate Sub Form_Load()
With ListView1
.ListItems.Add , , "list-aa"
.ListItems.Add , , "list-bb"
End With
With TreeView1
.Nodes.Add , , "aa", "tree-aa"
.Nodes.Add , , "bb", "tree-bb"
End With
End SubPrivate Sub ResetDragDrop()
Set m_DragItem = Nothing
Set TreeView1.DropHighlight = Nothing
Set ListView1.DropHighlight = Nothing
End SubPrivate Sub ListView1_DragDrop(Source As Control, x As Single, y As Single)
ResetDragDrop
End Sub
Private Sub ListView1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
If Button = 1 Then
Set m_DragItem = ListView1.HitTest(x, y)
m_DragX = x
m_DragY = y
End If
End SubPrivate Sub ListView1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
If Not (m_DragItem Is Nothing) Then
With ListView1
Dim item As ListItem
Set item = .HitTest(x, y)
If item Is Nothing Then
.Drag
ElseIf item <> m_DragItem Then
.Drag
ElseIf item = m_DragItem Then
'*** 保证拖动一段距离后才发生拖放,否则很容易将点击item的动作也当成拖放
If (Abs(x - m_DragX) > Me.ScaleX(5, Me.ScaleMode, vbPixels)) Or (Abs(y - m_DragY) > Me.ScaleY(5, Me.ScaleMode, vbPixels)) Then
.Drag
End If
End If
End With
End If
End SubPrivate Sub ListView1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
ResetDragDrop
End SubPrivate Sub TreeView1_DragDrop(Source As Control, x As Single, y As Single)
If Source Is ListView1 Then
Dim node As node
With TreeView1
Set node = .HitTest(x, y)
If node Is Nothing Then
MsgBox "请将Item拖放到某个节点上!", vbExclamation, Me.Caption
Else
.Nodes.Add node.Key, tvwChild, , m_DragItem.Text
End If
ResetDragDrop
End With
End If
End SubPrivate Sub TreeView1_DragOver(Source As Control, x As Single, y As Single, State As Integer)
If Source Is ListView1 Then
With TreeView1
If State = 1 Then
'*** 鼠标离开treeview
Set .DropHighlight = Nothing
Else
Set .DropHighlight = .HitTest(x, y)
End If
End With
End If
End Sub
Dim m_DragX As Long
Dim m_DragY As LongPrivate Sub Form_Load()
With ListView1
.ListItems.Add , , "list-aa"
.ListItems.Add , , "list-bb"
End With
With TreeView1
.Nodes.Add , , "aa", "tree-aa"
.Nodes.Add , , "bb", "tree-bb"
End With
End SubPrivate Sub ResetDragDrop()
Set m_DragItem = Nothing
Set TreeView1.DropHighlight = Nothing
Set ListView1.DropHighlight = Nothing
End SubPrivate Sub ListView1_DragDrop(Source As Control, x As Single, y As Single)
ResetDragDrop
End Sub
Private Sub ListView1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
If Button = 1 Then
Set m_DragItem = ListView1.HitTest(x, y)
m_DragX = x
m_DragY = y
End If
End SubPrivate Sub ListView1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
If Not (m_DragItem Is Nothing) Then
With ListView1
Dim item As ListItem
Set item = .HitTest(x, y)
If item Is Nothing Then
.Drag
ElseIf item <> m_DragItem Then
.Drag
ElseIf item = m_DragItem Then
'*** 保证拖动一段距离后才发生拖放,否则很容易将点击item的动作也当成拖放
If (Abs(x - m_DragX) > Me.ScaleX(5, Me.ScaleMode, vbPixels)) Or (Abs(y - m_DragY) > Me.ScaleY(5, Me.ScaleMode, vbPixels)) Then
.Drag
End If
End If
End With
End If
End SubPrivate Sub ListView1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
ResetDragDrop
End SubPrivate Sub TreeView1_DragDrop(Source As Control, x As Single, y As Single)
If Source Is ListView1 Then
Dim node As node
With TreeView1
Set node = .HitTest(x, y)
If node Is Nothing Then
MsgBox "请将Item拖放到某个节点上!", vbExclamation, Me.Caption
Else
.Nodes.Add node.Key, tvwChild, , m_DragItem.Text
End If
ResetDragDrop
End With
End If
End SubPrivate Sub TreeView1_DragOver(Source As Control, x As Single, y As Single, State As Integer)
If Source Is ListView1 Then
With TreeView1
If State = 1 Then
'*** 鼠标离开treeview
Set .DropHighlight = Nothing
Else
Set .DropHighlight = .HitTest(x, y)
End If
End With
End If
End Sub
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货