Clipboard.GetDataObject 方法

定义

检索当前位于系统剪贴板中的数据。Retrieves the data that is currently on the system Clipboard.

public:
 static System::Windows::Forms::IDataObject ^ GetDataObject();
public static System.Windows.Forms.IDataObject GetDataObject ();
static member GetDataObject : unit -> System.Windows.Forms.IDataObject
Public Shared Function GetDataObject () As IDataObject

返回

IDataObject

IDataObject,表示系统剪贴板中当前的数据;如果剪贴板中没有数据,则为 nullAn IDataObject that represents the data currently on the Clipboard, or null if there is no data on the Clipboard.

例外

未能从剪贴板中检索到数据。Data could not be retrieved from the Clipboard. 这种情况通常发生在剪贴板正在被其他进程使用的时候。This typically occurs when the Clipboard is being used by another process.

当前线程未处于单线程单元 (STA) 模式下,且 MessageLoop 属性值为 trueThe current thread is not in single-threaded apartment (STA) mode and the MessageLoop property value is true. 请将 STAThreadAttribute 添加到应用程序的 Main 方法中。Add the STAThreadAttribute to your application's Main method.

示例

下面的代码示例使用 Clipboard 方法将数据放置在系统剪贴板上并从中检索数据。The following code example uses Clipboard methods to place data on and retrieve it from the system Clipboard. 此代码假定 button1 button2 textBox1 已将、、和 textBox2 放置在窗体上。This code assumes button1, button2, textBox1, and textBox2 have been placed on the form.

button1_Click方法调用 SetDataObject 从文本框中选择文本并将其放入系统剪贴板。The button1_Click method calls SetDataObject to take selected text from the text box and place it on the system Clipboard.

button2_Click方法调用 GetDataObject 从系统剪贴板检索数据。The button2_Click method calls GetDataObject to retrieve data from the system Clipboard. 此代码使用 IDataObjectDataFormats 提取返回的数据。The code uses IDataObject and DataFormats to extract the data returned. 数据显示在中 textBox2The data is displayed in 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

注解

由于从剪贴板返回的对象的数据类型可能会有所不同,因此此方法返回中的数据 IDataObjectBecause the data type of the object returned from the Clipboard can vary, this method returns the data in an IDataObject. 然后,可以使用接口的方法 IDataObject ,以正确的数据类型来提取数据。Then you can use methods of the IDataObject interface to extract the data in its proper data type.

此方法尝试以100毫秒为间隔10次获取数据,并在 ExternalException 所有尝试均失败时引发。This method attempts to get the data ten times in 100-millisecond intervals, and throws an ExternalException if all attempts are unsuccessful.

备注

Clipboard类只能在设置为单线程单元 (STA) 模式的线程中使用。The Clipboard class can only be used in threads set to single thread apartment (STA) mode. 若要使用此类,请确保使用 Main 特性标记方法 STAThreadAttributeTo use this class, ensure that your Main method is marked with the STAThreadAttribute attribute.

适用于

另请参阅