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 COleClientItem object that has been selected.
If the data is native and the application is a server, create a new object derived from COleServerItem containing the selected data. Otherwise, create a COleDataSource object for the data.
Call the selected item's CopyToClipboard member function.
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 OnEditCut and 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
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 OnEditPaste to handle users choosing the Paste option from the Edit menu.
In the OnEditPaste function, create a COleDataObject object and call its AttachClipboard member function to link this object to the data on the Clipboard.
Call COleDataObject::IsDataAvailable to check whether a particular format is available.
Alternately, you can use COleDataObject::BeginEnumFormats to look for other formats until you find one most suited to your application.
Perform the paste of the format.
For an example of how this works, see the implementation of the OnEditPaste member functions in the view classes defined in the MFC OLE sample programs OCLIENT and HIERSVR.
提示
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.