How to: Store Text in the Clipboard (C++/CLI)

 

For the latest documentation on Visual Studio 2017, see Visual Studio 2017 Documentation.

The following code example uses the Clipboard object defined in the System.Windows.Forms namespace to store a string. This object provides two member functions: SetDataObject and GetDataObject. Data is stored in the Clipboard by sending any object derived from Object to SetDataObject.

// store_clipboard.cpp  
// compile with: /clr  
#using <System.dll>  
#using <System.Drawing.dll>  
#using <System.Windows.Forms.dll>  
  
using namespace System;  
using namespace System::Windows::Forms;  
  
[STAThread] int main()  
{  
   String^ str = "This text is copied into the Clipboard.";  
  
   // Use 'true' as the second argument if  
   // the data is to remain in the clipboard  
   // after the program terminates.  
   Clipboard::SetDataObject(str, true);  
  
   Console::WriteLine("Added text to the Clipboard.");  
  
   return 0;  
}  

How to: Retrieve Text from the Clipboard (C++/CLI)
Windows Operations (C++/CLI)
.NET Programming with C++/CLI (Visual C++)

Show: