Clipboard 类

提供将数据置于系统剪贴板中以及从中检索数据的方法。无法继承此类。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public NotInheritable Class Clipboard
用法
Dim instance As Clipboard
public sealed class Clipboard
public ref class Clipboard sealed
public final class Clipboard
public final class Clipboard

备注

有关用于 Clipboard 类的预定义格式列表,请参见 DataFormats 类。

调用 SetDataObject,将数据置于剪贴板中。若要将数据的一份持久性副本置于剪贴板中,可将 copy 参数设置为 true

提示

将数据以多种格式放置在剪贴板上可最大限度地确保目标应用程序(您可能不知道它的格式要求)成功地检索数据。

调用 GetDataObject,从剪贴板中检索数据。数据作为实现 IDataObject 接口的对象返回。使用 IDataObject 指定的方法和 DataFormats 中的字段从该对象中提取数据。如果不知道检索到的数据的格式,可调用 IDataObject 接口的 GetFormats 方法以获取存储数据所用的所有格式的列表。然后调用 IDataObject 接口的 GetData 方法,并指定您的应用程序可使用的格式。

在 Microsoft .NET Framework 2.0 版 中,Clipboard 类会提供附加方法,使系统剪贴板更易于使用。调用 Clear 方法,从剪贴板中移除所有数据。若要将特定格式的数据添加到剪贴板中,请调用适当的 SetFormat 方法(如 SetText),或调用 SetData 方法以指定格式。若要从剪贴板中检索特定格式的数据,请首先调用适当的 ContainsFormat 方法(如 ContainsText),以确定剪贴板中是否包含该格式的数据。如果包含,则再调用适当的 GetFormat 方法(如 GetText)来检索该数据。若要指定这些操作中的格式,可改为调用 ContainsDataGetData 方法。

提示

所有基于 Windows 的应用程序都共享系统剪贴板,所以当切换到另一个应用程序时,剪贴板的内容可能会发生变化。

对象必须是可序列化的,这样才能放在剪贴板中。如果将不可序列化的对象传递给 Clipboard 方法,该方法将失败,但不会引发异常。有关序列化的更多信息,请参见 序列化。如果目标应用程序需要特定的数据格式,则在序列化过程中为数据添加的标头可能会禁止应用程序识别数据。若要保留数据格式,请将数据作为 Byte 数组添加到 MemoryStream 中,然后将 MemoryStream 传递给 SetData 方法。

Clipboard 类只能用于设置为单线程单元 (STA) 模式的线程中。若要使用此类,请确保已使用 STAThreadAttribute 属性标记 Main 方法。

通过剪贴板使用图元文件格式时,可能需要特别注意一些事项。由于 DataObject 类的当前实现中的限制,使用较早的图元文件格式的应用程序可能不会识别由 .NET Framework 使用的图元文件格式。在这种情况下,必须与 Win32 剪贴板应用程序编程接口 (API) 进行交互。有关更多信息,请参见位于 https://support.microsoft.com/default.aspx?ln=zh-cn. 上的 Microsoft 知识库中编号为 323530 的文章“Metafiles on Clipboard Are Not Visible to All Applications”(剪贴板上的图元格式并不对于所有应用程序都是可见的)。

Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows CE 平台说明: 在 Windows CE 中,从剪贴板中粘贴的内存流可比复制到剪贴板中的内存流略大,因为会将多余的字节追加到原始内存流的末尾。若要准确检索内存流,请将对象的大小作为该对象的前缀来确定其接收方式,或将 DataObject 复制到剪贴板,其中包含该内存流和表示其大小的字符串值。

示例

下面的代码示例使用 Clipboard 方法将数据置于系统剪贴板中,并从中检索这些数据。这段代码假定 button1button2textBox1textBox2 已经创建,并已置于窗体上。

button1_Click 方法调用 SetDataObject,从文本框中提取选定的文本,然后将其置于系统剪贴板中。

button2_Click 方法调用 GetDataObject,从系统剪贴板中检索数据。这段代码使用 IDataObjectDataFormats 提取已返回的数据,并在 textBox2 中显示该数据。

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
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:
   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.get_SelectedText().Equals("")) {
        Clipboard.SetDataObject(textBox1.get_SelectedText());
    }
    else {
        textBox2.set_Text("No text selected in textBox1");
    }
} //button1_Click

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.set_Text((String)(iData.GetData(DataFormats.Text)));
    }
    else {
        // No it is not.
        textBox2.set_Text("Could not retrieve data off the clipboard.");
    }
} //button2_Click
private function button1_Click(sender : Object, e : System.EventArgs) {
    //Take the selected text from a text box and put it on the clipboard.
    if(textBox1.SelectedText != "")
       Clipboard.SetDataObject(textBox1.SelectedText);
    else
       textBox2.Text = "No text selected in textBox1";
 }
 
 private function button2_Click(sender : Object, e : System.EventArgs) {
    //Declare an IDataObject to hold the data returned from the clipboard.
    //Then retrieve the data from the clipboard.
    var iData : IDataObject = Clipboard.GetDataObject();
 
    //Determine 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.";
    }
 }
 

继承层次结构

System.Object
  System.Windows.Forms.Clipboard

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0

请参见

参考

Clipboard 成员
System.Windows.Forms 命名空间
DataObject
DataFormats
IDataObject

其他资源

Windows 窗体中额外的安全性注意事项