Exemple #1
0
        public static bool TryGetData <T>(this IDataObject dataObject, string format, out T data)
            where T : struct
        {
            var formatEtc = OleConverter.CreateFormat(format);

            formatEtc.tymed = TYMED.TYMED_HGLOBAL;

            if (0 == dataObject.QueryGetData(ref formatEtc))
            {
                STGMEDIUM medium;
                dataObject.GetData(ref formatEtc, out medium);
                try
                {
                    data = (T)Marshal.PtrToStructure(medium.unionmember, typeof(T));
                    return(true);
                }
                finally
                {
                    ReleaseStgMedium(ref medium);
                }
            }

            data = default(T);
            return(false);
        }
Exemple #2
0
        public static void SetData <T>(this IDataObject dataObject, string format, T data)
            where T : struct
        {
            var formatEtc = OleConverter.CreateFormat(format);

            formatEtc.tymed = TYMED.TYMED_HGLOBAL;

            // We need to set the drop description as an HGLOBAL.
            // Allocate space ...
            var unmanagedPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(T)));

            try
            {
                // ... and marshal the data
                Marshal.StructureToPtr(data, unmanagedPtr, false);

                // The medium wraps the HGLOBAL
                STGMEDIUM medium;
                medium.pUnkForRelease = null;
                medium.tymed          = TYMED.TYMED_HGLOBAL;
                medium.unionmember    = unmanagedPtr;

                // Set the data
                dataObject.SetData(ref formatEtc, ref medium, true);
            }
            catch
            {
                // If we failed, we need to free the HGLOBAL memory
                Marshal.FreeHGlobal(unmanagedPtr);
                throw;
            }
        }
Exemple #3
0
        public void SetData(string format, Type type, bool autoConvert, object data)
        {
            var tymed = GetCompatibleFormat(format, type);

            if (tymed != TYMED.TYMED_NULL)
            {
                var formatEtc = OleConverter.CreateFormat(format);
                formatEtc.tymed = tymed;

                // Set data on an empty DataObject instance
                var conv = new DataObject();
                conv.SetData(format, true, data);

                // Now retrieve the data, using the COM interface.
                // This will perform a managed to unmanaged conversion for us.
                STGMEDIUM medium;
                ((IComDataObject)conv).GetData(ref formatEtc, out medium);
                try
                {
                    // Now set the data on our data object
                    SetData(ref formatEtc, ref medium, true);
                }
                catch
                {
                    // On exceptions, release the medium
                    ReleaseStgMedium(ref medium);
                    throw;
                }
            }
            else
            {
                m_managedStorage[format] = data;
            }
        }
Exemple #4
0
        /// <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;
                }
            }
        }
Exemple #5
0
        public bool GetDataPresent(string format, bool autoConvert)
        {
            if (m_managedStorage.ContainsKey(format))
            {
                return(true);
            }

            var formatEtc = OleConverter.CreateFormat(format);

            if (QueryGetData(ref formatEtc) == 0)
            {
                return(true);
            }

            return(false);
        }
Exemple #6
0
        public object GetData(string format, bool autoConvert)
        {
            object obj;

            if (m_managedStorage.TryGetValue(format, out obj))
            {
                return(obj);
            }

            var formatEtc = OleConverter.CreateFormat(format);

            if (QueryGetData(ref formatEtc) == 0)
            {
                STGMEDIUM medium;
                GetData(ref formatEtc, out medium);
                return(OleConverter.Convert(format, ref medium));
            }

            return(null);
        }