Using Clipboard C# 4.0 (Wrapper inside)






4.67/5 (3 votes)
Library that manages all objects entering and leaving the Windows clipboard.
Introduction
How many times have you wanted a library that manages all objects entering and leaving the windows clipboard? Maybe without having to handle the various types of objects ..
Here it is!
Nothing could be simpler. This library will handle everything for you through a series of delegates to communicate the appearance of new notes and a thread that has the task of effectively monitor system clipboard.
This library track:
- Text and html;
- Images;
- Files;
The library needs, for now, a Windows Forms object for the proper management of the delegates. Future versions will replace this part with an interface that will manage the delegates. It will be for those who will implement this interface to manage the Invoke in the form (or XAML view) correctly.
Using the code
// Use these methods to interact directly with the clipboard without using the manager
// These methods are a common wrapper namespace "System.Windows.Forms.Clipboard"
string[] files = ClipboardManager.GetClipboardFiles();
Image image = ClipboardManager.GetClipboardImage();
string text = ClipboardManager.GetClipboardText();
ClipboardManager.SetClipboardFiles(new string[] { "file1", "file2" });
ClipboardManager.SetClipboardImage(new Bitmap("path"));
You can use the library only in a static way. This way you can read and write files, images and text to the clipboard. Please note that in case the clipboard was empty or tried, for example, to read a text in the clipboard when there is a file, static methods will return always null. The use of "Clipboard Manager" as static class is not recommended in cases where there is no certainty of what's in the clipboard.
// Use the "ClipboardManager" to manage in a more comprehensive the clipboard
// I assume that "this" is a Form
ClipboardManager manager = new ClipboardManager(this);
// Use "All" to handle all kinds of objects from the clipboard
// otherwise use "Files", "Image" or "Text"
manager.Type = ClipboardManager.CheckType.All;
// Use events to manage the objects in the clipboard
manager.OnNewFilesFound += (sender, eventArg) =>
{
foreach (String item in eventArg)
{
Console.WriteLine("New file found in clipboard : {0}", item);
}
};
manager.OnNewImageFound += (sender, eventArg) =>
{
Console.WriteLine("New image found in clipboard -> Width: {0} , Height: {1}",
eventArg.Width, eventArg.Height);
};
manager.OnNewTextFound += (sender, eventArg) =>
{
Console.WriteLine("New text found in clipboard : {0}", eventArg);
};
// Use the method "StartChecking" to start capturing objects in the clipboard
manager.StartChecking();
// Close the capturing
manager.Dispose();
Using the class "Clipboard Manager" as an instance you have the possibility of having, by event, reporting that a new object is entered into the clipboard. This class is very convenient because it allows the user to manage the objects in the clipboard in near real time (the manager operates through a thread and so there may be about a one second delay).
History
02/10/2012: Version 1.0.0.1 (Resolved image comparison bug + adding test project)
28/09/2012: Version 1.0.0.0