Share via


Using the Clipboard in a C++ Application for the Pocket PC

 

The Clipboard allows applications to share information without requiring both applications to share common file formats, save, and load the shared component. In effect, the Clipboard acts as a temporary storage device that is either empty or contains exactly one object, which can be represented in one or more ways - or "rendering formats" - to a target application.

Applies to:
   Microsoft Windows Powered Pocket PC 2000
   Microsoft eMbedded Visual C++ version 3.0

Languages Supported

Any language supported by embedded Visual C++ 3.0. Screenshots are in U.S. English.

Clipboard Operations Common to MFC and Win32

Both methods of using the Clipboard support rendering formats and use the same concepts of copy, cut (essentially moving the selected object to the Clipboard from the source application), and paste (copying the Clipboard contents to the target application). Details on some of the specific functions of the two methods are in the next two sections, but the basic steps are as follows:

  1. In the source application, add a handler function for the copy operation. Generally, this code will be called from a menu selection or a tap-and-hold menu.
  2. In the implementation of the copy operation, lock the Clipboard to prevent other applications from modifying the contents.
  3. Empty the contents of the Clipboard.
  4. For each rendering type that you will support, allocate memory using the LocalAlloc Microsoft Win32 function. (The different rendering types are available in the online help for the SetClipboardData Win32 function, but you will primarily use the CF_TEXT, CF_UNICODETEXT, CF_BITMAP, and CF_DIB rendering formats).
  5. If none of the default formats suffice, you can also specify your own Clipboard rendering format using the RegisterClipboardFormat Win32 function, but remember that custom rendering formats will rarely be supported by target applications (unless it is another of your own applications and you simply want to be able to move objects between your applications using the Clipboard).
  6. Place as many rendering formats as possible on the Clipboard, starting with the most descriptive format. The more formats you render, the greater the chance that the target application will support one of the formats. As shown in the figure below, you won't know in advance which rendering types the target application will support. In general, you should always provide at least the CF_UNICODETEXT format containing a description of the object.
  7. Once a rendering format has been sent to the Clipboard, you may still read the data but you must not lock or delete it. It is owned by the system and will be de-allocated the next time an application clears the contents of the Clipboard.
  8. Release your lock on the Clipboard to allow other applications to modify the contents.
  9. For the cut operation, simply call the copy operation, and then delete the selected object(s) from your application. Remember, once the memory has been handed off to the Clipboard, it is no longer your responsibility to manage it.
  10. To implement the paste operation, process the WM_INITPOPUPMENU message and enable or disable the Paste menu selection based on the information obtained by a call to either IsClipboardFormatAvailable (if you only support one rendering format) or GetPriorityClipboardFormat (if you support more than one rendering format).
  11. In the processing of the paste operation, lock the Clipboard to prevent another application from modifying the contents.
  12. Use the GetClipboardData Win32 function to specify the desired format. Remember not to lock or free the memory obtained - the system will take care of it.
  13. Unlock the Clipboard before exiting your paste implementation code.

Click here for larger image

Figure 1: Multiple rendering methods of the same object. Click on thumbnail for larger image.

Implementing the Clipboard in MFC

MFC has made basic implementation of Clipboard processing rudimentary, provided that your application uses the document/view architecture of MFC. Your implementation of the CDocument::Serialize member function will let you cut, copy, and paste your entire document.

You can, of course, take control of the Clipboard operations yourself, as follows:

  1. In the implementation of the copy operation (generally the CView::OnEditCopy derived member function), lock the Clipboard using the CWnd::OpenClipboard member function to prevent other applications from modifying the contents.
  2. Empty the contents of the Clipboard using the EmptyClipboard Win32 function.
  3. Render the object as described in Step 4 of "Clipboard Operations Common to MFC and Win32" above.
  4. Release your lock on the Clipboard using the CWnd::CloseClipboard member function.
  5. For the cut operation, simply call the copy handler and then delete the selected object(s) from your application. Remember, once the memory has been handed off to the Clipboard it is no longer your responsibility to manage it.
  6. The cut-and-paste operations follow Steps 9-13 in the "Clipboard Operations Common to MFC and Win32" section above. Remember to use CWnd::OpenClipboard and CWnd::CloseClipboard to lock and unlock the Clipboard.

Implementing the Clipboard in Win32

Although you don't have the automatic support for the Clipboard facilitated by the document/view architecture of MFC, you still have access to the same Clipboard functionality as an MFC application and can, in fact, freely exchange information with both Win32 and MFC applications.

To use the Clipboard from Win32:

  1. In the implementation of the copy operation (generally the WM_COMMAND processing of a menu selection for the EDIT...COPY operation), lock the Clipboard using the OpenClipboard Win32 function to prevent other applications from modifying the contents.
  2. Empty the contents of the Clipboard using the EmptyClipboard Win32 function.
  3. Render the object as described in Step 4 of the "Clipboard Operations Common to MFC and Win32" section above.
  4. Release your lock on the Clipboard using the CloseClipboard Win32 function.
  5. For the cut operation, simply call the copy handler and then delete the selected object(s) from your application. Remember, once the memory has been handed off to the Clipboard it is no longer your responsibility to manage it.
  6. The cut-and-paste operations follow Steps 9 - 13 in the "Clipboard Operations Common to MFC and Win32" section above.

Conclusion

Implementing the Clipboard in your application allows your users to share the features and functionality of other applications, and to integrate the best of those applications' features with your own application. Considering the amount of power you add to your application for the relatively small amount of code to add it, you might want to think twice before leaving Clipboard support out of your next application's feature set.