DataObject 类

定义

实现基数据传输机制。Implements a basic data transfer mechanism.

public ref class DataObject : System::Runtime::InteropServices::ComTypes::IDataObject, System::Windows::Forms::IDataObject
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
public class DataObject : System.Runtime.InteropServices.ComTypes.IDataObject, System.Windows.Forms.IDataObject
type DataObject = class
    interface IDataObject
    interface IDataObject
Public Class DataObject
Implements IDataObject, IDataObject
继承
DataObject
属性
实现

示例

下面的代码示例在 DataObject中添加数据。The following code example adds data in a DataObject. 首先,将创建一个新的 DataObject,并在其中存储一个组件。First, a new DataObject is created and a component is stored in it. 然后,它会检查 DataObject中是否存在适当类型的数据。Then, it checks to see if data in the appropriate type exists in the DataObject. 结果将显示在文本框中。The result is displayed in a text box. 此代码要求已创建 textBox1This code requires that textBox1 has been created.

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.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 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

下一个示例检索存储在 DataObject中的数据。The next example retrieves the data stored in a DataObject. 首先,创建一个包含文本数据的新 DataObjectFirst, a new DataObject is created with text data. 然后,检索数据,将其格式指定为字符串,并在文本框中显示。Then, the data is retrieved, specifying its format as a string, and displayed in a text box. 数据格式将自动从文本转换为字符串。The data format is automatically converted from text to string. 此代码要求已创建 textBox1This code requires that textBox1 has been created.

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( DataFormats::Text )->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.Text = myDataObject.GetData(DataFormats.Text).ToString();
 }
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(DataFormats.Text).ToString()
End Sub

注解

DataObject 实现 IDataObject 接口,该接口的方法为数据传输提供与格式无关的机制。DataObject implements the IDataObject interface, whose methods provide a format-independent mechanism for data transfer.

DataObject 通常与 Clipboard 和拖放操作一起使用。A DataObject is typically used with the Clipboard and in drag-and-drop operations. DataObject 类提供 IDataObject 接口的建议实现。The DataObject class provides the recommended implementation of the IDataObject interface. 建议使用 DataObject 类,而不是自行实现 IDataObjectIt is suggested that you use the DataObject class rather than implementing IDataObject yourself.

不同格式的多个数据片段可以存储在 DataObject中。Multiple pieces of data in different formats can be stored in a DataObject. 数据通过其关联格式从 DataObject 检索。Data is retrieved from a DataObject by its associated format. 由于目标应用程序可能未知,因此,您可以通过将数据放在多个格式的 DataObject 中,来增加应用程序格式的数据的可能性。Because the target application might not be known, you can increase the likelihood that the data will be in the appropriate format for an application by placing the data in a DataObject in multiple formats. 请参阅 DataFormats,了解预定义格式。See DataFormats for the predefined formats. 通过创建 DataFormats.Format 类的实例,可以实现自己的格式。You can implement your own format by creating an instance of the DataFormats.Format class.

若要在 DataObject中存储数据,请将数据传递给构造函数或调用 SetDataTo store data in a DataObject, pass the data to the constructor or call SetData. 可以将多种格式的数据添加到相同的 DataObjectYou can add data in multiple formats to the same DataObject. 如果希望添加的数据仅以本机格式检索,请调用 SetData(String, Boolean, Object),并将 autoConvert 参数设置为 falseIf you want the data you add to be retrieved in its native format only, call SetData(String, Boolean, Object) with the autoConvert parameter set to false.

可以从 DataObject 以与 GetData兼容的任何格式检索数据。Data can be retrieved from a DataObject in any format which is compatible with GetData. 例如,可以将文本转换为 Unicode。For example, text can be converted to Unicode. 若要以存储数据的格式检索数据,请调用 GetData,并将 autoConvert 参数设置为 falseTo retrieve data in the format in which it was stored, call GetData with the autoConvert parameter set to false.

若要确定数据存储的格式,请调用 GetFormatsTo determine what formats the data is stored in, call GetFormats. 若要确定格式是否可用,请使用所需的格式调用 GetDataPresentTo determine if a format is available, call GetDataPresent with the desired format.

.NET Framework 2.0.NET Framework 2.0中,DataObject 类提供附加方法,使您可以更轻松地处理常见格式的数据。In .NET Framework 2.0.NET Framework 2.0, the DataObject class provides additional methods that make it easier to work with data in common formats. 若要将特定格式的数据添加到 DataObject,请使用适当的 Set格式方法,如 SetTextTo add data of a particular format to the DataObject, use the appropriate SetFormat method, such as SetText. 若要从 DataObject检索特定格式的数据,请首先调用适当的 Contains格式方法(如 ContainsText)以确定 DataObject 是否包含该格式的数据,然后调用适当的 Get格式如果 DataObject 包含数据,则方法(如 GetText)检索数据。To retrieve data of a particular format from the DataObject, first call the appropriate ContainsFormat method (such as ContainsText) to determine whether the DataObject contains data in that format, then call the appropriate GetFormat method (such as GetText) to retrieve the data if the DataObject contains it.

