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。 若要擷取其儲存格式的資料,請使用設定為 false 的參數呼叫 。 GetDataautoConvert
若要判斷儲存資料的格式,請呼叫 GetFormats 。 若要判斷格式是否可用,請使用所需的格式呼叫 GetDataPresent 。
在 .NET Framework 2.0 中 DataObject ,類別提供其他方法,可讓您更輕鬆地使用通用格式的資料。 若要將特定格式的資料加入 至 DataObject ,請使用適當的 SetFormat方法,例如 SetText 。 若要從 DataObject 擷取特定格式的資料,請先呼叫適當的 ContainsFormat方法 (,例如 ContainsText) 判斷是否 DataObject 包含該格式的資料,然後呼叫適當的 GetFormat方法 (,例如 GetText) ,以在包含該格式時 DataObject 擷取資料。
注意
搭配剪貼簿使用元檔案格式時,可能需要特殊考慮。 由於 類別目前實作 DataObject 的限制,使用舊版元檔案格式的應用程式可能無法辨識.NET Framework所使用的元檔案格式。 在此情況下,您必須與 Win32 剪貼簿應用程式程式設計介面 (API) 交互操作。
物件必須可序列化,才能將它放在剪貼簿上。 如需序列化的詳細資訊,請參閱 System.Runtime.Serialization 。 如果您的目標應用程式需要非常特定的資料格式,在序列化程式中新增至資料的標頭可能會防止應用程式辨識您的資料。 若要保留資料格式,請將您的資料新增為 Byte 陣列, MemoryStream 並將 傳遞 MemoryStream 至 SetData 方法。
建構函式
| DataObject() |
初始化 DataObject 類別的新執行個體。 |
| DataObject(Object) |
初始化 DataObject 類別的新執行個體,並在其中加入指定的物件。 |
| DataObject(String, Object) |
初始化 DataObject 類別的新執行個體,並以指定之格式加入至指定的物件。 |