/// <summary> /// Empty the Clipboard and Restore to system clipboard data contained in a collection of ClipData objects /// </summary> /// <param name="clipData">The collection of ClipData containing data stored from clipboard</param> /// <returns></returns> public static bool SetClipboard(ReadOnlyCollection <ClipData> clipData) { //Open clipboard to allow its manipultaion if (!OpenClipboard(IntPtr.Zero)) { return(false); } //Clear the clipboard EmptyClipboard(); if (clipData == null || clipData.Count == 0) { return(false); } //Get an Enumerator to iterate into each ClipData contained into the collection IEnumerator <ClipData> cData = clipData.GetEnumerator(); while (cData.MoveNext()) { ClipData cd = cData.Current; //Get the pointer for inserting the buffer data into the clipboard IntPtr alloc = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, cd.Size); IntPtr gLock = GlobalLock(alloc); //Clopy the buffer of the ClipData into the clipboard if ((int)cd.Size > 0) { Marshal.Copy(cd.Buffer, 0, gLock, cd.Buffer.GetLength(0)); } else { } //Release pointers GlobalUnlock(alloc); SetClipboardData(cd.Format, alloc); } ; //Close the clipboard to realese unused resources CloseClipboard(); return(true); }
/// <summary> /// Convert to a ClipData collection all data present in the clipboard /// </summary> /// <returns></returns> public static ReadOnlyCollection<ClipData> GetClipboard() { //Init a list of ClipData, which will contain each Clipboard Data List<ClipData> clipData = new List<ClipData>(); //Open Clipboard to allow us to read from it if (!OpenClipboard(IntPtr.Zero)) return new ReadOnlyCollection<ClipData>(clipData); //Loop for each clipboard data type uint format = 0; while ((format = EnumClipboardFormats(format)) != 0) { //Check if clipboard data type is recognized, and get its name string formatName = "0"; ClipData cd; if (format > 14) { StringBuilder res = new StringBuilder(); if (GetClipboardFormatName(format, res, 200) > 0) { formatName = res.ToString(); } } //Get the pointer for the current Clipboard Data IntPtr pos = GetClipboardData(format); //Goto next if it's unreachable if (pos == IntPtr.Zero) continue; //Get the clipboard buffer data properties UIntPtr lenght = GlobalSize(pos); IntPtr gLock = GlobalLock(pos); byte[] buffer; if ((int)lenght > 0) { //Init a buffer which will contain the clipboard data buffer = new byte[(int)lenght]; int l = Convert.ToInt32(lenght.ToString()); //Copy data from clipboard to our byte[] buffer Marshal.Copy(gLock, buffer, 0, l); } else { buffer = new byte[0]; } //Create a ClipData object that represtens current clipboard data cd = new ClipData(format, formatName, buffer); cd.FormatName = formatName; //Add current Clipboard Data to the list clipData.Add(cd); } //Close the clipboard and realese unused resources CloseClipboard(); //Returns the list of Clipboard Datas as a ReadOnlyCollection of ClipData return new ReadOnlyCollection<ClipData>(clipData); }
/// <summary> /// Convert to a ClipData collection all data present in the clipboard /// </summary> /// <returns></returns> public static ReadOnlyCollection <ClipData> GetClipboard() { //Init a list of ClipData, which will contain each Clipboard Data List <ClipData> clipData = new List <ClipData>(); //Open Clipboard to allow us to read from it if (!OpenClipboard(IntPtr.Zero)) { return(new ReadOnlyCollection <ClipData>(clipData)); } //Loop for each clipboard data type uint format = 0; while ((format = EnumClipboardFormats(format)) != 0) { //Check if clipboard data type is recognized, and get its name string formatName = "0"; ClipData cd; if (format > 14) { StringBuilder res = new StringBuilder(); if (GetClipboardFormatName(format, res, 200) > 0) { formatName = res.ToString(); } } //Get the pointer for the current Clipboard Data IntPtr pos = GetClipboardData(format); //Goto next if it's unreachable if (pos == IntPtr.Zero) { continue; } //Get the clipboard buffer data properties UIntPtr lenght = GlobalSize(pos); IntPtr gLock = GlobalLock(pos); byte[] buffer; if ((int)lenght > 0) { //Init a buffer which will contain the clipboard data buffer = new byte[(int)lenght]; int l = Convert.ToInt32(lenght.ToString()); //Copy data from clipboard to our byte[] buffer Marshal.Copy(gLock, buffer, 0, l); } else { buffer = new byte[0]; } //Create a ClipData object that represtens current clipboard data cd = new ClipData(format, formatName, buffer); cd.FormatName = formatName; //Add current Clipboard Data to the list clipData.Add(cd); } //Close the clipboard and realese unused resources CloseClipboard(); //Returns the list of Clipboard Datas as a ReadOnlyCollection of ClipData return(new ReadOnlyCollection <ClipData>(clipData)); }