From af7996b401055958ace027c47b4a628f0ec1f857 Mon Sep 17 00:00:00 2001 From: dahall Date: Thu, 3 Dec 2020 19:28:41 -0700 Subject: [PATCH] Added ShellDataObject constructor taking a list of ShellItems which adds them via CFSTR_SHELLIDLIST --- Windows.Shell/ShellDataObject.cs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Windows.Shell/ShellDataObject.cs b/Windows.Shell/ShellDataObject.cs index d61caa30..9d99feaf 100644 --- a/Windows.Shell/ShellDataObject.cs +++ b/Windows.Shell/ShellDataObject.cs @@ -42,6 +42,29 @@ namespace Vanara.Windows.Shell { } + /// Initializes a new instance of the class. + /// A list of ShellItem instances. + public ShellDataObject(ShellItem[] items) : base(ShellClipboardFormat.CFSTR_SHELLIDLIST, GetPIDLArrayStream(items)) + { + } + + private static Stream GetPIDLArrayStream(ShellItem[] items) + { + var str = new MemoryStream(); + var pidlBytes = Array.ConvertAll(items, i => i.PIDL.GetBytes()); + int offset = 0; + str.Write(BitConverter.GetBytes((uint)pidlBytes.Length), offset, sizeof(uint)); + offset += sizeof(uint); + for (var i = 0; i < pidlBytes.Length; i++, offset += sizeof(uint)) + str.Write(BitConverter.GetBytes((uint)pidlBytes[i].Length), offset, sizeof(uint)); + for (var i = 0; i < pidlBytes.Length; i++) + { + str.Write(pidlBytes[i], offset, pidlBytes[i].Length); + offset += pidlBytes[i].Length; + } + return str; + } + /// This format identifier is used by a data object to indicate whether it is in a drag-and-drop loop. /// /// Some drop targets might call IDataObject::GetData and attempt to extract data while the object is still within the drag-and-drop