Game Development Reference
In-Depth Information
Querying Available Data Formats
You already know that only a single chunk of data can be stored on the Clipboard
at any one time, but it is also important to point out that some data types can be
easily converted to a variety of different formats. .NET supports both implicit and
explicit conversions between multiple data formats, but you generally need to be
aware of the formats that a certain type format can convert to; attempting to con-
vert data to an unsupported format will cause exceptions.
IDataObject has a useful method called GetFormats() that can be used to query the
formats that the stored data can be converted to, provided that auto conversion
was true when the data was stored on the Clipboard.
There are two overloaded definitions for GetFormats() shown in the following. The
first definition retrieves all the formats that the stored data can be converted to.
public string[] IDataObject.GetFormats();
The next definition allows you to specify whether or not to include all convertible
formats, or only list native types.
public string[] IDataObject.GetFormats(bool autoConvert);
Call GetFormats() to get the supported formats before calling GetData() .
Complete Solution
The code snippets in this chapter have been consolidated into a helper class that
makes it easier to work with the Clipboard. .NET 2.0 has introduced several wrap-
per methods around SetDataObject() and GetDataObject() that are specific to cer-
tain data types. The intent of this chapter is to show how the low-level API for
the Clipboard works, because all the wrapper methods do is encapsulate the
SetDataObject() approach.
The complete source code for the helper class is shown below.
internal static class ClipboardHelper
internal static string[] GetCurrentFormats()
IDataObject dataObject = Clipboard.GetDataObject();
string[] formats = dataObject.GetFormats(true);
Search Nedrilad ::

Custom Search