|
|||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionAt the end of April 2002, Nish and I were trying to figure out some of .NET's clipboard capabilities. When we had finished the conversation we decided on writing a two part article, Nish doing part one on the basics then I would do part two on some more advanced topics. I was going to write it as soon as the screensaver competition was over, needless to say I forgot....until now that is. In this article I hope to cover multiple data formats, and creating your own custom data formats. Multiple data formatsPlacing multiple data formats on the clipboard is incredibly easy; once you know the trick :-) The first tric...er step is to figure out what data formats you will be using.
Later I'll discuss custom formats so ignore those for now. To help you in
your search the Now that you know the formats you will use, create a instance of a class that
implements IDataObject ido = new DataObject();
With the ido.SetData(DataFormats.Text, true, myString);
That line of code adds a new Text formatted object with the value of myString
to the Adding multiple, additional, formats is as easy as additional calls to Clipboard.SetDataObject(ido, true);
This line finally places the data on the clipboard where it can be read by
any program. The With the data copied to the IDataObject ido = Clipboard.GetDataObject();
if(ido.GetDataPresent(DataFormats.Text))
{
// Text data is present on the clipboard
textBox1.Text = (string) ido.GetData(DataFormats.Text);
}
Custom data formatsA custom data format can be anything; whether a special text formatting such as HTML or XML or it can be an instance of a class. To use a custom format, first you register it with Windows. In the demo application you will find that I register the type with Windows in the static constructor for the custom type class, CustomData. static CustomData
{
format = DataFormats.GetFormat(
typeof(CustomData).FullName
);
}
The code The only unique part about creating a custom format that uses a class is that
the class must be serializable, that is it needs to have the [Serializable()]
public class CustomData
.....
To use it on the clipboard you do so as you would any other format. IDataObject ido = new DataObject();
CustomData cd = new CustomData(myImage, myString);
ido.SetData(CustomData.Format.Name, false, cd);
Retrieving data from it is done the same as before. IDataObject ido = Clipboard.GetDataObject();
if( ido.GetDataPresent(CustomData.Format.Name) )
{
// Do something interesting with the data
CustomData cd = (CustomData)
ido.GetData(CustomData.Format.Name);
}
Persisting data on the clipboard, ie the 2nd parameter to SetDataObjectAccording to MSDN the second parameter tells the To see this in action run the included ClipboardTest2 application, uncheck "Persist Data"; then proceed to Draw, Copy, and Paste, then Draw and Paste. You'll see that even though the data was copied only once the new data is shown. The reason for this lies in that I continue to operate on the same object
that was passed into the To demonstrate this open up the ClipboardTest2 application, again uncheck "Persist Data"; and then Draw, Copy, and Paste. Proceed to Draw and Paste, then open up your favorite image editing software; MSpaint will do. Now Paste the bitmap there; do some more Draw and Pastes then Paste again in another copy of MSPaint. You'll see the same bitmap was pasted in both MSPaint's. This seems like a bug, but really it is a bug in my use. Once you place an object on the clipboard the intended result is that object stays the same; not to change it like I did. This means the optimal solution is to create a copy and cache it somewhere until it is needed by the Clipboard object. If you choose to not persist data to the clipboard, upon exiting the application you should re-place the data on the clipboard and persist it; so that the contents are still there for the user to use. ConclusionThere you have it folks, the For those interested in the Visual Style I used in the screen shots, I got it from themexp.org; you'll also need StyleXP or the free update to uxtheme.dll from the same site.
| ||||||||||||||||||||||||||||||