|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThe code uses Windows API calls to read and write into the clipboard. It provides a ClipboardHelper, an easy to use API bridge, which offers, in addition, functions to serialize complex clipboard data to hard disk and restore them when you want, using the I tested it with Internet Explorer, Word, OpenOffice, including with image files. BackgroundThe .NET framework and the To read and manipulate these data, we need to call user32.dll and kernerl32.dll. Using the codeThe code includes two projects: the library ClipboardHelper, and the console application ClipboardTest. The whole code is commented, to understand the API usage and the implemented saving process. To use the library is really easy: it's a static class that exposes a //Comment next line to end the demo mode
//and backup your clipboard data.
ClipboardHelper.Deserialize(demo);
Console.WriteLine("restore the demo clipboard");
//Open the clipboard and serialize into a directory
ClipboardHelper.Serialize(fileName);
Console.WriteLine("serialize clipboard to " + fileName);
//Deserialize the clipboard and set data
//to win clipboard ready to be pasted
ClipboardHelper.Deserialize(fileName);
Console.WriteLine("restore the clipboard " + fileName);
The test application shows an example of the Inside the ClipboardHelperData returned by API calls are saved in public static void SaveToFile(ReadOnlyCollection<DATACLIP> clipData, string clipName)
{
IEnumerator<DATACLIP> cData = clipData.GetEnumerator();
while (cData.MoveNext())
{
XmlSerializer xml = new XmlSerializer(typeof(DataClip));
using (StreamWriter sw = new StreamWriter(di.FullName +
@"\" + i.ToString() + ".cli",false))
{
xml.Serialize(sw, cData.Current);
}
}
}
Deep into the clipboardThe clipboard contains data in fragmented buffers. Each of them has a specific EnumClipboardFormats of user32.dll returns the array of DataFormats contained in the clipboard. For each DataFormat, we can call GetClipboardData. It returns the handle to the clipboard object. By calling GlobalSize and GlobalLock from kernel32.dll, we obtain the clipboard object properties (its length and the pointer to it), so we can copy the clipboard object into our buffer (a byte array) via the Marshal.Copy method.
To get the clipboard, first of all, check to open the clipboard: Win32ClipboardAPI.OpenClipboard(IntPtr.Zero)
Then, we get all //Init a list of ClipData,
// which will contain each Clipboard Data
List<DataClip> clipData = new List<DataClip>();
//Loop for each clipboard data type
uint format = 0;
while ((format = Win32ClipboardAPI.EnumClipboardFormats(format)) != 0)
{
//Get the formatName
StringBuilder formatName = new StringBuilder();
Win32ClipboardAPI.GetClipboardFormatName(format,
formatName, 100);
//Get the pointer for the current Clipboard Data
IntPtr pos = Win32ClipboardAPI.GetClipboardData(format);
//Get the clipboard buffer data properties
UIntPtr lenght = Win32MemoryAPI.GlobalSize(pos);
IntPtr gLock = Win32MemoryAPI.GlobalLock(pos);
byte[] buffer;
//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);
//Create a ClipData object that
//represents the current clipboard data
DataClip cd = new DataClip(format,
formatName.ToString(), buffer);
//Add current Clipboard Data to the list
clipData.Add(cd);
}
History
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||