备注

使用带有剪贴板的图元文件格式时,可能需要特别注意。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).

对象必须是可序列化的,才能将其放在剪贴板上。An object must be serializable for it to be put on the Clipboard. 有关序列化的详细信息,请参阅 System.Runtime.SerializationSee 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 并将 MemoryStream 传递到 SetData 方法。To preserve your data format, add your data as a Byte array to a MemoryStream and pass the MemoryStream to the SetData method.

构造函数

DataObject()

初始化 DataObject 类的新实例。Initializes a new instance of the DataObject class.

DataObject(Object)

初始化 DataObject 类的新实例,并为其添加指定的对象。Initializes a new instance of the DataObject class and adds the specified object to it.

DataObject(String, Object)

初始化 DataObject 类的新实例,并以指定格式为其添加指定的对象。Initializes a new instance of the DataObject class and adds the specified object in the specified format.

方法

ContainsAudio()

指示此数据对象是否包含 WaveAudio 格式的数据。Indicates whether the data object contains data in the WaveAudio format.

ContainsFileDropList()

指示此数据对象是否包含 FileDrop 格式的数据,或者是否包含可以转换为该格式的数据。Indicates whether the data object contains data that is in the FileDrop format or can be converted to that format.

ContainsImage()

指示此数据对象是否包含 Bitmap 格式的数据,或者是否包含可以转换为该格式的数据。Indicates whether the data object contains data that is in the Bitmap format or can be converted to that format.

ContainsText()

指示此数据对象是否包含 UnicodeText 格式的数据。Indicates whether the data object contains data in the UnicodeText format.

ContainsText(TextDataFormat)

指示此数据对象是否包含由指定的 TextDataFormat 值表示的格式的文本数据。Indicates whether the data object contains text data 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 data object.

GetData(String)

返回与所指定数据格式关联的数据。Returns the data associated with the specified data format.

GetData(String, Boolean)

返回与所指定数据格式关联的数据,使用自动转换参数来确定是否将数据转换为此格式。Returns the data associated with the specified data format, using an automated conversion parameter to determine whether to convert the data to the format.

GetData(Type)

返回与所指定类类型格式关联的数据。Returns the data associated with the specified class type format.

GetDataPresent(String)

确定此 DataObject 中存储的数据是否与指定的格式关联,或是否可以转换成指定的格式。Determines whether data stored in this DataObject is associated with, or can be converted to, the specified format.

GetDataPresent(String, Boolean)

确定此 DataObject 是否包含指定格式的数据,或者是否包含可以转换为指定格式的数据,后者可选。Determines whether this DataObject contains data in the specified format or, optionally, contains data that can be converted to the specified format.

GetDataPresent(Type)

确定此 DataObject 中存储的数据是否与指定的格式关联,或是否可以转换成指定的格式。Determines whether data stored in this DataObject is associated with, or can be converted to, the specified format.

GetFileDropList()

检索此数据对象中文件名的集合。Retrieves a collection of file names from the data object.

GetFormats()

返回存储在此 DataObject 中的数据所关联的或可以转换为的所有格式的列表。Returns a list of all formats that data stored in this DataObject is associated with or can be converted to.

GetFormats(Boolean)

返回存储在此 DataObject 中的数据所关联的或可以转换为的所有格式的列表,同时使用自动转换参数来确定是只检索本机数据格式,还是检索此数据可以转换为的所有格式。Returns a list of all formats that data stored in this DataObject is associated with or can be converted to, using an automatic conversion parameter to determine whether to retrieve only native data formats or all formats that the data can be converted to.

GetHashCode()

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

(继承自 Object)
GetImage()

检索此数据对象中的图像。Retrieves an image from the data object.

GetText()

检索此数据对象中 UnicodeText 格式的文本数据。Retrieves text data from the data object in the UnicodeText format.

GetText(TextDataFormat)

检索此数据对象中由指定的 TextDataFormat 值表示的格式的文本数据。Retrieves text data from the data object 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 格式添加到此数据对象。Adds a Byte array to the data object in the WaveAudio format after converting it to a Stream.

SetAudio(Stream)

StreamWaveAudio 格式添加到此数据对象。Adds a Stream to the data object in the WaveAudio format.

SetData(Object)

使用对象类型作为数据格式将指定对象添加到 DataObjectAdds the specified object to the DataObject using the object type as the data format.

