public DragDropEffects DoDragDrop(UIElement aSourceElement, IDataObject aDataObject, DragDropEffects aEffects, FrameworkElement aSourceVisual) { QueryContinueDragEventHandler queryContinue = null; DragDropEffects resultEffects = DragDropEffects.None; if (aDataObject != null && aSourceElement != null) { DragDrop.AddPreviewQueryContinueDragHandler(aSourceElement, queryContinue = new QueryContinueDragEventHandler(DragHelper_OnQueryContinueDrag)); IsDragging = true; CreateDragDropWindow(aSourceVisual); this.iDragdropWindow.Show(); try { resultEffects = DragDrop.DoDragDrop(aSourceElement, aDataObject, aEffects); } catch (Exception ex) { Linn.UserLog.WriteLine("Exception caught performing DragDrop : " + ex.ToString()); } DestroyDragDropWindow(); DragDrop.RemovePreviewQueryContinueDragHandler(aSourceElement, DragHelper_OnQueryContinueDrag); IsDragging = false; System.Windows.Input.Mouse.Capture(null); DestroyDragDropWindow(); } return(resultEffects); }
/// <summary> /// Initializes a new instance of the <see cref="DragAdorner{T}"/> class. /// </summary> /// <param name="adornedElement">The drag source.</param> /// <param name="child">The content to render.</param> protected DragAdorner(UIElement adornedElement, T child) : base(adornedElement) { this.elementOffset = adornedElement.PointToScreen(new Point(0, 0)) - User32.GetMousePosition(); var mp = User32.GetMousePosition(adornedElement) + this.elementOffset; this.Offset = new TranslateTransform(mp.X, mp.Y); child.RenderTransform = this.Offset; base.Child = child; DragDrop.AddPreviewQueryContinueDragHandler(adornedElement, this.UpdatePosition); }
private void StartDrag(MouseEventArgs args) { IDataObject data = null; UIElement dragelement = null; // ADD THE DATA if (this.Callback != null) { DragDataWrapper dw = new DragDataWrapper(); data = new DataObject(typeof(DragDataWrapper).ToString(), dw); if ((this.Callback.SupportedActions & DragDropProviderActions.MultiFormatData) != 0) { this.Callback.AppendData(ref data, args); } if ((this.Callback.SupportedActions & DragDropProviderActions.Data) != 0) { dw.Data = this.Callback.GetData(); } if ((this.Callback.SupportedActions & DragDropProviderActions.Visual) != 0) { dragelement = this.Callback.GetVisual(args); } else { dragelement = args.OriginalSource as UIElement; } dw.Source = this.DragSource; dw.Shim = this.Callback; Debug.Assert(this.DragScope == null, "The DragDataWrapper is meant for in-proc... Sorry for asserting, just wanted to confirm.. comment out assertion if needed"); } else { dragelement = args.OriginalSource as UIElement; data = new DataObject(typeof(UIElement).ToString(), dragelement); } if (dragelement == null || data == null || dragelement == this.DragSource) { return; } DragEventHandler dragOver = null; DragEventHandler dragLeave = null; QueryContinueDragEventHandler queryContinue = null; GiveFeedbackEventHandler giveFeedback = null; DragDropEffects effects = this.GetDragDropEffects(); DragDropEffects resultEffects; // Inprocess Drag ... if (this.DragScope != null) { bool previousAllowDrop = this.DragScope.AllowDrop; this.adorner = new DragAdorner(this.DragScope, (UIElement)dragelement, true, this.Opacity); this.layer = AdornerLayer.GetAdornerLayer(this.DragScope as Visual); this.layer.Add(this.adorner); if (this.DragScope != this.DragSource) { this.DragScope.AllowDrop = true; DragDrop.AddPreviewDragOverHandler((DependencyObject)this.DragScope, dragOver = new DragEventHandler(DragScope_DragOver)); DragDrop.AddPreviewDragLeaveHandler(this.DragScope, dragLeave = new DragEventHandler(DragScope_DragLeave)); DragDrop.AddPreviewQueryContinueDragHandler(this.DragSource, queryContinue = new QueryContinueDragEventHandler(OnQueryContinueDrag)); } try { this.IsDragging = true; this.mouseLeftScope = false; resultEffects = DragDrop.DoDragDrop(this.DragSource, data, effects); this.DragFinished(resultEffects); } catch { Debug.Assert(false); } if (this.DragScope != this.DragSource) { this.DragScope.AllowDrop = previousAllowDrop; DragDrop.RemovePreviewDragOverHandler(this.DragScope, dragOver); DragDrop.RemovePreviewDragLeaveHandler(this.DragScope, dragLeave); DragDrop.RemovePreviewQueryContinueDragHandler(this.DragSource, queryContinue); } } else { DragDrop.AddPreviewQueryContinueDragHandler(this.DragSource, queryContinue = new QueryContinueDragEventHandler(OnQueryContinueDrag)); DragDrop.AddGiveFeedbackHandler(this.DragSource, giveFeedback = new GiveFeedbackEventHandler(OnGiveFeedback)); this.IsDragging = true; if ((this.Callback.SupportedActions & DragDropProviderActions.Visual) != 0) { this.CreateDragDropWindow(dragelement); this.dragdropWindow.Show(); } try { resultEffects = DragDrop.DoDragDrop(this.DragSource, data, effects); } finally { } if ((this.Callback.SupportedActions & DragDropProviderActions.Visual) != 0) { this.DestroyDragDropWindow(); } DragDrop.RemovePreviewQueryContinueDragHandler(this.DragSource, OnQueryContinueDrag); DragDrop.AddGiveFeedbackHandler(this.DragSource, OnGiveFeedback); this.IsDragging = false; this.DragFinished(resultEffects); } }
void StartDrag(MouseEventArgs args) { IDataObject data = null; UIElement dragelement = null; // ADD THE DATA if (_callback != null) { DragDataWrapper dw = new DragDataWrapper(); data = new DataObject(typeof(DragDataWrapper).ToString(), dw); if ((_callback.SupportedActions & DragDropProviderActions.MultiFormatData) != 0) { _callback.AppendData(ref data, args); } if ((_callback.SupportedActions & DragDropProviderActions.Data) != 0) { dw.Data = _callback.GetData(); } if ((_callback.SupportedActions & DragDropProviderActions.Visual) != 0) { dragelement = _callback.GetVisual(args); } else { dragelement = args.OriginalSource as UIElement; } dw.Source = _dragSource; dw.Shim = _callback; // System.Diagnostics.Debug.Assert(DragScope == null, "The DragDataWrapper is meant for in-proc... Sorry for asserting, just wanted to confirm.. comment out assertion if needed"); } else { dragelement = args.OriginalSource as UIElement; data = new DataObject(typeof(UIElement).ToString(), dragelement); } if (dragelement == null || data == null || dragelement == this._dragSource) { return; } #if DEBUG // Add it as Text ... good for testing purposes if (!data.GetDataPresent(DataFormats.Text)) { data.SetData(DataFormats.Text, "abcd"); } #endif DragEventHandler dragOver = null; DragEventHandler dragLeave = null; QueryContinueDragEventHandler queryContinue = null; GiveFeedbackEventHandler giveFeedback = null; DragDropEffects effects = GetDragDropEffects(); DragDropEffects resultEffects; // Inprocess Drag ... if (DragScope != null) { bool previousAllowDrop = DragScope.AllowDrop; _adorner = new DragAdorner(DragScope, (UIElement)dragelement, true, Opacity); _layer = AdornerLayer.GetAdornerLayer(DragScope as Visual); _layer.Add(_adorner); if (DragScope != _dragSource) { DragScope.AllowDrop = true; DragDrop.AddPreviewDragOverHandler((DependencyObject)DragScope, dragOver = new DragEventHandler(DragScope_DragOver)); DragDrop.AddPreviewDragLeaveHandler(DragScope, dragLeave = new DragEventHandler(DragScope_DragLeave)); DragDrop.AddPreviewQueryContinueDragHandler(_dragSource, queryContinue = new QueryContinueDragEventHandler(onQueryContinueDrag)); } try { IsDragging = true; this._mouseLeftScope = false; resultEffects = DragDrop.DoDragDrop(_dragSource, data, effects); DragFinished(resultEffects); } catch { System.Diagnostics.Debug.Assert(false); } if (DragScope != _dragSource) { DragScope.AllowDrop = previousAllowDrop; DragDrop.RemovePreviewDragOverHandler(DragScope, dragOver); DragDrop.RemovePreviewDragLeaveHandler(DragScope, dragLeave); DragDrop.RemovePreviewQueryContinueDragHandler(_dragSource, queryContinue); } } else { DragDrop.AddPreviewQueryContinueDragHandler(_dragSource, queryContinue = new QueryContinueDragEventHandler(onQueryContinueDrag)); DragDrop.AddGiveFeedbackHandler(_dragSource, giveFeedback = new GiveFeedbackEventHandler(onGiveFeedback)); IsDragging = true; CreateDragDropWindow(dragelement); this._dragdropWindow.Show(); try { resultEffects = DragDrop.DoDragDrop(_dragSource, data, effects); } finally { } DestroyDragDropWindow(); DragDrop.RemovePreviewQueryContinueDragHandler(_dragSource, onQueryContinueDrag); DragDrop.AddGiveFeedbackHandler(_dragSource, onGiveFeedback); IsDragging = false; DragFinished(resultEffects); } }