Beispiel #1
0
        public virtual void ProcessMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            if (e.Handled)
            {
                return;
            }

#if DEBUG
            this.CheckMouseEventArgs(e);
#endif

            if (!this.DraggedElement.CaptureMouse())
            {
                Debug.Fail("The DragSourceManager was unable to capture the mouse on the dragged element.");
                return;
            }

            //Get the current window (which can be a popup) on which the drag is happening
            var parent = this.GetDraggedElementWindow();

            this.InitialMousePositionToDragContainerAdorner = e.GetPosition(this.AdornerLayerInsideDragContainer);
            this.InitialMousePositionToDraggedElement       = e.GetPosition(this.DraggedElement);
            this.MouseToScreenPositionFactor = DragSourceManager.CalculateMouseToScreenPositionFactor(parent);
            this.ParentWindowIsPopup         = (parent is Popup);

            var draggedElementMousePosition = e.GetPosition(this.DraggedElement);
            m_draggedElementMouseOffset = new Vector(draggedElementMousePosition.X, draggedElementMousePosition.Y);
        }
Beispiel #2
0
        private static Vector CalculateMouseToScreenPositionFactor(FrameworkElement element)
        {
            if (element == null)
            {
                return(new Vector(1d, 1d));
            }

            var source = PresentationSource.FromVisual(element);

            if (source == null)
            {
                var parent = DragSourceManager.GetPopupParent(element);
                while (parent != null)
                {
                    source = PresentationSource.FromDependencyObject(parent);
                    if (source != null)
                    {
                        break;
                    }

                    parent = DragSourceManager.GetPopupParent(parent);
                }
            }

            double x, y;

            if (source != null)
            {
                var deviceUnits = source.CompositionTarget.TransformToDevice;
                x = deviceUnits.M11;
                y = deviceUnits.M22;
            }
            else
            {
                using (var hwnd = new HwndSource(new HwndSourceParameters()))
                {
                    var deviceUnits = hwnd.CompositionTarget.TransformToDevice;
                    x = deviceUnits.M11;
                    y = deviceUnits.M22;
                }
            }

            return(new Vector((x == 0d) ? 1d : 1d / x, (y == 0d) ? 1d : 1d / y));
        }