DataObject DataObject DataObject DataObject Class

定义

实现基数据传输机制。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
继承
DataObjectDataObjectDataObjectDataObject
属性
实现

示例

下面的代码示例添加数据DataObjectThe following code example adds data in a DataObject. 首先,新DataObject创建并在其中存储一个组件。First, a new DataObject is created and a component is stored in it. 然后,它检查中的相应类型的数据是否存在于DataObjectThen, it checks to see if data in the appropriate type exists in the DataObject. 在文本框中显示结果。The result is displayed in a text box. 此代码需要textBox1已创建。This 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 'AddMyData3

下一步的示例检索中存储的数据DataObjectThe next example retrieves the data stored in a DataObject. 首先,新DataObject使用文本数据创建。First, 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. 此代码需要textBox1已创建。This 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 'GetMyData2

注解

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类,而无需实现IDataObject自己。It is suggested that you use the DataObject class rather than implementing IDataObject yourself.

多个以不同格式的数据可以存储在DataObjectMultiple 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与兼容的任何格式GetDataData can be retrieved from a DataObject in any format which is compatible with GetData. 例如,文本可以转换为 Unicode。For example, text can be converted to Unicode. 若要检索已存储的格式中的数据,请调用GetDataautoConvert参数设置为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. 若要确定一种格式是否可用,请调用GetDataPresent所需的格式。To 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格式方法 (如GetText) 来检索数据,如果DataObject包含它。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.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,并将传递MemoryStreamSetData方法。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() DataObject() DataObject()

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

DataObject(Object) DataObject(Object) DataObject(Object) DataObject(Object)

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

DataObject(String, Object) DataObject(String, Object) DataObject(String, Object) DataObject(String, Object)

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

方法

ContainsAudio() ContainsAudio() ContainsAudio() ContainsAudio()

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

ContainsFileDropList() ContainsFileDropList() ContainsFileDropList() ContainsFileDropList()

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

ContainsImage() ContainsImage() ContainsImage() ContainsImage()

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

ContainsText() ContainsText() ContainsText() ContainsText()

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

ContainsText(TextDataFormat) ContainsText(TextDataFormat) ContainsText(TextDataFormat) ContainsText(TextDataFormat)

指示数据对象是否包含表示由指定的格式的文本数据TextDataFormat值。Indicates whether the data object contains text data in the format indicated by the specified TextDataFormat value.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

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

(Inherited from Object)
GetAudioStream() GetAudioStream() GetAudioStream() GetAudioStream()

检索此数据对象中的音频流。Retrieves an audio stream from the data object.

GetData(String) GetData(String) GetData(String) GetData(String)

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

GetData(String, Boolean) GetData(String, Boolean) GetData(String, Boolean) 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) GetData(Type) GetData(Type) GetData(Type)

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

GetDataPresent(String) GetDataPresent(String) GetDataPresent(String) GetDataPresent(String)

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

GetDataPresent(String, Boolean) GetDataPresent(String, Boolean) GetDataPresent(String, Boolean) 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) GetDataPresent(Type) GetDataPresent(Type) GetDataPresent(Type)

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

GetFileDropList() GetFileDropList() GetFileDropList() GetFileDropList()

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

GetFormats() GetFormats() GetFormats() GetFormats()

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

GetFormats(Boolean) GetFormats(Boolean) GetFormats(Boolean) 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() GetHashCode() GetHashCode() GetHashCode()

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

(Inherited from Object)
GetImage() GetImage() GetImage() GetImage()

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

GetText() GetText() GetText() GetText()

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

GetText(TextDataFormat) GetText(TextDataFormat) GetText(TextDataFormat) GetText(TextDataFormat)

表示由指定的格式数据对象中检索文本数据TextDataFormat值。Retrieves text data from the data object in the format indicated by the specified TextDataFormat value.

GetType() GetType() GetType() GetType()

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

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

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

(Inherited from Object)
SetAudio(Byte[]) SetAudio(Byte[]) SetAudio(Byte[]) SetAudio(Byte[])

将添加Byte中的数据对象的数组WaveAudio后将其转换为格式StreamAdds a Byte array to the data object in the WaveAudio format after converting it to a Stream.

SetAudio(Stream) SetAudio(Stream) SetAudio(Stream) SetAudio(Stream)

将添加Stream中的数据对象WaveAudio格式。Adds a Stream to the data object in the WaveAudio format.

SetData(Object) SetData(Object) SetData(Object) SetData(Object)

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

SetData(String, Boolean, Object) SetData(String, Boolean, Object) SetData(String, Boolean, Object) 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) SetData(String, Object) SetData(String, Object) SetData(String, Object)

添加到指定的对象DataObject使用指定的格式。Adds the specified object to the DataObject using the specified format.

SetData(Type, Object) SetData(Type, Object) SetData(Type, Object) SetData(Type, Object)

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

SetFileDropList(StringCollection) SetFileDropList(StringCollection) SetFileDropList(StringCollection) SetFileDropList(StringCollection)

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

SetImage(Image) SetImage(Image) SetImage(Image) SetImage(Image)

将添加Image中的数据对象Bitmap格式。Adds an Image to the data object in the Bitmap format.

SetText(String) SetText(String) SetText(String) SetText(String)

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

SetText(String, TextDataFormat) SetText(String, TextDataFormat) SetText(String, TextDataFormat) SetText(String, TextDataFormat)

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

ToString() ToString() ToString() ToString()

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

(Inherited from Object)

显式界面实现

IDataObject.DAdvise(FORMATETC, ADVF, IAdviseSink, Int32) IDataObject.DAdvise(FORMATETC, ADVF, IAdviseSink, Int32) IDataObject.DAdvise(FORMATETC, ADVF, IAdviseSink, Int32) 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) IDataObject.DUnadvise(Int32) IDataObject.DUnadvise(Int32) IDataObject.DUnadvise(Int32)

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

IDataObject.EnumDAdvise(IEnumSTATDATA) IDataObject.EnumDAdvise(IEnumSTATDATA) IDataObject.EnumDAdvise(IEnumSTATDATA) IDataObject.EnumDAdvise(IEnumSTATDATA)

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

IDataObject.EnumFormatEtc(DATADIR) IDataObject.EnumFormatEtc(DATADIR) IDataObject.EnumFormatEtc(DATADIR) 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) IDataObject.GetCanonicalFormatEtc(FORMATETC, FORMATETC) IDataObject.GetCanonicalFormatEtc(FORMATETC, FORMATETC) 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) IDataObject.GetData(FORMATETC, STGMEDIUM) IDataObject.GetData(FORMATETC, STGMEDIUM) 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) IDataObject.GetDataHere(FORMATETC, STGMEDIUM) IDataObject.GetDataHere(FORMATETC, STGMEDIUM) 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) IDataObject.QueryGetData(FORMATETC) IDataObject.QueryGetData(FORMATETC) 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) IDataObject.SetData(FORMATETC, STGMEDIUM, Boolean) IDataObject.SetData(FORMATETC, STGMEDIUM, Boolean) 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.

适用于

另请参阅