Clipboard Clipboard Clipboard Clipboard Class

定義

提供將資料放置於系統,以及從系統剪貼簿擷取資料的方法。Provides methods to place data on and retrieve data from the system Clipboard. 這個類別無法被繼承。This class cannot be inherited.

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

範例

下列程式碼範例會Clipboard使用方法來放置資料, 並從系統剪貼簿取出。The following code example uses Clipboard methods to place data on and retrieve it from the system Clipboard. 此程式碼button1假設button2textBox1建立、 textBox2 、和, 並將其放置在表單上。This code assumes button1, button2, textBox1, and textBox2 have been created and placed on the form.

方法會呼叫SetDataObject以從文字方塊中選取文字, 並將它放在系統剪貼簿上。 button1_ClickThe button1_Click method calls SetDataObject to take selected text from the text box and place it on the system Clipboard.

方法會呼叫GetDataObject以從系統剪貼簿取出資料。 button2_ClickThe button2_Click method calls GetDataObject to retrieve data from the system Clipboard. 程式碼會IDataObject使用DataFormats和來解壓縮傳回的資料, 並在中textBox2顯示資料。The code uses IDataObject and DataFormats to extract the data returned and displays the data 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

備註

如需搭配Clipboard類別使用的預先定義格式清單, DataFormats請參閱類別。For a list of predefined formats to use with the Clipboard class, see the DataFormats class.

呼叫SetDataObject以將資料放在剪貼簿上, 並取代其目前的內容。Call SetDataObject to put data on the Clipboard, replacing its current contents. 若要將資料的持續複本放在剪貼簿上, 請copy將參數true設定為。To place a persistent copy of the data on the Clipboard, set the copy parameter to true.

注意

若要以多種格式將資料放在剪貼簿上DataObject , 請使用IDataObject類別或執行。To place data on the Clipboard in multiple formats, use the DataObject class or an IDataObject implementation. 以多種格式將資料放在剪貼簿上, 以最大化目標應用程式 (其格式需求您可能不知道) 可以成功取出資料的可能性。Place data on the Clipboard in multiple formats to maximize the possibility that a target application, whose format requirements you might not know, can successfully retrieve the data.

呼叫GetDataObject以從剪貼簿取出資料。Call GetDataObject to retrieve data from the Clipboard. 資料會當做執行IDataObject介面的物件傳回。The data is returned as an object that implements the IDataObject interface. 使用中IDataObject DataFormats的和欄位所指定的方法, 從物件解壓縮資料。Use the methods specified by IDataObject and fields in DataFormats to extract the data from the object. 如果您不知道所抓取資料的格式, 請呼叫GetFormats IDataObject介面的方法, 以取得資料儲存所在之所有格式的清單。If you do not know the format of the data you retrieved, call the GetFormats method of the IDataObject interface to get a list of all formats that data is stored in. 然後呼叫GetData IDataObject介面的方法, 並指定您的應用程式可以使用的格式。Then call the GetData method of the IDataObject interface, and specify a format that your application can use.

.NET Framework 2.0.NET Framework 2.0Clipboard , 類別會提供額外的方法, 讓您更輕鬆地使用系統剪貼簿。In .NET Framework 2.0.NET Framework 2.0, the Clipboard class provides additional methods that make it easier to work with the system Clipboard. Clear呼叫方法, 以移除剪貼簿中的所有資料。Call the Clear method to remove all data from the Clipboard. 若要將特定格式的資料加入至剪貼簿, 並取代現有的資料, 請Set呼叫適當的格式 SetText方法 (例如), SetData或呼叫方法來指定格式。To add data of a particular format to the Clipboard, replacing the existing data, call the appropriate SetFormat method, such as SetText, or call the SetData method to specify the format. 若要從剪貼簿抓取特定格式的資料, 請先呼叫適當Contains格式 ContainsText方法 (例如) 方法, 以判斷剪貼簿是否包含該格式的資料, 然後呼叫適當Get格式方法 ( GetText例如), 以在剪貼簿中包含資料時, 取得資料。To retrieve data of a particular format from the Clipboard, first call the appropriate ContainsFormat method (such as ContainsText) method to determine whether the Clipboard contains data in that format, and then call the appropriate GetFormat method (such as GetText) to retrieve the data if the Clipboard contains it. 若要在這些作業中指定格式, 請ContainsDataGetData為呼叫和方法。To specify the format in these operations, call the ContainsData and GetData methods instead.

