DataObject 類別

定義

實作基本資料傳輸機制。

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 並將 傳遞 MemoryStreamSetData 方法。

建構函式

DataObject()

初始化 DataObject 類別的新執行個體。

DataObject(Object)

初始化 DataObject 類別的新執行個體,並在其中加入指定的物件。

DataObject(String, Object)

初始化 DataObject 類別的新執行個體,並以指定之格式加入至指定的物件。

方法

ContainsAudio()

指出資料物件是否包含 WaveAudio 格式的資料。

ContainsFileDropList()

指出資料物件上是否包含 FileDrop 格式的資料,或是可以轉換為該格式的資料。

ContainsImage()

指出資料物件上是否包含 Bitmap 格式的資料,或是可以轉換為該格式的資料。

ContainsText()

指出資料物件是否包含 UnicodeText 格式的資料。

ContainsText(TextDataFormat)

指出資料物件是否包含由所指定 TextDataFormat 值表示之格式的文字資料。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetAudioStream()

從資料物件擷取音效串流。

GetData(String)

傳回與所指定資料格式相關的資料。

GetData(String, Boolean)

傳回與所指定資料格式相關的資料,使用自動轉換參數來決定是否要將資料轉換成格式。

GetData(Type)

傳回與所指定類別類型格式相關的資料。

GetDataPresent(String)

判斷儲存於這個 DataObject 中的資料是否與所指定格式建立關聯,或是可以轉換成指定的格式。

GetDataPresent(String, Boolean)

判斷這個 DataObject 是否包含指定格式的資料,或者是包含可以轉換成指定格式的資料。

GetDataPresent(Type)

判斷儲存於這個 DataObject 中的資料是否與所指定格式建立關聯,或是可以轉換成指定的格式。

GetFileDropList()

從資料物件擷取檔案名稱集合。

GetFormats()

傳回所有格式的清單,儲存於這個 DataObject 中的資料與這些格式建立關聯,或是該資料可以轉換成這些格式。

GetFormats(Boolean)

使用自動轉換參數判斷是否只擷取原生資料格式,或擷取所有可以轉換的格式,傳回與儲存於這個 DataObject 中之資料建立關聯或者所有可以轉換之格式的清單。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetImage()

從資料物件擷取影像。

GetText()

UnicodeText 格式從資料物件擷取文字資料。

GetText(TextDataFormat)

以所指定 TextDataFormat 值表示之格式來從資料物件擷取文字資料。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
SetAudio(Byte[])

Byte 格式的 WaveAudio 陣列轉換為 Stream 之後,再將該陣列新增至資料物件。

SetAudio(Stream)

Stream 格式的 WaveAudio 資料新增至資料物件。

SetData(Object)

使用物件類型作為資料格式,將指定的物件新增至 DataObject

SetData(String, Boolean, Object)

使用指定的格式,將指定的物件新增至 DataObject,並指出該資料是否可以轉換成另一種格式。

SetData(String, Object)

使用指定的格式,將指定的物件新增至 DataObject

SetData(Type, Object)

使用指定的類型作為格式,將指定的物件新增至 DataObject

SetFileDropList(StringCollection)

FileDrop 格式的檔案名稱集合新增至資料物件。

SetImage(Image)

Image 格式的 Bitmap 資料新增至資料物件。

SetText(String)

UnicodeText 格式的文字資料新增至資料物件。

SetText(String, TextDataFormat)

將文字資料以所指定 TextDataFormat 值表示之格式新增至資料物件。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

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

建立資料物件和通知接收之間的連接。 這個方法是由支援通知接收的物件所呼叫,且可以讓接收通知收到物件資料變更的通知。

IDataObject.DUnadvise(Int32)

終結先前已經建立的告知連接。

IDataObject.EnumDAdvise(IEnumSTATDATA)

建立可以用來列舉目前諮詢連接的物件。

IDataObject.EnumFormatEtc(DATADIR)

建立物件,用於列舉資料物件的 FORMATETC 結構。 呼叫 GetData(FORMATETC, STGMEDIUM)SetData(FORMATETC, STGMEDIUM, Boolean) 時會使用這些結構。

IDataObject.GetCanonicalFormatEtc(FORMATETC, FORMATETC)

提供標準的 FORMATETC 結構,這個結構在邏輯上相當於更複雜的結構。 使用這個方法判斷兩個不同的 FORMATETC 結果是否會傳回相同的資料,而不需要重複呈現。

IDataObject.GetData(FORMATETC, STGMEDIUM)

從來源資料物件取得資料。 GetData(FORMATETC, STGMEDIUM) 方法由資料消費者 (Data Consumer) 呼叫,以呈現指定的 FORMATETC 結構所描述的資料,並透過指定的 STGMEDIUM 結構傳輸這些資料。 接著再由呼叫端負責釋放 STGMEDIUM 結構。

IDataObject.GetDataHere(FORMATETC, STGMEDIUM)

從來源資料物件取得資料。 這個方法 (由資料消費者呼叫) 與 GetData(FORMATETC, STGMEDIUM) 方法不同,呼叫端必須針對後者配置及釋放指定的存放媒體。

IDataObject.QueryGetData(FORMATETC)

判斷資料物件是否能夠呈現 FORMATETC 結構中所描述的資料。 嘗試執行貼上或置放作業的物件可以在呼叫 GetData(FORMATETC, STGMEDIUM) 之前呼叫這個方法,以指出作業是否成功。

IDataObject.SetData(FORMATETC, STGMEDIUM, Boolean)

將資料傳輸給實作這個方法的物件。 這個方法是由包含資料來源的物件所呼叫。

適用於

另請參閱