下面的代码示例使用 System.Windows.Forms 命名空间中定义的 Clipboard 对象存储字符串。 此对象提供两个成员函数:SetDataObject 和 GetDataObject。 通过将派生自 Object 的任何对象发送给 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;
}