注意

所有 Windows 應用程式都會共用系統剪貼簿, 因此當您切換到另一個應用程式時, 內容可能會變更。All Windows-based applications share the system Clipboard, so the contents are subject to change when you switch to another application.

物件必須是可序列化的, 才能放在剪貼簿上。An object must be serializable for it to be put on the Clipboard. 如果您將不可序列化的物件傳遞至剪貼簿方法, 此方法將會失敗, 而不會擲回例外狀況。If you pass a non-serializable object to a Clipboard method, the method will fail without throwing an exception. System.Runtime.Serialization需序列化的詳細資訊, 請參閱。See System.Runtime.Serialization for more information on serialization. 如果您的目標應用程式需要非常特定的資料格式, 則在序列化程式中加入資料的標頭可能會讓應用程式無法辨識您的資料。If your target application requires a very specific data format, the headers added to the data in the serialization process may prevent the application from recognizing your data. 若要保留您的資料格式, 請將您Byte的資料當做MemoryStream陣列加入至MemoryStream , SetData並將傳遞至方法。To preserve your data format, add your data as a Byte array to a MemoryStream and pass the MemoryStream to the SetData method.

Clipboard類別只能用在設定為單一執行緒單元 (STA) 模式的執行緒中。The Clipboard class can only be used in threads set to single thread apartment (STA) mode. 若要使用這個類別, 請確定Main您的方法是STAThreadAttribute以屬性標記。To use this class, ensure that your Main method is marked with the STAThreadAttribute attribute.

搭配使用元檔案格式與剪貼簿時, 可能需要特殊考慮。Special considerations may be necessary when using the metafile format with the Clipboard. 由於目前的DataObject類別實作為限制, 使用舊版元檔案格式的應用程式可能無法辨識 .NET Framework 所使用的元檔案格式。Due to a limitation in the current implementation of the DataObject class, the metafile format used by the .NET Framework may not be recognized by applications that use an older metafile format. 在此情況下, 您必須與 Win32 剪貼簿應用程式開發介面 (Api) 相交互操作。In this case, you must interoperate with the Win32 Clipboard application programming interfaces (APIs).

方法

Clear() Clear() Clear() Clear()

從剪貼簿中移除所有資料。Removes all data from the Clipboard.

ContainsAudio() ContainsAudio() ContainsAudio() ContainsAudio()

指出剪貼簿上是否有 WaveAudio 格式的資料。Indicates whether there is data on the Clipboard in the WaveAudio format.

ContainsData(String) ContainsData(String) ContainsData(String) ContainsData(String)

指出剪貼簿上是否有指定格式的資料,或是可以轉換為該格式的資料。Indicates whether there is data on the Clipboard that is in the specified format or can be converted to that format.

ContainsFileDropList() ContainsFileDropList() ContainsFileDropList() ContainsFileDropList()

指出剪貼簿上是否有 FileDrop 格式的資料,或是可以轉換為該格式的資料。Indicates whether there is data on the Clipboard that is in the FileDrop format or can be converted to that format.

ContainsImage() ContainsImage() ContainsImage() ContainsImage()

指出剪貼簿上是否有 Bitmap 格式的資料,或是可以轉換為該格式的資料。Indicates whether there is data on the Clipboard that is in the Bitmap format or can be converted to that format.

ContainsText() ContainsText() ContainsText() ContainsText()

指出剪貼簿上是否有 TextUnicodeText 格式的資料,格式依作業系統而定。Indicates whether there is data on the Clipboard in the Text or UnicodeText format, depending on the operating system.

ContainsText(TextDataFormat) ContainsText(TextDataFormat) ContainsText(TextDataFormat) ContainsText(TextDataFormat)

指出剪貼簿上是否有由所指定 TextDataFormat 值代表之格式的文字資料。Indicates whether there is text data on the Clipboard in the format indicated by the specified TextDataFormat value.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetAudioStream() GetAudioStream() GetAudioStream() GetAudioStream()

從剪貼簿擷取音效串流。Retrieves an audio stream from the Clipboard.

GetData(String) GetData(String) GetData(String) GetData(String)

