DataObject 类

实现基数据传输机制。

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

语法

声明
<ClassInterfaceAttribute(ClassInterfaceType.None)> _
Public Class DataObject
    Implements IDataObject, IDataObject
用法
Dim instance As DataObject
[ClassInterfaceAttribute(ClassInterfaceType.None)] 
public class DataObject : IDataObject, IDataObject
[ClassInterfaceAttribute(ClassInterfaceType::None)] 
public ref class DataObject : IDataObject, IDataObject
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.None) */ 
public class DataObject implements IDataObject, IDataObject
ClassInterfaceAttribute(ClassInterfaceType.None) 
public class DataObject implements IDataObject, IDataObject

备注

DataObject 实现 IDataObject 接口,该接口的方法为数据传输提供与格式无关的机制。

DataObject 通常用于 Clipboard 和拖放操作。DataObject 类提供 IDataObject 接口的建议实现。建议使用 DataObject 类,而不用自己实现 IDataObject

可将不同格式的多种数据存储在 DataObject 中。可通过与数据关联的格式从 DataObject 中检索这些数据。因为目标应用程序可能未知,所以通过将数据以多种格式放置在 DataObject 中,可使数据符合应用程序的正确格式的可能性增大。请参见 DataFormats 以获得预定义的格式。可通过创建 DataFormats.Format 类的实例来实现自己的格式。

若要在 DataObject 中存储数据,请将这些数据传递给构造函数或调用 SetData。可以使用多种格式将数据添加到同一 DataObject。如果想只以其本机格式检索添加的数据,请调用 SetData(String,Boolean,Object),将其 autoConvert 参数设置为 false

可从 DataObject 中以任何与 GetData 兼容的格式检索数据。例如,文本可被转换为 Unicode。若要按照存储数据的原格式来检索该数据,可调用 GetData,并将 autoConvert 参数设置为 false

若要确定数据存储的格式,可调用 GetFormats。若要确定一种格式是否可用,可用所需的格式来调用 GetDataPresent

在 Microsoft .NET Framework 2.0 版 中,DataObject 类可提供其他方法,使得处理常用格式的数据更加容易。若要将特定格式的数据添加到 DataObject 中,请使用适当的 SetFormat 方法,如 SetText。若要检索 DataObject 中特定格式的数据,请首先调用适当的 ContainsFormat 方法(如 ContainsText),以确定 DataObject 是否包含该格式的数据,如果 DataObject 包含该格式的数据,再调用适当的 GetFormat 方法(如 GetText)进行检索。

提示

通过剪贴板使用图元文件格式时,可能需要特别注意一些事项。由于 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”(剪贴板上的图元格式并不对于所有应用程序都是可见的)。

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

示例

下面的代码示例在 DataObject 中添加数据。首先,创建新的 DataObject,并且在其中存储一个组件。然后,进行检查以确定 DataObject 中是否存在适当类型的数据。结果显示在文本框内。此代码要求已创建了 textBox1

Private Sub AddMyData3()
    ' Creates a component to store in the data object.
    Dim myComponent As New Component()
    
    ' Creates a new data object.
    Dim myDataObject As New DataObject()
    
    ' Adds the component to the DataObject.
    myDataObject.SetData(myComponent)
    
    ' Prints whether data of the specified type is in the DataObject.
    Dim myType As Type = myComponent.GetType()
    If myDataObject.GetDataPresent(myType) Then
        textBox1.Text = "Data of type " & myType.ToString() & _
            " is present in the DataObject"
    Else
        textBox1.Text = "Data of type " & myType.ToString() & _
            " is not present in the DataObject"
    End If
End Sub 'AddMyData3
private void AddMyData3() {
    // Creates a component to store in the data object.
    Component myComponent = new Component();
 
    // Creates a new data object.
    DataObject myDataObject = new DataObject();
 
    // Adds the component to the DataObject.
    myDataObject.SetData(myComponent);
 
    // Prints whether data of the specified type is in the DataObject.
    Type myType = myComponent.GetType();
    if(myDataObject.GetDataPresent(myType))
       textBox1.Text = "Data of type " + myType.ToString() + 
       " is present in the DataObject";
    else
       textBox1.Text = "Data of type " + myType.ToString() +
       " is not present in the DataObject";
 }
private:
   void AddMyData3()
   {
      // Creates a component to store in the data object.
      Component^ myComponent = gcnew Component;
      
      // Creates a new data object.
      DataObject^ myDataObject = gcnew DataObject;
      
      // Adds the component to the DataObject.
      myDataObject->SetData( myComponent );
      
      // Prints whether data of the specified type is in the DataObject.
      Type^ myType = myComponent->GetType();
      if ( myDataObject->GetDataPresent( myType ) )
      {
         textBox1->Text = String::Concat( "Data of type ", myType,
            " is present in the DataObject" );
      }
      else
      {
         textBox1->Text = String::Concat( "Data of type ", myType,
            " is not present in the DataObject" );
      }
   }
private void AddMyData3()
{
    // Creates a component to store in the data object.
    Component myComponent = new Component();

    // Creates a new data object.
    DataObject myDataObject = new DataObject();

    // Adds the component to the DataObject.
    myDataObject.SetData(myComponent);

    // Prints whether data of the specified type is in the DataObject.
    Type myType = myComponent.GetType();

    if (myDataObject.GetDataPresent(myType)) {
        textBox1.set_Text("Data of type " + myType.ToString() 
            + " is present in the DataObject");
    }
    else {
        textBox1.set_Text("Data of type " + myType.ToString() 
            + " is not present in the DataObject");
    }
} //AddMyData3

下一个示例将检索存储在 DataObject 中的数据。首先,使用文本数据创建一个新的 DataObject。然后检索该数据,指定其格式为字符串,并且将该数据显示在一个文本框中。数据格式自动从文本转换为字符串。此代码要求已创建了 textBox1

Private Sub GetMyData2()
    ' Creates a new data object using a string and the text format.
    Dim myDataObject As New DataObject(DataFormats.Text, "Text to Store")
    
    ' Prints the string in a text box.
    textBox1.Text = myDataObject.GetData("System.String").ToString()
End Sub 'GetMyData2
private void GetMyData2() {
    // Creates a new data object using a string and the text format.
    DataObject myDataObject = new DataObject(DataFormats.Text, "Text to Store");
 
    // Prints the string in a text box.
    textBox1.Text = myDataObject.GetData("System.String").ToString();
 }
void GetMyData2()
{
   // Creates a new data object using a string and the text format.
   DataObject^ myDataObject = gcnew DataObject( DataFormats::Text,"Text to Store" );
   
   // Prints the string in a text box.
   textBox1->Text = myDataObject->GetData( "System.String" )->ToString();
}
private void GetMyData2()
{
    // Creates a new data object using a string and the text format.
    DataObject myDataObject = new DataObject(DataFormats.Text, 
        "Text to Store");

    // Prints the string in a text box.
    textBox1.set_Text(myDataObject.GetData("System.String").ToString());
} //GetMyData2

继承层次结构

System.Object
  System.Windows.Forms.DataObject

线程安全

此类型的任何公共静态(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

请参见

参考

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