/// <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 <DataClip> clipData) { if (MonoHelper.IsUnix) { return(false); } //Open clipboard to allow its manipultaion if (!NativeMethods.OpenClipboard(IntPtr.Zero)) { return(false); } //Clear the clipboard NativeMethods.EmptyClipboard(); //Get an Enumerator to iterate into each ClipData contained into the collection IEnumerator <DataClip> cData = clipData.GetEnumerator(); while (cData.MoveNext()) { DataClip cd = cData.Current; //Get the pointer for inserting the buffer data into the clipboard IntPtr alloc = NativeMethods.GlobalAlloc(NativeMethods.GMEM_MOVEABLE | NativeMethods.GMEM_DDESHARE, cd.Size); IntPtr gLock = NativeMethods.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 NativeMethods.GlobalUnlock(alloc); NativeMethods.SetClipboardData((uint)cd.Format, alloc); } ; //Close the clipboard to realese unused resources NativeMethods.CloseClipboard(); return(true); }
/// <summary> /// Convert to a DataClip collection all data present in the clipboard /// </summary> /// <returns></returns> public static ReadOnlyCollection <DataClip> GetClipboard() { //Init a list of ClipData, which will contain each Clipboard Data List <DataClip> clipData = new List <DataClip>(); //Open Clipboard to allow us to read from it if (!NativeMethods.OpenClipboard(IntPtr.Zero)) { return(new ReadOnlyCollection <DataClip>(clipData)); } //Loop for each clipboard data type int format = 0; while ((format = (int)NativeMethods.EnumClipboardFormats((uint)format)) != 0) { //Check if clipboard data type is recognized, and get its name string formatName = "0"; DataClip cd; if (format > 14) { StringBuilder res = new StringBuilder(); if (NativeMethods.GetClipboardFormatName((uint)format, res, 100) > 0) { formatName = res.ToString(); } } //Get the pointer for the current Clipboard Data IntPtr pos = NativeMethods.GetClipboardData((uint)format); //Goto next if it's unreachable if (pos == IntPtr.Zero) { continue; } //Get the clipboard buffer data properties UIntPtr lenght = NativeMethods.GlobalSize(pos); IntPtr gLock = NativeMethods.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 DataClip(format, formatName, buffer); cd.FormatName = formatName; //Add current Clipboard Data to the list clipData.Add(cd); } //Close the clipboard and realese unused resources NativeMethods.CloseClipboard(); //Returns the list of Clipboard Datas as a ReadOnlyCollection of ClipData return(new ReadOnlyCollection <DataClip>(clipData)); }
/// <summary> /// Convert to a DataClip collection all data present in the clipboard /// </summary> /// <returns></returns> public static ReadOnlyCollection<DataClip> GetClipboard() { //Init a list of ClipData, which will contain each Clipboard Data List<DataClip> clipData = new List<DataClip>(); //Open Clipboard to allow us to read from it if (!NativeMethods.OpenClipboard(IntPtr.Zero)) return new ReadOnlyCollection<DataClip>(clipData); //Loop for each clipboard data type int format = 0; while ((format = (int)NativeMethods.EnumClipboardFormats((uint)format)) != 0) { //Check if clipboard data type is recognized, and get its name string formatName = "0"; DataClip cd; if (format > 14) { StringBuilder res = new StringBuilder(); if (NativeMethods.GetClipboardFormatName((uint)format, res, 100) > 0) { formatName = res.ToString(); } } //Get the pointer for the current Clipboard Data IntPtr pos = NativeMethods.GetClipboardData((uint)format); //Goto next if it's unreachable if (pos == IntPtr.Zero) continue; //Get the clipboard buffer data properties UIntPtr lenght = NativeMethods.GlobalSize(pos); IntPtr gLock = NativeMethods.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 DataClip(format, formatName, buffer); cd.FormatName = formatName; //Add current Clipboard Data to the list clipData.Add(cd); } //Close the clipboard and realese unused resources NativeMethods.CloseClipboard(); //Returns the list of Clipboard Datas as a ReadOnlyCollection of ClipData return new ReadOnlyCollection<DataClip>(clipData); }