從剪貼簿擷取指定格式的資料。Retrieves data from the Clipboard in the specified format.

GetDataObject() GetDataObject() GetDataObject() GetDataObject()

擷取目前在系統剪貼簿中的資料。Retrieves the data that is currently on the system Clipboard.

GetFileDropList() GetFileDropList() GetFileDropList() GetFileDropList()

從剪貼簿擷取檔名稱的集合。Retrieves a collection of file names from the Clipboard.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetImage() GetImage() GetImage() GetImage()

從剪貼簿擷取影像。Retrieves an image from the Clipboard.

GetText() GetText() GetText() GetText()

從剪貼簿擷取 TextUnicodeText 格式的文字資料,格式依作業系統而定。Retrieves text data from the Clipboard in the Text or UnicodeText format, depending on the operating system.

GetText(TextDataFormat) GetText(TextDataFormat) GetText(TextDataFormat) GetText(TextDataFormat)

從剪貼簿擷取以所指定 TextDataFormat 值表示之格式的文字資料。Retrieves text data from the Clipboard in the format indicated by the specified TextDataFormat value.

GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
SetAudio(Byte[]) SetAudio(Byte[]) SetAudio(Byte[]) SetAudio(Byte[])

清除剪貼簿,並將 Byte 陣列轉換為 WaveAudio,然後再以 Stream 格式加入至剪貼簿。Clears the Clipboard and then adds a Byte array in the WaveAudio format after converting it to a Stream.

SetAudio(Stream) SetAudio(Stream) SetAudio(Stream) SetAudio(Stream)

清除剪貼簿,然後加入 Stream 格式的 WaveAudioClears the Clipboard and then adds a Stream in the WaveAudio format.

SetData(String, Object) SetData(String, Object) SetData(String, Object) SetData(String, Object)

清除剪貼簿,然後加入指定之格式的資料。Clears the Clipboard and then adds data in the specified format.

SetDataObject(Object) SetDataObject(Object) SetDataObject(Object) SetDataObject(Object)

清除剪貼簿,然後將非永續性資料置於其中。Clears the Clipboard and then places nonpersistent data on it.

SetDataObject(Object, Boolean) SetDataObject(Object, Boolean) SetDataObject(Object, Boolean) SetDataObject(Object, Boolean)

清除 [剪貼簿],然後將資料放置在 [剪貼簿] 上,並指定在應用程式結束之後,是否應保留資料。Clears the Clipboard and then places data on it and specifies whether the data should remain after the application exits.

SetDataObject(Object, Boolean, Int32, Int32) SetDataObject(Object, Boolean, Int32, Int32) SetDataObject(Object, Boolean, Int32, Int32) SetDataObject(Object, Boolean, Int32, Int32)

清除 [剪貼簿],然後嘗試以指定的次數將資料放置於 [剪貼簿] 上,而且在兩次嘗試之間採用指定的延遲,同時可以選擇在應用程式結束以後,將資料保留在 [剪貼簿] 上。Clears the Clipboard and then attempts to place data on it the specified number of times and with the specified delay between attempts, optionally leaving the data on the Clipboard after the application exits.

SetFileDropList(StringCollection) SetFileDropList(StringCollection) SetFileDropList(StringCollection) SetFileDropList(StringCollection)

清除剪貼簿,然後加入 FileDrop 格式的檔案名稱集合。Clears the Clipboard and then adds a collection of file names in the FileDrop format.

SetImage(Image) SetImage(Image) SetImage(Image) SetImage(Image)

清除剪貼簿,然後加入 Image 格式的 BitmapClears the Clipboard and then adds an Image in the Bitmap format.

SetText(String) SetText(String) SetText(String) SetText(String)

清除剪貼簿,然後依作業系統而定加入 TextUnicodeText 格式的文字資料。Clears the Clipboard and then adds text data in the Text or UnicodeText format, depending on the operating system.

SetText(String, TextDataFormat) SetText(String, TextDataFormat) SetText(String, TextDataFormat) SetText(String, TextDataFormat)

清除剪貼簿,然後加入指定之 TextDataFormat 值所表示格式的文字資料。Clears the Clipboard and then adds text data in the format indicated by the specified TextDataFormat value.

ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

適用於

另請參閱