-
Notifications
You must be signed in to change notification settings - Fork 0
/
DragDropHelper.vb
126 lines (115 loc) · 5.6 KB
/
DragDropHelper.vb
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
Imports Microsoft.VisualBasic
Imports System
Imports System.Linq
Imports System.Windows.Forms
Imports DevExpress.XtraBars.Ribbon
Imports System.Collections.Generic
Imports DevExpress.XtraBars.Ribbon.ViewInfo
Imports System.Drawing
Imports System.Diagnostics
Namespace DragDropExample
Public Class DragDropHelper
Private galleryControl1, galleryControl2, DragSource As GalleryControl
Private _SelectedPen As New Pen(Color.Indigo, 3)
Private targetHighlightItem As GalleryItem
Private DragItemHitInfo As RibbonHitInfo
Public Sub New(ByVal galleryControl1 As GalleryControl, ByVal galleryControl2 As GalleryControl)
Me.galleryControl1 = galleryControl1
Me.galleryControl2 = galleryControl2
End Sub
Public Property SelectedPen() As Pen
Get
Return _SelectedPen
End Get
Set(ByVal value As Pen)
_SelectedPen = value
End Set
End Property
Public Sub EnableDragDrop()
galleryControl2.AllowDrop = True
galleryControl1.AllowDrop = galleryControl2.AllowDrop
AddHandler galleryControl1.MouseDown, AddressOf OnGalleryControlMouseDown
AddHandler galleryControl2.MouseDown, AddressOf OnGalleryControlMouseDown
AddHandler galleryControl1.MouseMove, AddressOf OnGalleryControlMouseMove
AddHandler galleryControl2.MouseMove, AddressOf OnGalleryControlMouseMove
AddHandler galleryControl1.DragOver, AddressOf OnGalleryControlDragOver
AddHandler galleryControl2.DragOver, AddressOf OnGalleryControlDragOver
AddHandler galleryControl1.DragDrop, AddressOf OnGalleryControlDragDrop
AddHandler galleryControl2.DragDrop, AddressOf OnGalleryControlDragDrop
AddHandler galleryControl1.Gallery.CustomDrawItemText, AddressOf OnCustomDrawItemText
AddHandler galleryControl2.Gallery.CustomDrawItemText, AddressOf OnCustomDrawItemText
End Sub
Private Sub OnGalleryControlMouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
Dim gallery As GalleryControl = CType(sender, GalleryControl)
Dim hitInfo As RibbonHitInfo = gallery.CalcHitInfo(e.Location)
If hitInfo.InGalleryItem Then
DragItemHitInfo = hitInfo
DragSource = gallery
Else
DragItemHitInfo = Nothing
End If
End Sub
Private Sub OnGalleryControlMouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
If e.Button <> MouseButtons.Left OrElse Control.ModifierKeys <> Keys.None OrElse DragItemHitInfo Is Nothing Then
Return
End If
Dim gallery As GalleryControl = CType(sender, GalleryControl)
Dim checkedItems As List(Of GalleryItem) = gallery.Gallery.GetCheckedItems()
If checkedItems.Count = 0 Then
checkedItems.Add(DragItemHitInfo.GalleryItem)
End If
If (Not New Rectangle(DragItemHitInfo.HitPoint.X - SystemInformation.DragSize.Width / 2, DragItemHitInfo.HitPoint.Y - SystemInformation.DragSize.Height / 2, SystemInformation.DragSize.Width, SystemInformation.DragSize.Height).Contains(e.Location)) Then
gallery.DoDragDrop(checkedItems, DragDropEffects.All)
End If
End Sub
Private Sub OnGalleryControlDragOver(ByVal sender As Object, ByVal e As DragEventArgs)
If e.Data.GetDataPresent(GetType(List(Of GalleryItem))) Then
e.Effect = DragDropEffects.Move
End If
Dim gallery As GalleryControl = CType(sender, GalleryControl)
Dim hitInfo As RibbonHitInfo = gallery.CalcHitInfo(gallery.PointToClient(New Point(e.X, e.Y)))
targetHighlightItem = hitInfo.GalleryItem
gallery.Invalidate()
End Sub
Private Sub OnGalleryControlDragDrop(ByVal sender As Object, ByVal e As DragEventArgs)
If (Not e.Data.GetDataPresent(GetType(List(Of GalleryItem)))) OrElse DragSource Is Nothing Then
Return
End If
Dim dragTarget As GalleryControl = CType(sender, GalleryControl)
Dim hitInfo As RibbonHitInfo = dragTarget.CalcHitInfo(dragTarget.PointToClient(New Point(e.X, e.Y)))
Dim targetItem As GalleryItem = hitInfo.GalleryItem
If targetItem IsNot Nothing Then
Dim target As GalleryItemCollection = targetItem.GalleryGroup.Items
Dim index As Integer = target.IndexOf(targetItem)
Dim draggedItems As List(Of GalleryItem) = CType(e.Data.GetData(GetType(List(Of GalleryItem))), List(Of GalleryItem))
For Each item As GalleryItem In draggedItems
Dim source As GalleryItemCollection = item.GalleryGroup.Items
source.Remove(item)
target.Insert(index, item)
index += 1
Next item
End If
targetHighlightItem = Nothing
End Sub
Private Sub OnCustomDrawItemText(ByVal sender As Object, ByVal e As GalleryItemCustomDrawEventArgs)
If e.Item.Equals(targetHighlightItem) Then
Dim newPoint As New Point(e.Bounds.X, e.Bounds.Bottom)
e.Cache.Graphics.DrawLine(SelectedPen, newPoint, New Point(e.Bounds.Right, e.Bounds.Bottom))
End If
End Sub
Public Sub DisableDragDrop()
galleryControl2.AllowDrop = True
galleryControl1.AllowDrop = galleryControl2.AllowDrop
RemoveHandler galleryControl1.MouseDown, AddressOf OnGalleryControlMouseDown
RemoveHandler galleryControl2.MouseDown, AddressOf OnGalleryControlMouseDown
RemoveHandler galleryControl1.MouseMove, AddressOf OnGalleryControlMouseMove
RemoveHandler galleryControl2.MouseMove, AddressOf OnGalleryControlMouseMove
RemoveHandler galleryControl1.DragOver, AddressOf OnGalleryControlDragOver
RemoveHandler galleryControl2.DragOver, AddressOf OnGalleryControlDragOver
RemoveHandler galleryControl1.DragDrop, AddressOf OnGalleryControlDragDrop
RemoveHandler galleryControl2.DragDrop, AddressOf OnGalleryControlDragDrop
RemoveHandler galleryControl1.Gallery.CustomDrawItemText, AddressOf OnCustomDrawItemText
RemoveHandler galleryControl2.Gallery.CustomDrawItemText, AddressOf OnCustomDrawItemText
End Sub
End Class
End Namespace