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 方法将数据放在系统剪贴板上并从中检索数据。 此代码假定button1button2``textBox1``textBox2创建并放置在窗体上。

该方法 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 接口的对象返回。 使用对象 IDataObject 中指定的方法和字段 DataFormats 从对象中提取数据。 如果不知道检索到的数据的格式,请调用 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 只能在设置为单线程单元 (STA) 模式的线程中使用。 若要使用此类,请确保使用属性标记STAThreadAttribute方法Main

在剪贴板中使用图元文件格式时,可能需要特殊注意事项。 由于类的当前实现DataObject存在限制,.NET Framework使用元文件格式的应用程序可能无法识别.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 值指示的格式添加文本数据。

适用于

另请参阅