Clipboard 類別

定義

提供將資料放置於系統,以及從系統剪貼簿擷取資料的方法。 此類別無法獲得繼承。

public ref class Clipboard sealed
public ref class Clipboard abstract sealed
public sealed class Clipboard
public static class Clipboard
type Clipboard = class
Public NotInheritable Class Clipboard
Public Class Clipboard
繼承
Clipboard

範例

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

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

方法 button2_Click 會呼叫 GetDataObject 以從系統剪貼簿擷取資料。 程式碼會使用 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

備註

如需要與 類別搭配 Clipboard 使用之預先定義格式的清單,請參閱 類別 DataFormats

呼叫 SetDataObject 以將資料放在剪貼簿上,並取代其目前的內容。 若要在剪貼簿上放置資料的永續性複本,請將 copy 參數設定為 true

注意

若要以多種格式將資料放在剪貼簿上,請使用 DataObject 類別或 IDataObject 實作。 將資料放在剪貼簿上以多種格式,以最大化目標應用程式可能不知道其格式需求可以成功擷取資料的可能性。

呼叫 GetDataObject 以從剪貼簿擷取資料。 資料會以實作 IDataObject 介面的物件的形式傳回。 使用 中的 DataFormats 和 欄位所指定 IDataObject 的方法,從 物件擷取資料。 如果您不知道所擷取的資料格式,請呼叫 GetFormats 介面的 IDataObject 方法,以取得儲存資料的所有格式清單。 然後呼叫 GetData 介面的 IDataObject 方法,並指定您的應用程式可以使用的格式。

在 .NET Framework 2.0 中,類別 Clipboard 提供其他方法,讓您更輕鬆地使用系統剪貼簿。 Clear呼叫 方法,從剪貼簿中移除所有資料。 若要將特定格式的資料新增至剪貼簿,請取代現有的資料、呼叫適當的 SetFormat方法,例如 SetText ,或呼叫 SetData 方法來指定格式。 若要從剪貼簿擷取特定格式的資料,請先呼叫適當的 ContainsFormat方法 (例如 ContainsText) 方法,以判斷剪貼簿是否包含該格式的資料,然後呼叫適當的 GetFormat方法 (,例如 GetText) ,以在剪貼簿包含資料時擷取資料。 若要在這些作業中指定格式,請改為呼叫 ContainsDataGetData 方法。

注意

所有 Windows 應用程式都會共用系統剪貼簿,因此當您切換到另一個應用程式時,內容可能會變更。

物件必須可序列化,才能將它放在剪貼簿上。 如果您將不可序列化的物件傳遞至剪貼簿方法,方法將會失敗,而不會擲回例外狀況。 如需序列化的詳細資訊,請參閱 System.Runtime.Serialization 。 如果您的目標應用程式需要非常特定的資料格式,在序列化程式中新增至資料的標頭可能會防止應用程式辨識您的資料。 若要保留資料格式,請將您的資料新增為 Byte 陣列, MemoryStream 並將 傳遞 MemoryStreamSetData 方法。

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

搭配剪貼簿使用元檔案格式時,可能需要特殊考慮。 由於 類別目前實作 DataObject 的限制,使用舊版元檔案格式的應用程式可能無法辨識.NET Framework所使用的元檔案格式。 在此情況下,您必須與 Win32 剪貼簿應用程式程式設計介面 (API) 交互操作。

方法

Clear()

從剪貼簿中移除所有資料。

ContainsAudio()

指出剪貼簿上是否有 WaveAudio 格式的資料。

ContainsData(String)

指出剪貼簿上是否有指定格式的資料,或是可以轉換為該格式的資料。

ContainsFileDropList()

指出剪貼簿上是否有 FileDrop 格式的資料,或是可以轉換為該格式的資料。

ContainsImage()

指出剪貼簿上是否有 Bitmap 格式的資料,或是可以轉換為該格式的資料。

ContainsText()

指出剪貼簿上是否有 TextUnicodeText 格式的資料,格式依作業系統而定。

ContainsText(TextDataFormat)

指出剪貼簿上是否有由所指定 TextDataFormat 值代表之格式的文字資料。

GetAudioStream()

從剪貼簿擷取音效串流。

GetData(String)

從剪貼簿擷取指定格式的資料。

GetDataObject()

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

GetFileDropList()

從剪貼簿擷取檔名稱的集合。

GetImage()

從剪貼簿擷取影像。

GetText()

從剪貼簿擷取 TextUnicodeText 格式的文字資料,格式依作業系統而定。

GetText(TextDataFormat)

從剪貼簿擷取以所指定 TextDataFormat 值表示之格式的文字資料。

SetAudio(Byte[])

清除剪貼簿,並將 Byte 陣列轉換為 WaveAudio,然後再以 Stream 格式加入至剪貼簿。

SetAudio(Stream)

清除剪貼簿,然後加入 Stream 格式的 WaveAudio

SetData(String, Object)

清除剪貼簿,然後加入指定之格式的資料。

SetDataObject(Object)

清除剪貼簿,然後將非永續性資料置於其中。

SetDataObject(Object, Boolean)

清除 [剪貼簿],然後將資料放置在 [剪貼簿] 上,並指定在應用程式結束之後,是否應保留資料。

SetDataObject(Object, Boolean, Int32, Int32)

清除 [剪貼簿],然後嘗試以指定的次數將資料放置於 [剪貼簿] 上,而且在兩次嘗試之間採用指定的延遲,同時可以選擇在應用程式結束以後,將資料保留在 [剪貼簿] 上。

SetFileDropList(StringCollection)

清除剪貼簿,然後加入 FileDrop 格式的檔案名稱集合。

SetImage(Image)

清除剪貼簿,然後加入 Image 格式的 Bitmap

SetText(String)

清除剪貼簿,然後依作業系統而定加入 TextUnicodeText 格式的文字資料。

SetText(String, TextDataFormat)

清除剪貼簿,然後加入指定之 TextDataFormat 值所表示格式的文字資料。

適用於

另請參閱