Clipboard 类

定义

提供将数据置于系统剪贴板中以及从中检索数据的方法。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
继承
Clipboard

示例

下面的代码示例使用Clipboard方法将数据放置在系统剪贴板上并从中检索数据。The following code example uses Clipboard methods to place data on and retrieve it from the system Clipboard. 此代码假定button1已创建textBox1、、 textBox2和, button2并将其放置在窗体上。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提取返回的数据, 并在中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参数设置为。 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.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到, SetData并将传递MemoryStream给方法。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()

从剪贴板中移除所有数据。Removes all data from the Clipboard.

ContainsAudio()

指示在剪贴板中是否存在 WaveAudio 格式的数据。Indicates whether there is data on the Clipboard in the WaveAudio format.

ContainsData(String)

指示剪贴板中是否存在指定格式的数据,或可转换成此格式的数据。Indicates whether there is data on the Clipboard that is in the specified format or can be converted to that format.

ContainsFileDropList()

指示剪贴板中是否存在 FileDrop 格式或可转换成此格式的数据。Indicates whether there is data on the Clipboard that is in the FileDrop format or can be converted to that format.

ContainsImage()

指示剪贴板中是否存在 Bitmap 格式或可转换成此格式的数据。Indicates whether there is data on the Clipboard that is in the Bitmap format or can be converted to that format.

ContainsText()

指示剪贴板中是否存在 TextUnicodeText 格式的数据(取决于操作系统)。Indicates whether there is data on the Clipboard in the Text or UnicodeText format, depending on the operating system.

ContainsText(TextDataFormat)

指示剪贴板中是否存在具有指定的 TextDataFormat 值所指示的格式的文本数据。Indicates whether there is text data on the Clipboard in the format indicated by the specified TextDataFormat value.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetAudioStream()

检索剪贴板上的音频流。Retrieves an audio stream from the Clipboard.

GetData(String)

从剪贴板中检索指定格式的数据。Retrieves data from the Clipboard in the specified format.

GetDataObject()

检索当前位于系统剪贴板中的数据。Retrieves the data that is currently on the system Clipboard.

GetFileDropList()

从剪贴板中检索文件名的集合。Retrieves a collection of file names from the Clipboard.

GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetImage()

检索剪贴板上的图像。Retrieves an image from the Clipboard.

GetText()

从剪贴板中检索 TextUnicodeText 格式的文本数据(取决于操作系统)。Retrieves text data from the Clipboard in the Text or UnicodeText format, depending on the operating system.

GetText(TextDataFormat)

从剪贴板中检索由指定的 TextDataFormat 值表示的格式的文本数据。Retrieves text data from the Clipboard in the format indicated by the specified TextDataFormat value.

GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
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)

清除剪贴板然后以 Stream 格式添加 WaveAudioClears the Clipboard and then adds a Stream in the WaveAudio format.

SetData(String, Object)

清除剪贴板然后以所指定的格式添加数据。Clears the Clipboard and then adds data in the specified format.

SetDataObject(Object)

清除剪贴板然后,然后将非持久性数据置于其中。Clears the Clipboard and then places nonpersistent data on it.

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)

清除剪贴板并尝试指定的次数,以将数据置于系统剪贴板中,且两次尝试之间具有指定的延迟,可以选择在退出应用程序后将数据保留在剪贴板中。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)

清除剪贴板,然后添加 FileDrop 格式中的文件名集合。Clears the Clipboard and then adds a collection of file names in the FileDrop format.

SetImage(Image)

清除剪贴板然后以 Image 格式添加 BitmapClears the Clipboard and then adds an Image in the Bitmap format.

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)

除剪贴板然后,然后以所指定 TextDataFormat 值指示的格式添加文本数据。Clears the Clipboard and then adds text data in the format indicated by the specified TextDataFormat value.

ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

适用于

另请参阅