HOW TO:將文字儲存在剪貼簿中

更新:2007 年 11 月

下列程式碼範例會使用定義在 System.Windows.Forms 命名空間中的 Clipboard 物件來儲存字串。這個物件提供兩個成員函式:SetDataObjectGetDataObject。資料會藉由將任何衍生自 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;
}

請參閱

工作

HOW TO:從剪貼簿擷取文字

其他資源

C++ 中的 Windows 作業

.NET 程式設計指南