/// <summary> /// Begins a drag-and-drop operation, with an image attached. /// </summary> /// <param name="data">The data to drag.</param> /// <param name="allowedEffects">One of the DragDropEffects values.</param> /// <param name="dragImage">The image to attach to the cursor</param> /// <param name="cursorOffset">The offset within the image that the cursor attaches to.</param> /// <returns>A value from the DragDropEffects enumeration that represents the final effect that was performed during the drag-and-drop operation.</returns> public DragDropEffects DoDragDrop(object data, DragDropEffects allowedEffects, Bitmap dragImage, Point cursorOffset) { var adviseConnection = 0; try { DragDropHelper.SetFlags(1); // Create IDataObject m_dragData = new DragDropDataObject(); // attach image if we have one. if (dragImage != null) { DragDropHelper.InitializeFromBitmap(m_dragData, dragImage, cursorOffset); } // We need to listen for drop description changes. If a drop target // changes the drop description, we shouldn't provide a default one. var formatEtc = OleConverter.CreateFormat("DropDescription"); var hr = m_dragData.DAdvise(ref formatEtc, 0, new AdviseSink(m_dragData), out adviseConnection); if (hr != 0) { Marshal.ThrowExceptionForHR(hr); } // associate data with it. m_dragData.SetData("DragDropExtender", this); m_dragData.SetData(data); m_owner.GiveFeedback += OnGiveFeedback; m_owner.QueryContinueDrag += OnQueryContinueDrag; return(m_owner.DoDragDrop(m_dragData, allowedEffects)); } finally { m_owner.GiveFeedback -= OnGiveFeedback; m_owner.QueryContinueDrag -= OnQueryContinueDrag; if (m_dragData != null) { // Stop listening to drop description changes m_dragData.DUnadvise(adviseConnection); m_dragData.Dispose(); m_dragData = null; } } }
/// <summary> /// Begins a drag-and-drop operation, with an image attached. /// </summary> /// <param name="data">The data to drag.</param> /// <param name="allowedEffects">One of the DragDropEffects values.</param> /// <param name="dragImage">The image to attach to the cursor</param> /// <param name="cursorOffset">The offset within the image that the cursor attaches to.</param> /// <returns>A value from the DragDropEffects enumeration that represents the final effect that was performed during the drag-and-drop operation.</returns> public DragDropEffects DoDragDrop(object data, DragDropEffects allowedEffects, Bitmap dragImage, Point cursorOffset) { var adviseConnection = 0; try { DragDropHelper.SetFlags(1); // Create IDataObject m_dragData = new DragDropDataObject(); // attach image if we have one. if (dragImage != null) { DragDropHelper.InitializeFromBitmap(m_dragData, dragImage, cursorOffset); } // We need to listen for drop description changes. If a drop target // changes the drop description, we shouldn't provide a default one. var formatEtc = OleConverter.CreateFormat("DropDescription"); var hr = m_dragData.DAdvise(ref formatEtc, 0, new AdviseSink(m_dragData), out adviseConnection); if (hr != 0) { Marshal.ThrowExceptionForHR(hr); } // associate data with it. m_dragData.SetData("DragDropExtender", this); m_dragData.SetData(data); m_owner.GiveFeedback += OnGiveFeedback; m_owner.QueryContinueDrag += OnQueryContinueDrag; return m_owner.DoDragDrop(m_dragData, allowedEffects); } finally { m_owner.GiveFeedback -= OnGiveFeedback; m_owner.QueryContinueDrag -= OnQueryContinueDrag; if (m_dragData != null) { // Stop listening to drop description changes m_dragData.DUnadvise(adviseConnection); m_dragData.Dispose(); m_dragData = null; } } }