Game Development Reference
The Clipboard Class and IDataObject
Accessing the Clipboard and storing data is made possible by the Clipboard class
in the System.Windows.Forms namespace, specifically the SetDataObject() and
SetDataObject() is used to store arbitrary data on the Clipboard, and it defines
whether or not the data remains persisted after the application exits. Similarly,
GetDataObject() is used to retrieve arbitrary data that is stored on the Clipboard.
GetDataObject() can also be used to determine whether data exists on the
Clipboard, and what format the data is. Both methods use the IDataObject inter-
face, which is a format-independent mechanism that is used for data transfer and
notification of changes in data. The IDataObject interface is used in this situation
because arbitrary data can be stored on the Clipboard, and we need an initial way
to determine what format the data is before we can work with it.
The SetDataObject() method has two overloaded definitions. The first one (shown
in the following line of code) is used to store an object on the Clipboard, releasing
the data when the application exits.
public static void SetDataObject(object data);
The following method has a copy parameter that specifies whether the data should
be persisted on the Clipboard after the application exits.
public static void SetDataObject(object data, bool copy);
The GetData() method can be used to get the associated data and convert it to the
Storing Built-In Types
Storing built-in types is very simple, especially since .NET automatically handles
conversion between similar data formats (ANSI and Unicode text, for example).
The following code shows how to store a built-in type ( DataFormats.Text for this
string text = “This is a test!”;
IDataObject dataObject = new DataObject();
dataObject.SetData(DataFormats.Text, true, text);
The following code shows how to retrieve the text from the Clipboard.