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
| ' Declare global variables.
Dim indrag As Boolean ' Flag that signals a Drag Drop operation.
Dim nodX As Object ' Item that is being dragged.
Private Sub Form_Load()
' Load a bitmap into an Imagelist control.
Dim imgX As ListImage
Dim BitmapPath As String
BitmapPath = "icons\mail\mail01a.ico" ' Change to a valid path.
Set imgX = imagelist1.ListImages.Add(, , LoadPicture(BitmapPath))
' Initialize TreeView control and create several nodes.
TreeView1.ImageList = imagelist1
Dim nodX As Node ' Create a tree.
Set nodX = TreeView1.Nodes.Add(, , , "Parent1", 1)
Set nodX = TreeView1.Nodes.Add(, , , "Parent2", 1)
Set nodX = TreeView1.Nodes.Add(1, tvwChild, , "Child 1", 1)
Set nodX = TreeView1.Nodes.Add(1, tvwChild, , "Child 2", 1)
Set nodX = TreeView1.Nodes.Add(2, tvwChild, , "Child 3", 1)
Set nodX = TreeView1.Nodes.Add(2, tvwChild, , "Child 4", 1)
Set nodX = TreeView1.Nodes.Add(3, tvwChild, , "Child 5", 1)
nodX.EnsureVisible ' Expand tree to show all nodes.
End Sub
Private Sub TreeView1_MouseDown_
(Button As Integer, Shift As Integer, x As Single, y As Single)
Set nodX = TreeView1.SelectedItem ' Set the item being dragged.
End Sub
Private Sub TreeView1_MouseMove _
(Button As Integer, Shift As Integer, x As Single, y As Single)
If Button = vbLeftButton Then ' Signal a Drag operation.
indrag = True ' Set the flag to true.
' Set the drag icon with the CreateDragImage method.
TreeView1.DragIcon = TreeView1.SelectedItem.CreateDragImage
TreeView1.Drag vbBeginDrag ' Drag operation.
End If
End Sub
Private Sub TreeView1_DragDrop_
(Source As Control, x As Single, y As Single)
If TreeView1.DropHighlight Is Nothing Then
Set TreeView1.DropHighlight = Nothing
indrag = False
Exit Sub
Else
If nodX = TreeView1.DropHighlight Then Exit Sub
Cls
Print nodX.Text & " dropped on " & TreeView1.DropHighlight.Text
Set TreeView1.DropHighlight = Nothing
indrag = False
End If
End Sub
Private Sub TreeView1_DragOver(Source As Control, x As Single, y As Single, State As Integer)
If indrag = True Then
' Set DropHighlight to the mouse's coordinates.
Set TreeView1.DropHighlight = TreeView1.HitTest(x, y)
End If
End Sub |
Partager