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
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
下一个示例检索存储在 a 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
注解
DataObjectIDataObject实现接口,其方法提供独立于格式的机制进行数据传输。
A DataObject 通常用于 Clipboard 拖放操作。 该 DataObject 类提供接口的建议 IDataObject 实现。 建议使用 DataObject 类而不是自行实现 IDataObject 。
可以存储在一个 DataObject不同格式的多个数据片段中。 数据通过关联的格式从其 DataObject 检索。 由于目标应用程序可能未知,因此可以通过以多种格式放置数据 DataObject 来增加数据为应用程序采用适当格式的可能性。 请参阅 DataFormats 预定义格式。 可以通过创建类的 DataFormats.Format 实例来实现自己的格式。
若要将数据存储在 a 中 DataObject,请将数据传递给构造函数或调用 SetData。 可以将多种格式的数据添加到同一 DataObject格式。 如果希望仅以本机格式检索所添加的数据, SetData(String, Boolean, Object) 请使用 autoConvert
参数设置为 调用 false
。
可以从与任何格式兼容的GetData格式检索DataObject数据。 例如,文本可以转换为 Unicode。 To retrieve data in the format in which it was stored, call GetData with the autoConvert
parameter set to false
.
若要确定存储数据的格式,请调用 GetFormats。 若要确定格式是否可用,请使用所需的格式进行调用 GetDataPresent 。
在 .NET Framework 2.0 中,该DataObject类提供了其他方法,使使用常用格式的数据更易于使用。 若要向 添加特定格式 DataObject的数据,请使用相应的 Set
Format 方法,例如 SetText。 若要从中DataObject检索特定格式的数据,请首先调用相应的 Format 方法 ((如 ContainsText) ),以确定该格式中的数据是否DataObject包含该格式,然后调用相应的 Contains``Get
Format 方法 (,例如GetText) ,以在包含数据时DataObject检索数据。
备注
在剪贴板中使用图元文件格式时,可能需要特殊注意事项。 由于类的当前实现DataObject存在限制,.NET Framework使用元文件格式的应用程序可能无法识别.NET Framework使用的图元文件格式。 在这种情况下,必须与 Win32 剪贴板应用程序编程接口互操作, (API) 。
对象必须可序列化,才能将其放在剪贴板上。 有关详细信息,请参阅 System.Runtime.Serialization 序列化。 如果目标应用程序需要非常具体的数据格式,则序列化过程中添加到数据中的标头可能会阻止应用程序识别数据。 若要保留数据格式,请将数据添加为Byte数组并将其MemoryStream传递给MemoryStreamSetData方法。
构造函数
DataObject() |
初始化 DataObject 类的新实例。 |
DataObject(Object) |
初始化 DataObject 类的新实例,并为其添加指定的对象。 |
DataObject(String, Object) |
初始化 DataObject 类的新实例,并以指定格式为其添加指定的对象。 |