Click here to Skip to main content
Click here to Skip to main content

Using Clipboard C# 4.0 (Wrapper inside)

By , 2 Oct 2012
Rate this:
Please Sign up or sign in to vote.

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 

License

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

About the Author

D.Net Solutions
Software Developer (Senior) D.Net Solution
Italy Italy
No Biography provided
Follow on   Twitter   Google+

Comments and Discussions

 
SuggestionA few things... PinmemberAndrew Rissing28-Sep-12 5:50 
-StringCollection contains a CopyTo method you might want to check out.
-CheckClipBoard()
--You may want to break each case into a separate method so that the "All" case doesn't repeat this logic.
--Image/Files will not work as it creates a new instance each time a Get is called. You'll want to try that one out.
GeneralRe: A few things... PinmemberD.Net Solutions1-Oct-12 2:59 
GeneralRe: A few things... PinmemberAndrew Rissing1-Oct-12 3:48 
GeneralRe: A few things... PinmemberD.Net Solutions2-Oct-12 0:33 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 2 Oct 2012
Article Copyright 2012 by D.Net Solutions
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid