Clipboard 類別

定義

提供將資料放入及從系統剪貼簿取回資料的方法。 此類別無法獲得繼承。

public ref class Clipboard sealed
public sealed class Clipboard
type Clipboard = class
Public NotInheritable 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 將資料放入夾板,替換其目前的內容。 若要將資料的持久副本放入剪貼簿,請將參數設 copytrue

Note

若要將資料以多種格式放入剪貼簿,請使用類別 DataObject 或實 IDataObject 作。 將資料以多種格式放置於剪貼簿上,以最大化目標應用程式(你可能不了解其格式需求)成功取得資料的可能性。

打電話 GetDataObject 從剪貼簿取資料。 資料會以實作 IDataObject 介面的物件形式回傳。 使用欄位IDataObjectDataFormats中指定的方法,從物件中擷取資料。 如果你不知道所取得資料的格式,請呼叫 GetFormats 介面的方法 IDataObject ,取得該資料所儲存的所有格式清單。 接著呼叫GetDataIDataObject介面的方法,並指定你的應用程式可以使用的格式。

課程 Clipboard 提供額外方法,使系統剪貼簿的操作更為簡便。 呼叫 Clear 移除剪貼簿中所有資料的方法。 若要將特定格式的資料加入剪貼簿,替換現有資料,請呼叫適當的 Set格式 方法,例如 SetText,或呼叫 SetData 該方法來指定格式。 要從剪貼簿擷取特定格式的資料,首先呼叫適當的 Contains格式 方法(例如 ContainsText)方法來判斷夾板是否包含該格式的資料,然後再呼叫適當的 Get格式 方法(例如 GetText)來擷取夾板是否包含該資料。 若要指定這些操作的格式,請呼叫 and ContainsDataGetData 方法。

Note

所有基於 Windows 的應用程式共用系統剪貼簿,因此當你切換到其他應用程式時,內容可能會有所變動。

物件必須可序列化,才能放在剪貼簿上。 如果你把一個不可序列化的物件傳給剪貼簿方法,該方法會失敗且不會拋出例外。 更多關於序號的資訊請參見 System.Runtime.Serialization 。 如果你的目標應用程式需要非常特定的資料格式,序列化過程中新增的標頭可能會阻止應用程式辨識你的資料。 為了保留資料格式,將資料加入陣Byte列到 aMemoryStream,然後傳給MemoryStreamSetData方法。

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

在使用剪貼簿的元檔案格式時,可能需要特別注意。 由於目前 DataObject 類別實作的限制,.NET 框架所使用的元檔案格式可能無法被使用較舊元檔案格式的應用程式識別。 在這種情況下,你必須與 Win32 剪貼簿應用程式介面(API)互通。

方法

名稱 Description
Clear()

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

ContainsAudio()

顯示剪貼板上是否有格式資料 WaveAudio

ContainsData(String)

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

ContainsFileDropList()

顯示剪貼簿 FileDrop 上是否有該格式的資料,或是否能轉換成該格式。

ContainsImage()

顯示剪貼簿 Bitmap 上是否有該格式的資料,或是否能轉換成該格式。

ContainsText()

顯示剪貼板 Text 上是否有 OR UnicodeText 格式的資料,視作業系統而定。

ContainsText(TextDataFormat)

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

Equals(Object)

判斷指定的 物件是否等於目前的物件。

(繼承來源 Object)
GetAudioStream()

從剪貼簿擷取音訊串流。

GetData(String)

從剪貼簿以指定格式檢索資料。

GetDataObject()

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

GetFileDropList()

從夾板中取回一組檔案名稱。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetImage()

從剪貼簿中取出一張圖片。

GetText()

依作業系統不同,從剪貼簿以 or UnicodeText 格式擷取文字資料Text

GetText(TextDataFormat)

從剪貼簿中以指定 TextDataFormat 值所示格式檢索文字資料。

GetType()

取得目前實例的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
SetAudio(Byte[])

清除剪貼簿,然後ByteWaveAudio在轉換成 Stream.

SetAudio(Stream)

清除剪貼簿,然後在格式中新增 a StreamWaveAudio

SetData(String, Object)

清除剪貼簿,然後以指定格式新增資料。

SetDataObject(Object, Boolean, Int32, Int32)

清除剪貼簿,然後嘗試在指定次數及間隔內放置資料,選擇性地在應用程式結束後將資料留在剪貼簿上。

SetDataObject(Object, Boolean)

清除剪貼簿,然後將資料放入上,並指定應用程式結束後資料是否應該保留。

SetDataObject(Object)

清除剪貼簿,然後放置非持久的資料。

SetFileDropList(StringCollection)

清除剪貼簿,然後以該 FileDrop 格式新增一組檔案名稱。

SetImage(Image)

清除剪貼簿,然後在格式中新增一個ImageBitmap

SetText(String, TextDataFormat)

清除剪貼簿,然後依指定 TextDataFormat 值格式新增文字資料。

SetText(String)

清除剪貼簿,然後依作業系統不同,以 or UnicodeText 格式新增文字資料Text

ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)

適用於

另請參閱