SetData(String, Boolean, Object)

使用指定格式将指定对象添加到 DataObject 并指示这些数据是否可以转换为其他格式。Adds the specified object to the DataObject using the specified format and indicating whether the data can be converted to another format.

SetData(String, Object)

使用指定格式将指定对象添加到 DataObjectAdds the specified object to the DataObject using the specified format.

SetData(Type, Object)

使用指定类型作为格式将指定对象添加到 DataObjectAdds the specified object to the DataObject using the specified type as the format.

SetFileDropList(StringCollection)

将文件名的集合以 FileDrop 格式添加到此数据对象。Adds a collection of file names to the data object in the FileDrop format.

SetImage(Image)

ImageBitmap 格式添加到此数据对象。Adds an Image to the data object in the Bitmap format.

SetText(String)

将文本数据以 UnicodeText 格式添加到此数据对象。Adds text data to the data object in the UnicodeText format.

SetText(String, TextDataFormat)

将文本数据以指定的 TextDataFormat 值表示的格式添加到此数据对象。Adds text data to the data object in the format indicated by the specified TextDataFormat value.

ToString()

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

(继承自 Object)

显式界面实现

IDataObject.DAdvise(FORMATETC, ADVF, IAdviseSink, Int32)

在数据对象和通知接收器之间创建连接。Creates a connection between a data object and an advisory sink. 此方法由对象进行调用,该对象支持通知接收器并使通知接收器能收到有关对象数据中的更改的通知。This method is called by an object that supports an advisory sink and enables the advisory sink to be notified of changes in the object's data.

IDataObject.DUnadvise(Int32)

销毁以前建立的通知连接。Destroys a notification connection that had been previously established.

IDataObject.EnumDAdvise(IEnumSTATDATA)

创建可用于对当前的通知连接进行枚举的对象。Creates an object that can be used to enumerate the current advisory connections.

IDataObject.EnumFormatEtc(DATADIR)

创建一个用于枚举数据对象的 FORMATETC 结构的对象。Creates an object for enumerating the FORMATETC structures for a data object. 这些结构用于对 GetData(FORMATETC, STGMEDIUM)SetData(FORMATETC, STGMEDIUM, Boolean) 的调用中。These structures are used in calls to GetData(FORMATETC, STGMEDIUM) or SetData(FORMATETC, STGMEDIUM, Boolean).

IDataObject.GetCanonicalFormatEtc(FORMATETC, FORMATETC)

提供一个在逻辑上与更复杂的结构等效的标准 FORMATETC 结构。Provides a standard FORMATETC structure that is logically equivalent to a more complex structure. 使用此方法可以确定两个不同的 FORMATETC 结构是否将返回相同的数据,从而无需重复呈现。Use this method to determine whether two different FORMATETC structures would return the same data, removing the need for duplicate rendering.

IDataObject.GetData(FORMATETC, STGMEDIUM)

从源数据对象中获取数据。Obtains data from a source data object. 数据使用者调用的 GetData(FORMATETC, STGMEDIUM) 方法将呈现指定的 FORMATETC 结构中描述的数据,并通过指定的 STGMEDIUM 结构传输该数据。The GetData(FORMATETC, STGMEDIUM) method, which is called by a data consumer, renders the data described in the specified FORMATETC structure and transfers it through the specified STGMEDIUM structure. 然后,调用方对释放 STGMEDIUM 结构的责任进行假定。The caller then assumes responsibility for releasing the STGMEDIUM structure.

IDataObject.GetDataHere(FORMATETC, STGMEDIUM)

从源数据对象中获取数据。Obtains data from a source data object. 数据使用者调用的此方法与 GetData(FORMATETC, STGMEDIUM) 方法的区别在于调用方必须分配并释放指定的存储介质。This method, which is called by a data consumer, differs from the GetData(FORMATETC, STGMEDIUM) method in that the caller must allocate and free the specified storage medium.

IDataObject.QueryGetData(FORMATETC)

确定数据对象是否能够呈现 FORMATETC 结构中所描述的数据。Determines whether the data object is capable of rendering the data described in the FORMATETC structure. 尝试执行粘贴或删除操作的对象可以在调用 GetData(FORMATETC, STGMEDIUM) 来获取操作是否成功的指示之前调用此方法。Objects attempting a paste or drop operation can call this method before calling GetData(FORMATETC, STGMEDIUM) to get an indication of whether the operation may be successful.

IDataObject.SetData(FORMATETC, STGMEDIUM, Boolean)

将数据传输至实现此方法的对象。Transfers data to the object that implements this method. 此方法由包含数据源的对象调用。This method is called by an object that contains a data source.

适用于

另请参阅