Chapter 12 The OLE Clipboard

What do you do with a data object?

What do you do with a data object?

What do you do with a data object?

Put it on the clipboard!

Version 2 of an old sea chantey

As we saw in Chapter 10, data objects encompass the functionality of all the existing data transfer protocols. Using raw data objects by themselves to exchange information between applications is rich enough to describe anything you could do with existing Windows APIs, and more. In this chapter, we'll focus on how you can use data objects to implement clipboard operations.

Conceptually the operations are simple: to place data on the clipboard, you pass an IDataObject pointer to OLE, which enumerates that object's available formats and calls IDataObject::GetData when a consumer requests data. To retrieve information from the clipboard, you ask OLE for a data object that represents whatever is currently on the clipboard and then call IDataObject::GetData.

Simple? On the surface, yes, but having to create an entire data object and FORMATETC enumerator to do something as simple as copy a small piece of text to the clipboard seems like overkill. Accordingly, the first part of this chapter describes how much of a data object you need for clipboard transfers and discusses implementation of a data transfer component that provides a generic data object for such transfers. We'll then see how Cosmo and Patron incorporate this component to implement their clipboard support. Through Cosmo, we'll see how clipboard code based on the Windows API is converted to use the OLE Clipboard. Through Patron, we'll see the addition of new clipboard code that enables this application to paste both bitmaps and metafiles using the concepts from the Freeloader sample in Chapter 11. In addition, we'll discuss the Paste Special dialog box from the OLE UI Library and see how Component Cosmo implements clipboard support using a snapshot copy of the Polyline component (because Polyline already implements IDataObject). In essence, this involves the same techniques as working with the data transfer object but on a more application-specific basis.

Using the OLE Clipboard protocol is not a requirement for your application unless you want it to take advantage of Uniform Data Transfer through the FORMATETC and STGMEDIUM structures. In other words, unless you need to exchange data with formats and mediums other than those you can use with the Windows API, you do not need to use OLE. But as we'll see in later chapters, the transfer of compound document objects and OLE Controls requires specific storage mediums that make use of the OLE Clipboard. Because the protocol itself is rather simple and you gain so much flexibility in making the change, I encourage you to follow the guidelines in this chapter.

With that bit of preaching out of the way, let's look at the mechanisms for the OLE Clipboard and then dive into code examples. This chapter is light on theory and heavy on coding because all we're concerned with here is the creative use of a data object.