PageRenderTime 124ms CodeModel.GetById 114ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/1.0/EasySL.Share.SL/DragHelper.cs

#
C# | 138 lines | 107 code | 31 blank | 0 comment | 10 complexity | cf6924e14fad6677328a9f8d54ef87cd MD5 | raw file
  1using System;
  2using System.Windows;
  3using System.Windows.Media;
  4using System.Windows.Controls.Primitives;
  5using System.Windows.Controls;
  6using System.Windows.Input;
  7
  8namespace EasySL.Share
  9{
 10    public static class DragHelper
 11    {
 12        public static void Drag(this FrameworkElement attachedElement)
 13        {
 14            bool isDragging = false;
 15            Point lastPosition = new Point(0, 0);
 16
 17            attachedElement.MouseLeftButtonDown += (s, e) =>
 18            {
 19                isDragging = true;
 20                lastPosition = e.GetPosition(null);
 21                attachedElement.CaptureMouse();
 22            };
 23
 24            attachedElement.MouseLeftButtonUp += (s, e) =>
 25            {
 26                isDragging = false;
 27                attachedElement.ReleaseMouseCapture();
 28            };
 29
 30            attachedElement.MouseMove += (s, e) =>
 31            {
 32                if (!isDragging)
 33                    return;
 34
 35                Point currentPosition = e.GetPosition(null);
 36
 37                double dX = currentPosition.X - lastPosition.X;
 38                double dY = currentPosition.Y - lastPosition.Y;
 39
 40                lastPosition = currentPosition;
 41
 42                Transform oldTransform = attachedElement.RenderTransform;
 43                TransformGroup rt = new TransformGroup();
 44                TranslateTransform newPos = new TranslateTransform();
 45                newPos.X = dX;
 46                newPos.Y = dY;
 47
 48                if (oldTransform != null)
 49                {
 50                    rt.Children.Add(oldTransform);
 51                }
 52                rt.Children.Add(newPos);
 53
 54                MatrixTransform mt = new MatrixTransform();
 55                mt.Matrix = rt.Value;
 56
 57                attachedElement.RenderTransform = mt;
 58
 59            };
 60        }
 61
 62        public static void Drag(this FrameworkElement attachedElement, object dragObject)
 63        {
 64            attachedElement.Drag(dragObject, null, null);
 65        }
 66
 67        public static void Drag(this FrameworkElement attachedElement, object dragObject, MouseEventHandler onMove, MouseButtonEventHandler onDragCompleted)
 68        {
 69            Popup rootPopup = new Popup();
 70            rootPopup.IsOpen = false;
 71
 72            ContentControl dragElement = new ContentControl();
 73            dragElement.Content = dragObject;
 74
 75            rootPopup.Child = dragElement;
 76
 77            bool isDragging = false;
 78            Point lastPosition = new Point(0, 0);
 79
 80            attachedElement.MouseLeftButtonDown += (s, e) =>
 81            {
 82                isDragging = true;
 83                lastPosition = e.GetPosition(null);
 84                dragElement.CaptureMouse();
 85
 86                Point p = e.GetPosition(attachedElement);
 87                rootPopup.IsOpen = true;
 88                rootPopup.HorizontalOffset = lastPosition.X - p.X;
 89                rootPopup.VerticalOffset = lastPosition.Y - p.Y;
 90            };
 91
 92            dragElement.MouseLeftButtonUp += (s, e) =>
 93            {
 94                isDragging = false;
 95                dragElement.ReleaseMouseCapture();
 96
 97                rootPopup.IsOpen = false;
 98                dragElement.RenderTransform = null;
 99
100                if (onDragCompleted != null)
101                    onDragCompleted(s, e);
102            };
103
104            dragElement.MouseMove += (s, e) =>
105            {
106                if (!isDragging)
107                    return;
108
109                Point currentPosition = e.GetPosition(null);
110
111                double dX = currentPosition.X - lastPosition.X;
112                double dY = currentPosition.Y - lastPosition.Y;
113
114                lastPosition = currentPosition;
115
116                Transform oldTransform = dragElement.RenderTransform;
117                TransformGroup rt = new TransformGroup();
118                TranslateTransform newPos = new TranslateTransform();
119                newPos.X = dX;
120                newPos.Y = dY;
121
122                if (oldTransform != null)
123                {
124                    rt.Children.Add(oldTransform);
125                }
126                rt.Children.Add(newPos);
127
128                MatrixTransform mt = new MatrixTransform();
129                mt.Matrix = rt.Value;
130
131                dragElement.RenderTransform = mt;
132
133                if (onMove != null)
134                    onMove(s, e);
135            };
136        }
137    }
138}