Clipboard: Copying and Pasting Data
This topic describes the minimum work necessary to implement copying to and pasting from the Clipboard in your OLE application. It is recommended that you read the Data Objects and Data Sources (OLE) topics before proceeding.
Before you can implement either copying or pasting, you must first provide functions to handle the Copy, Cut, and Paste options on the Edit menu.
Copying or Cutting Data
To copy data to the Clipboard
Determine whether the data to be copied is native data or is an embedded or linked item.
If the data is embedded or linked, obtain a pointer to the
COleClientItemobject that has been selected.
If the data is native and the application is a server, create a new object derived from
COleServerItemcontaining the selected data. Otherwise, create a
COleDataSourceobject for the data.
Call the selected item's
If the user chose a Cut operation instead of a Copy operation, delete the selected data from your application.
To see an example of this sequence, see the
OnEditCopy functions in the MFC OLE sample programs OCLIENT and HIERSVR. Note that these samples maintain a pointer to the currently selected data, so step 1 is already complete.
Pasting data is more complicated than copying it because you need to choose the format to use in pasting the data into your application.
To paste data from the Clipboard
In your view class, implement
OnEditPasteto handle users choosing the Paste option from the Edit menu.
OnEditPastefunction, create a
COleDataObjectobject and call its
AttachClipboardmember function to link this object to the data on the Clipboard.
COleDataObject::IsDataAvailableto check whether a particular format is available.
Alternately, you can use
COleDataObject::BeginEnumFormatsto look for other formats until you find one most suited to your application.
Perform the paste of the format.
The main benefit of separating the paste operation into its own function is that the same paste code can be used when data is dropped in your application during a drag-and-drop operation. As in OCLIENT and HIERSVR, your
OnDrop function can also call
DoPasteItem, reusing the code written to implement Paste operations.
To handle the Paste Special option on the Edit menu, see the topic Dialog Boxes in OLE.
What do you want to know more about