HOW TO:從剪貼簿擷取文字 (C++/CLI)

下列程式碼範例會使用 GetDataObject 成員函式,傳回 IDataObject 介面的指標。這個介面可以用來查詢資料的格式並用來擷取真正的資料。

範例

// read_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( )
{
   IDataObject^ data = Clipboard::GetDataObject( );

   if (data)
   {
      if (data->GetDataPresent(DataFormats::Text)) 
      {
         String^ text = static_cast<String^>
           (data->GetData(DataFormats::Text));
         Console::WriteLine(text); 
      }
      else
         Console::WriteLine("Nontext data is in the Clipboard.");
   }
   else 
   {
      Console::WriteLine("No data was found in the Clipboard.");
   }

   return 0;
}

請參閱

其他資源

Windows 作業 (C++/CLI)

.NET 程式設計的 Visual C++