The Clipboard
The clipboard is where your data is stored when you copy or cut some text or an
image or a file or a folder. The clipboard is common across processes, which
means we can use the clipboard as a mechanism to transfer data between two
processes. Win32 gave us a Clipboard API with functions like OpenClipboard
and
GetClipboardData
which most of you might be familiar with. In .NET Microsoft
has encapsulated the functionality of most of these functions into some
classes. The most prominent of these classes is a class which has
been named aptly as Clipboard
. It's a very simple class with just
two functions. In this article, we'll see how to copy/paste text, images
and also how to maintain clipboard data in multiple formats.
I'd like to thank James Johnson for his amazing help while I
was fiddling with the clipboard. I didn't have a reference book to help
me, but that didn't matter much as James more than made up for it. Poor Mike Dunn had to suffer some .NET talk when he walked into
Bob's HungOut just as I was discussing an issue with James. Anyway
let's get on with things now.
Copying text into the clipboard
The Clipboard
class has a SetDataObject
function which is used to insert clipboard data. It takes an Object
as
argument in the single argument overload. In the other overload it takes
both an Object
and a bool
variable, which we can set
to true
if we want the clipboard to persist even
after our application has exited. In our example program we'll use the
second overload as that seems to be the more common situation, where we
would want other programs to access what we have copied onto the
clipboard.
Clipboard.SetDataObject(textBox1.Text,true);
Retrieving text from the clipboard
The Clipboard
class also has a
GetDataObject
function which returns an IDataObject
.
The IDataObject
interface has a function called
GetDataPresent
which we use to determine if the
IDataObject
has data of a specific format. We can
use the static fields of the DataFormats
class to
verify that the format we expect is indeed available in the
IDataObject
. If GetDataPresent
returns
true
, we can then use the GetData
function to retrieve the data format that we are expecting.
if(Clipboard.GetDataObject().GetDataPresent(DataFormats.Text))
textBox1.Text = Clipboard.GetDataObject().GetData(DataFormats.Text).ToString();
else
textBox1.Text = "The clipboad does not contain any text";
Observe how I have copied some text from Notepad and pasted it into
our sample program. You can also copy text from our program and paste it
into Notepad. And I hope you like my XP theme and colors as well.
Copying/Retrieving images from the Clipboard
This is exactly the same as in copying and pasting text except that
we use the DataFormats.Bitmap
format instead of DataFormats.Text
Clipboard.SetDataObject(pictureBox1.Image,true);
if(Clipboard.GetDataObject().GetDataPresent(DataFormats.Bitmap))
pictureBox1.Image = (Bitmap)Clipboard.GetDataObject().GetData(DataFormats.Bitmap);
I have copied from Paint Brush into our sample program. You can then
copy something else using Paint Brush, come back to our program, copy
the image and paste back into Paint Brush. The image has stretched a bit
because I have set the SizeMode
property of
the Picture control to StretchImage
.
Maintain multiple formats in the Clipboard
There are situations where you are not sure what kind of data the
target application of a copy/paste cycle is expecting in the clipboard.
In such cases we can maintain multiple data formats in the clipboard, so
that the target application can retrieve the data format it's expecting.
In our sample, we'll copy both the text in the edit box as well as the
image in the picture control into the clipboard. Now you can open Notepad
and paste into it the text as well as open Paint Brush and paste into it
the bitmap. I have provided a [Paste Both] button for ease of
demonstration. Just run a second instance of the sample program and we
can paste both the text and the image into the respective controls.
We make use of the DataObject
class which
implements the IDataObject
interface. We call the
SetData
function twice, each time passing in a
different format data. We use the following override of the function :-
[ClassInterface(ClassInterfaceType.None)]
public virtual void SetData(
string format,
bool autoConvert,
object data
);
The code is very straightforward. We create a DataObject
object, use SetData
twice, once with the text and
then with the image, then we add this DataObject
to
the clipboard.
DataObject m_data = new DataObject();
m_data.SetData(DataFormats.Text,true,textBox1.Text);
m_data.SetData(DataFormats.Bitmap,true,pictureBox1.Image);
Clipboard.SetDataObject(m_data,true);
Now you can paste both into Notepad as well as into any Graphics
application that allows you to paste a Bitmap into it.