Option Explicit Private Sub Form_Load() TreeView1.Nodes.Add , , "aa", "aa" TreeView1.Nodes.Add , , "bb", "bb" ListView1.ListItems.Add , , "cc" ListView1.ListItems.Add , , "dd" ListView1.OLEDragMode = ccOLEDragAutomatic ListView1.LabelEdit = lvwManual End SubPrivate Sub ListView1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single) If Button = vbLeftButton Then ListView1.DragIcon = ListView1.SelectedItem.CreateDragImage ListView1.Drag vbBeginDrag End If End SubPrivate Sub TreeView1_DragDrop(Source As Control, x As Single, y As Single) If Not TreeView1.DropHighlight Is Nothing Then TreeView1.Nodes.Add TreeView1.DropHighlight.Key, tvwChild, GetNextKey() & ListView1.SelectedItem.Text, ListView1.SelectedItem.Text TreeView1.DropHighlight.Expanded = True End If End Sub Private Sub TreeView1_DragOver(Source As Control, x As Single, y As Single, State As Integer) Set TreeView1.DropHighlight = TreeView1.HitTest(x, y) End SubPrivate Function GetNextKey() As String On Error GoTo MyErr Dim sNewKey As String Dim iHold As Integer Dim i As Integer On Error GoTo MyErr iHold = Val(TreeView1.Nodes(1).Key) For i = 1 To TreeView1.Nodes.Count If Val(TreeView1.Nodes(i).Key) > iHold Then iHold = Val(TreeView1.Nodes(i).Key) End If Next iHold = iHold + 1 sNewKey = CStr(iHold) & "_" GetNextKey = sNewKey Exit Function MyErr: GetNextKey = "1_" End Function
这里面的换一下就可以: Private Sub ListView1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single) If Button = vbLeftButton Then ListView1.DragIcon = ListView1.SelectedItem.CreateDragImage ListView1.Drag vbBeginDrag End If End Sub
设想是在listview的mousedown选中某项,在treeview的dragdrop添加这项
将listview的dragmode设为自动,但拖动时无法看到选中某项,我想实现,在listview中,鼠标选中,则高亮显示,拖动这项,而不是整个listview拖动
请各位提提意见
Private Sub Form_Load()
TreeView1.Nodes.Add , , "aa", "aa"
TreeView1.Nodes.Add , , "bb", "bb"
ListView1.ListItems.Add , , "cc"
ListView1.ListItems.Add , , "dd"
ListView1.OLEDragMode = ccOLEDragAutomatic
ListView1.LabelEdit = lvwManual
End SubPrivate Sub ListView1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
If Button = vbLeftButton Then
ListView1.DragIcon = ListView1.SelectedItem.CreateDragImage
ListView1.Drag vbBeginDrag
End If
End SubPrivate Sub TreeView1_DragDrop(Source As Control, x As Single, y As Single)
If Not TreeView1.DropHighlight Is Nothing Then
TreeView1.Nodes.Add TreeView1.DropHighlight.Key, tvwChild, GetNextKey() & ListView1.SelectedItem.Text, ListView1.SelectedItem.Text
TreeView1.DropHighlight.Expanded = True
End If
End Sub
Private Sub TreeView1_DragOver(Source As Control, x As Single, y As Single, State As Integer)
Set TreeView1.DropHighlight = TreeView1.HitTest(x, y)
End SubPrivate Function GetNextKey() As String
On Error GoTo MyErr
Dim sNewKey As String
Dim iHold As Integer
Dim i As Integer
On Error GoTo MyErr
iHold = Val(TreeView1.Nodes(1).Key)
For i = 1 To TreeView1.Nodes.Count
If Val(TreeView1.Nodes(i).Key) > iHold Then
iHold = Val(TreeView1.Nodes(i).Key)
End If
Next
iHold = iHold + 1
sNewKey = CStr(iHold) & "_"
GetNextKey = sNewKey
Exit Function
MyErr:
GetNextKey = "1_"
End Function
基本功能实现,还想请教一个问题,就是拖动时整个listview一起拖动,可不可以不显示整个listview框,只显示拖动项?
Private Sub ListView1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
If Button = vbLeftButton Then
ListView1.DragIcon = ListView1.SelectedItem.CreateDragImage
ListView1.Drag vbBeginDrag
End If
End Sub