如何:从剪贴板中检索文本 (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)

编程在Visual C++的.NET