Click here to Skip to main content
15,868,141 members
Articles / Programming Languages / C#
Tip/Trick

Using Clipboard C# 4.0 (Wrapper inside)

Rate me:
Please Sign up or sign in to vote.
4.67/5 (3 votes)
2 Oct 2012CPOL1 min read 26.4K   1.2K   16   4
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

C#
// 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.

C#
// 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 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) D.Net Solution
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionA few things... Pin
Andrew Rissing28-Sep-12 5:50
Andrew Rissing28-Sep-12 5:50 
GeneralRe: A few things... Pin
D.Net Solutions1-Oct-12 2:59
D.Net Solutions1-Oct-12 2:59 
GeneralRe: A few things... Pin
Andrew Rissing1-Oct-12 3:48
Andrew Rissing1-Oct-12 3:48 
GeneralRe: A few things... Pin
D.Net Solutions2-Oct-12 0:33
D.Net Solutions2-Oct-12 0:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.