[Previous] [Next]

Chapter 19

The Clipboard and OLE Drag-and-Drop

Since version 1.0, Microsoft Windows has supported the transfer of data through the clipboard. The clipboard is a centralized location where data can be stored en route from one application to another or on its way to another part of the same application. Even novices quickly grasp the clipboard's simple cut/copy/paste paradigm: select the data, cut it or copy it to the clipboard, and paste it somewhere else. Many applications feature Cut, Copy, and Paste commands in their Edit menus, and millions of Windows users know Ctrl-X, Ctrl-C, and Ctrl-V as the keyboard equivalents of these basic clipboard commands.

The original clipboard (which I'll refer to as the legacy clipboard or simply as the clipboard) is still present in Windows 98 and Windows 2000, but programmers are encouraged to forego using it in favor of a newer, more capable clipboard known as the OLE clipboard. The OLE clipboard is backward-compatible with the legacy clipboard, meaning that one application can copy data to the OLE clipboard and another application can retrieve that data from the legacy clipboard, and vice versa. More important, the OLE clipboard lets programmers do a few things that the legacy clipboard doesn't. To transfer a large bitmap using the legacy clipboard, for example, you must allocate enough memory to store the entire bitmap. Transfer the same bitmap through the OLE clipboard, however, and you can store the bitmap in a more sensible storage medium, such as in a file on a hard disk.

Closely related to the OLE clipboard is a visual method of transferring data called OLE drag-and-drop. OLE drag-and-drop simplifies cut/copy/paste by eliminating the middleman. Rather than requiring one command to move the data to the clipboard and another to paste it into a document, OLE drag-and-drop lets you grab a piece of data with the mouse and drag it to the desired location. Under the hood, a mechanism much like the OLE clipboard is used to facilitate the data transfer. But to the user, the transfer appears seamless—and very intuitive.

MFC doesn't provide explicit support for operations involving the legacy clipboard, but it does support the OLE clipboard and OLE drag-and-drop. Writing code to use either of these data transfer mechanisms is no picnic if you're doing it from scratch, but MFC makes it surprisingly simple.

The best way to understand the mechanics of the OLE clipboard and OLE drag-and-drop is to first understand how the legacy clipboard works. To that end, I'll begin this chapter with an overview of the legacy clipboard and a review of the Windows API functions used to access it. I'll then introduce the OLE clipboard and OLE drag-and-drop and demonstrate how to use MFC to develop applications that support both of them in no time flat.

The CHM file was converted to HTML by chm2web software.