Compartir a través de


Clipboard: Using the Windows Clipboard

This topic describes how to use the standard Windows Clipboard API within your MFC application.

Most applications for Windows support cutting or copying data to the Windows Clipboard and pasting data from the Clipboard. The Clipboard data formats vary among applications. The framework supports only a limited number of Clipboard formats for a limited number of classes. You will normally implement the Clipboard-related commands — Cut, Copy, and Paste — on the Edit menu for your view. The class library defines the command IDs for these commands: ID_EDIT_CUT, ID_EDIT_COPY, and ID_EDIT_PASTE. Their message-line prompts are also defined.

Messages and Commands in the Framework explains how to handle menu commands in your application by mapping the menu command to a handler function. As long as your application does not define handler functions for the Clipboard commands on the Edit menu, they remain disabled. To write handler functions for the Cut and Copy commands, implement selection in your application. To write a handler function for the Paste command, query the Clipboard to see whether it contains data in a format your application can accept. For example, to enable the Copy command, you might write a handler something like the following:

void CMyListView::OnEditCopy()
{
   if ( !OpenClipboard() )
   {
      AfxMessageBox( _T("Cannot open the Clipboard") );
      return;
   }
   // Remove the current Clipboard contents
   if( !EmptyClipboard() )
   {
      AfxMessageBox( _T("Cannot empty the Clipboard") );
      return;
   }
   // Get the currently selected data
   HGLOBAL hGlob = GlobalAlloc(GMEM_FIXED, 64);
   strcpy_s((char*)hGlob, 64, "Current selection\r\n");
   // For the appropriate data formats...
   if ( ::SetClipboardData( CF_TEXT, hGlob ) == NULL )
   {
      CString msg;
      msg.Format(_T("Unable to set Clipboard data, error: %d"), GetLastError());
      AfxMessageBox( msg );
      CloseClipboard();
      GlobalFree(hGlob);
      return;
   }
   CloseClipboard();
}

The Cut, Copy, and Paste commands are only meaningful in certain contexts. The Cut and Copy commands should be enabled only when something is selected, and the Paste command only when something is in the Clipboard. You can provide this behavior by defining update handler functions that enable or disable these commands depending on the context. For more information, see How to Update User-Interface Objects.

The Microsoft Foundation Class Library does provide Clipboard support for text editing with the CEdit and CEditView classes. The OLE classes also simplify implementing Clipboard operations that involve OLE items. For more information on the OLE classes, see Clipboard: Using the OLE Clipboard Mechanism.

Implementing other Edit menu commands, such as Undo (ID_EDIT_UNDO) and Redo (ID_EDIT_REDO), is also left to you. If your application does not support these commands, you can easily delete them from your resource file using the Visual C++ resource editors.

What do you want to know more about?

See Also

Concepts

Clipboard