DataObject 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
实现基数据传输机制。
public ref class DataObject : System::Windows::Forms::IDataObject
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.Windows.Forms.IDataObject
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
public class DataObject : System.Runtime.InteropServices.ComTypes.IDataObject, System.Windows.Forms.IDataObject
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
type DataObject = class
interface IDataObject
interface UnsafeNativeMethods.IOleDataObject
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
type DataObject = class
interface IDataObject
interface IDataObject
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
type DataObject = class
interface IDataObject
interface IDataObject
interface IDataObject.Interface
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
type DataObject = class
interface IDataObject
interface IDataObject.Interface
interface IDataObject
Public Class DataObject
Implements IDataObject
Public Class DataObject
Implements IDataObject, IDataObject
- 继承
-
DataObject
- 属性
- 实现
示例
下面的代码示例在 中添加 DataObject数据。 首先,创建一个新 DataObject 组件,并在其中存储一个组件。 然后,它会检查 中是否存在 DataObject相应类型的数据。 结果显示在文本框中。 此代码要求 textBox1 已创建 。
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。 首先,使用文本数据创建新的 DataObject 。 然后,检索数据,将其格式指定为字符串,并显示在文本框中。 数据格式会自动从文本转换为字符串。 此代码要求 textBox1 已创建 。
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在拖放操作中,通常与 Clipboard 和 一起使用。 类 DataObject 提供 接口的建议实现 IDataObject 。 建议使用 类, DataObject 而不是自行实现 IDataObject 。
可以在 中 DataObject存储格式不同的多个数据片段。 数据按关联的格式从 DataObject 中检索。 由于目标应用程序可能未知,因此可以通过将数据置于 DataObject 多种格式中来增加数据采用适合应用程序格式的可能性。 有关预定义格式,请参阅 DataFormats 。 可以通过创建 类的 DataFormats.Format 实例来实现自己的格式。
若要将数据存储在 中 DataObject,请将数据传递给构造函数或调用 SetData。 可以将多种格式的数据添加到同一个 DataObject。 如果希望仅以本机格式检索所添加的数据,请调用 SetData(String, Boolean, Object) 并将 autoConvert 参数设置为 false。
可以采用与 GetData兼容的任何格式从 DataObject 检索数据。 例如,文本可以转换为 Unicode。 若要检索存储数据的格式的数据,请调用 GetData 并将 autoConvert 参数设置为 false。
若要确定存储数据的格式,请调用 GetFormats。 若要确定格式是否可用,请使用所需格式调用 GetDataPresent 。
在 .NET Framework 2.0 中DataObject,类提供了其他方法,使处理通用格式的数据更容易。 若要将特定格式的数据添加到 , DataObject请使用适当的 SetFormat 方法,例如 SetText。 若要从 DataObject检索特定格式的数据,请先调用相应的 ContainsFormat 方法 ((如 ContainsText) )以确定 是否 DataObject 包含该格式的数据,然后调用相应的 GetFormat 方法 ((如 GetText) )来检索数据(如果 DataObject 包含数据)。
注意
在剪贴板中使用图元文件格式时,可能需要特殊注意事项。 由于 类的当前实现DataObject存在限制,使用较旧图元文件格式的应用程序可能无法识别.NET Framework使用的图元文件格式。 在这种情况下,必须 (API) 与 Win32 剪贴板应用程序编程接口互操作。
对象必须可序列化,才能将其放在剪贴板中。 有关序列化的详细信息,请参阅 System.Runtime.Serialization 。 如果目标应用程序需要非常具体的数据格式,则序列化过程中添加到数据的标头可能会阻止应用程序识别数据。 若要保留数据格式,请将数据作为 Byte 数组添加到 , MemoryStream 并将 传递给 MemoryStreamSetData 方法。
构造函数
| DataObject() |
初始化 DataObject 类的新实例。 |
| DataObject(Object) |
初始化 DataObject 类的新实例,并为其添加指定的对象。 |
| DataObject(String, Object) |
初始化 DataObject 类的新实例,并以指定格式为其添加指定的对象。 |