This repository has been archived by the owner on Sep 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DragDropHelper.cs
90 lines (76 loc) · 3.32 KB
/
DragDropHelper.cs
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
using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Ribbon.ViewInfo;
namespace WindowsFormsApplication19
{
public class DragDropHelper
{
RibbonHitInfo dragItemHitInfo = null;
GalleryControl sourceGallery, targetGallery;
public DragDropHelper(GalleryControl sourceGallery, GalleryControl targetGallery)
{
this.sourceGallery = sourceGallery;
this.targetGallery = targetGallery;
}
public void EnableDragDrop()
{
sourceGallery.MouseDown += OnSourceGalleryMouseDown;
sourceGallery.MouseMove += OnSourceGalleryMouseMove;
targetGallery.AllowDrop = true;
targetGallery.DragDrop += OnTargetGalleryDragDrop;
targetGallery.DragOver += OnTargetGalleryDragOver;
}
private void OnSourceGalleryMouseDown(object sender, MouseEventArgs e)
{
GalleryControl galControl = (GalleryControl)sender;
RibbonHitInfo hitInfo = galControl.CalcHitInfo(e.Location);
if (hitInfo.InGalleryItem)
{
dragItemHitInfo = hitInfo;
return;
}
dragItemHitInfo = null;
}
private void OnSourceGalleryMouseMove(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left || Control.ModifierKeys != Keys.None || dragItemHitInfo == null)
return;
Size dragSize = SystemInformation.DragSize;
Rectangle dragRect = new Rectangle(dragItemHitInfo.HitPoint.X - dragSize.Width / 2, dragItemHitInfo.HitPoint.Y - dragSize.Height / 2, dragSize.Width, dragSize.Height);
if (!(dragRect.Contains(e.Location)))
sourceGallery.DoDragDrop(dragItemHitInfo.GalleryItem, DragDropEffects.Copy);
}
private void OnTargetGalleryDragOver(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(GalleryItem)))
e.Effect = DragDropEffects.Copy;
}
private void OnTargetGalleryDragDrop(object sender, DragEventArgs e)
{
if (!(e.Data.GetDataPresent(typeof(GalleryItem))))
return;
GalleryItem draggedItem = e.Data.GetData(typeof(GalleryItem)) as GalleryItem;
GalleryControl galControl = (GalleryControl)sender;
RibbonHitInfo hitInfo = galControl.CalcHitInfo(galControl.PointToClient(new Point(e.X, e.Y)));
if (hitInfo.InGalleryGroup)
{
hitInfo.GalleryItemGroup.Items.Add((GalleryItem)draggedItem.Clone());
}
else
{
int groupIndex = galControl.Gallery.Groups.Add(new GalleryItemGroup());
galControl.Gallery.Groups[groupIndex].Caption = draggedItem.GalleryGroup.Caption;
galControl.Gallery.Groups[groupIndex].Items.Add((GalleryItem)draggedItem.Clone());
}
}
public void DisableDragDrop()
{
sourceGallery.MouseDown -= OnSourceGalleryMouseDown;
sourceGallery.MouseMove -= OnSourceGalleryMouseMove;
targetGallery.AllowDrop = false;
targetGallery.DragDrop -= OnTargetGalleryDragDrop;
targetGallery.DragOver -= OnTargetGalleryDragOver;
}
}
}