Clipboard.GetDataObject 方法

定義

擷取目前在系統剪貼簿中的資料。

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.IDataObject
Public Shared Function GetDataObject () As IDataObject

傳回

IDataObject

IDataObject,表示目前在剪貼簿中的資料,或者如果剪貼簿中沒有資料,則為 null

例外狀況

無法從剪貼簿擷取資料。 這通常在剪貼簿由另一個處理序使用時發生。

目前的執行緒不處於單一執行緒 Apartment (STA) 模式,而且 MessageLoop 屬性值為 true。 將 STAThreadAttribute 加入至應用程式的 Main 方法。

範例

下列程式碼範例會使用 Clipboard 方法來將資料放在上,並從系統剪貼簿擷取資料。 此程式碼假設 button1button2textBox1textBox2 已放在表單上。

方法 button1_Click 會呼叫 SetDataObject 以從文字方塊中取得選取的文字,並將它放在系統剪貼簿上。

方法會呼叫 GetDataObject ,以從系統剪 button2_Click 貼簿擷取資料。 程式碼會使用 IDataObjectDataFormats 來擷取傳回的資料。 資料會顯示在 中 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 毫秒間隔取得資料十次,如果所有嘗試都失敗,則會 ExternalException 擲回 。

注意

類別 Clipboard 只能用於設定為單一線程 Apartment (STA) 模式的執行緒。 若要使用此類別,請確定您的 Main 方法已以 STAThreadAttribute 屬性標示。

適用於

另請參閱