/// <summary> /// Sets the drop description for the drag image manager. /// </summary> /// <param name="dataObject">The DataObject to set.</param> /// <param name="dropDescription">The drop description.</param> public static void SetDropDescription(this IDataObject dataObject, DropDescription dropDescription) { FORMATETC formatETC; FillFormatETC(DropDescriptionFormat, TYMED.TYMED_HGLOBAL, out formatETC); // We need to set the drop description as an HGLOBAL. // Allocate space ... IntPtr pDD = Marshal.AllocHGlobal(Marshal.SizeOf(typeof (DropDescription))); try { // ... and marshal the data Marshal.StructureToPtr(dropDescription, pDD, false); // The medium wraps the HGLOBAL STGMEDIUM medium; medium.pUnkForRelease = null; medium.tymed = TYMED.TYMED_HGLOBAL; medium.unionmember = pDD; // Set the data IDataObject dataObjectCOM = dataObject; dataObjectCOM.SetData(ref formatETC, ref medium, true); } catch { // If we failed, we need to free the HGLOBAL memory Marshal.FreeHGlobal(pDD); throw; } }
/// <summary> /// Sets the drop description for the drag image manager. /// </summary> /// <param name="dataObject">The DataObject to set.</param> /// <param name="dropDescription">The drop description.</param> public static void SetDropDescription(this IDataObject dataObject, DropDescription dropDescription) { FORMATETC formatETC; FillFormatETC(DropDescriptionFormat, TYMED.TYMED_HGLOBAL, out formatETC); // We need to set the drop description as an HGLOBAL. // Allocate space ... IntPtr pDD = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(DropDescription))); try { // ... and marshal the data Marshal.StructureToPtr(dropDescription, pDD, false); // The medium wraps the HGLOBAL STGMEDIUM medium; medium.pUnkForRelease = null; medium.tymed = TYMED.TYMED_HGLOBAL; medium.unionmember = pDD; // Set the data IDataObject dataObjectCOM = dataObject; dataObjectCOM.SetData(ref formatETC, ref medium, true); } catch { // If we failed, we need to free the HGLOBAL memory Marshal.FreeHGlobal(pDD); throw; } }