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. 此代码假定button1button2textBox1,和textBox2已创建并已放置在窗体上。This code assumes button1, button2, textBox1, and textBox2 have been created and 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提取返回的数据,并显示中的数据textBox2The 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 'button1_Click
 
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 'button2_Click

注解

有关一系列预定义的格式以用于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参数trueTo 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.0,则Clipboard类提供其他方法,可以更轻松地使用系统剪贴板。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,并将传递MemoryStreamSetData方法。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 的浅表副本。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)

适用于

另请参阅