5.7.4 Cutting and Copying Objects to the Clipboard

A client application can copy or cut an object to the clipboard by opening the clip-board, calling the OleCopyToClipboard function, and closing the clipboard. To illustrate this sequence of steps, consider the following code from CLIDEMO.EXE:

From CLIDEMO.C:

.

.

.

case IDM_COPY:

case IDM_CUT:

ANY_OBJECT_BUSY;

if (!ObjCopy(pItem)

{

ErrorMessage((wParam==IDM_CUT) ? E_CLIPBOARD_CUT_FAILED

: E_CLIPBOARD_COPY_FAILED);

break;

}

if (wParam ==IDM_COPY)

break;

From Object.C:

.

.

.

/*

* ObjCopy()

*

* This function places an OLE object on the clipboard via the \

* OleCopyToClipboard() function.

*

* Returns BOOL - TRUE if object successfully placed on clipboard

*/

BOOL FAR ObjCopy // ENTRY:

(APPITEMPTR pItem) // pointer to app item

{ // LOCAL:

BOOL fReturn = TRUE; // return value

if (!OpenClipboard(hwndFrame))

return FALSE; // ERROR return

EmptyClipboard();

if (Error(OleCopyToClipboard(pItem->lpObject)))

fReturn = FALSE; // prepare for ERROR out

CloseClipboard();

return fReturn; // ERROR or SUCCESS

}

If the client application supports delayed rendering, however, it should:

Open and empty the clipboard.

Put the preferred data formats on the clipboard.

Call the OleEnumFormats function to retrieve the formats for the object.

Call the SetClipboardData function to put the formats on the clipboard, specifying NULL for the handle to the data.

If the call to the OleEnumFormats function retrieves the ObjectLink format, the client application should call SetClipboardData with OwnerLink instead of ObjectLink format. (See the description of the OleCopyToClipboard function in Chapter 9, "The OLE Client DLL.")

Put any additional presentation data formats on the clipboard.

Close the clipboard.

To support the Cut command on the Edit menu, a client application can call OleCopyToClipboard and then delete the object by using the OleDelete function. The client application can put only one of the selected objects on the clipboard, even when the user has selected and cut or copied multiple objects. In this case, the client application typically puts the first object in the selection onto the clipboard.

When an application, acting as both a client and server application, copies a selection to the clipboard containing one or more objects, it should first allocate enough memory for the selection. To determine the amount of memory required for each object, the application can call the OleQuerySize function. When memory has been allocated, the application should call the OleRegisterClientDoc function, specifying Clipboard for the document name. In this case, the handle returned by the call to OleRegisterClientDoc identifies a document that is used only during the copy operation.

To save each object to memory, the application calls the OleClone function, calls OleSaveToStream for the cloned object, and then calls OleRelease to free the memory for the cloned object. When the selection has been saved to the stream, the application can call the SetClipboardData function. If SetClipboardData is successful, the application should call the OleSavedClientDoc function. The application then calls the OleRevokeClientDoc function, specifying the handle retrieved by the call to OleRegisterClientDoc.