Clipboard.GetDataObject 方法   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
检索当前位于系统剪贴板中的数据。
public:
 static System::Windows::Forms::IDataObject ^ GetDataObject();public static System.Windows.Forms.IDataObject GetDataObject ();public static System.Windows.Forms.IDataObject? GetDataObject ();static member GetDataObject : unit -> System.Windows.Forms.IDataObjectPublic Shared Function GetDataObject () As IDataObject返回
IDataObject,表示系统剪贴板中当前的数据;如果剪贴板中没有数据,则为 null。
例外
未能从剪贴板中检索到数据。 这种情况通常发生在剪贴板正在被其他进程使用的时候。
当前线程未处于单线程单元 (STA) 模式下,且 MessageLoop 属性值为 true。 请将 STAThreadAttribute 添加到应用程序的 Main 方法中。
示例
下面的代码示例使用 Clipboard 方法将数据放在系统剪贴板上并从中检索数据。 此代码假定 button1、button2、已textBox1``textBox2放置在窗体上。
该方法 button1_Click 调用 SetDataObject 从文本框中获取所选文本,并将其放置在系统剪贴板上。
该方法 button2_Click 调用 GetDataObject 从系统剪贴板检索数据。 代码使用 IDataObject 和 DataFormats 提取返回的数据。 数据显示在 textBox2.
private:
   void button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      // Takes the selected text from a text box and puts it on the clipboard.
      if ( !textBox1->SelectedText->Equals( "" ) )
      {
         Clipboard::SetDataObject( textBox1->SelectedText );
      }
      else
      {
         textBox2->Text = "No text selected in textBox1";
      }
   }
   void button2_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      // Declares an IDataObject to hold the data returned from the clipboard.
      // Retrieves the data from the clipboard.
      IDataObject^ iData = Clipboard::GetDataObject();
      
      // Determines whether the data is in a format you can use.
      if ( iData->GetDataPresent( DataFormats::Text ) )
      {
         // Yes it is, so display it in a text box.
         textBox2->Text = (String^)(iData->GetData( DataFormats::Text ));
      }
      else
      {
         // No it is not.
         textBox2->Text = "Could not retrieve data off the clipboard.";
      }
   }
private void button1_Click(object sender, System.EventArgs e) {
    // Takes the selected text from a text box and puts it on the clipboard.
    if(textBox1.SelectedText != "")
       Clipboard.SetDataObject(textBox1.SelectedText);
    else
       textBox2.Text = "No text selected in textBox1";
 }
 
 private void button2_Click(object sender, System.EventArgs e) {
    // Declares an IDataObject to hold the data returned from the clipboard.
    // Retrieves the data from the clipboard.
    IDataObject iData = Clipboard.GetDataObject();
 
    // Determines whether the data is in a format you can use.
    if(iData.GetDataPresent(DataFormats.Text)) {
       // Yes it is, so display it in a text box.
       textBox2.Text = (String)iData.GetData(DataFormats.Text); 
    }
    else {
       // No it is not.
       textBox2.Text = "Could not retrieve data off the clipboard.";
    }
 }
Private Sub button1_Click(sender As Object, e As System.EventArgs)
    ' Takes the selected text from a text box and puts it on the clipboard.
    If textBox1.SelectedText <> "" Then
        Clipboard.SetDataObject(textBox1.SelectedText)
    Else
        textBox2.Text = "No text selected in textBox1"
    End If
End Sub
 
Private Sub button2_Click(sender As Object, e As System.EventArgs)
    ' Declares an IDataObject to hold the data returned from the clipboard.
    ' Retrieves the data from the clipboard.
    Dim iData As IDataObject = Clipboard.GetDataObject()
    
    ' Determines whether the data is in a format you can use.
    If iData.GetDataPresent(DataFormats.Text) Then
        ' Yes it is, so display it in a text box.
        textBox2.Text = CType(iData.GetData(DataFormats.Text), String)
    Else
        ' No it is not.
        textBox2.Text = "Could not retrieve data off the clipboard."
    End If
End Sub
注解
由于从剪贴板返回的对象数据类型可能会有所不同,因此此方法返回数据 IDataObject。 然后,可以使用接口的方法 IDataObject 提取其适当数据类型中的数据。
此方法尝试以 100 毫秒间隔获取数据 10 次,如果所有尝试都失败,则会引发一次 ExternalException 。
备注
类 Clipboard 只能在设置为单线程单元 (STA) 模式的线程中使用。 若要使用此类,请确保使用属性标记STAThreadAttribute方法Main。