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
下一個範例是擷取儲存在 的 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 A 通常用於Clipboard拖放操作中的 和 。 該 DataObject 類別提供推薦的介面實作 IDataObject 。 建議你使用這個 DataObject 類別,而不是自己實作 IDataObject 。
多個不同格式的資料可以儲存在 DataObject一個 。 資料依其相關格式從 a DataObject 中擷取。 由於目標應用程式可能未知,你可以透過將資料 DataObject 置入多種格式,提高資料以適當格式呈現應用的機率。 請參閱 DataFormats 預先定義的格式。 你可以透過建立該類別的實例 DataFormats.Format 來實作自己的格式。
若要將資料儲存在 中 DataObject,將資料傳給建構子或呼叫 SetData。 你可以將多種格式的資料加入同一個 DataObject。 如果你只想讓新增的資料以原生格式取得,請將SetData(String, Boolean, Object)autoConvert參數設為 false。
資料可從 中 DataObject 以任何與 相容 GetData的格式擷取。 例如,文字可以轉換成 Unicode。 若要取得資料儲存格式,請將GetDataautoConvert參數設為 false。
要判斷資料儲存格式,請呼叫 GetFormats。 要判斷是否有格式可用,請以所需格式致電 GetDataPresent 。
課程 DataObject 提供額外方法,使使用常見格式的資料更為簡便。 若要將特定格式的資料加入 DataObject,請使用適當的 Set格式 方法,例如 SetText。 若要從 中取得特定格式 DataObject的資料,首先呼叫適當的 ContainsFormat 方法(例如 ContainsText),以判斷 是否 DataObject 包含該格式的資料,接著呼叫適當的 Get格式 方法(例如 GetText),若包含 DataObject 該資料則呼叫 。
Note
在使用剪貼簿的元檔案格式時,可能需要特別注意。 由於目前 DataObject 類別實作的限制,.NET 框架所使用的元檔案格式可能無法被使用較舊元檔案格式的應用程式識別。 在這種情況下,你必須與 Win32 剪貼簿應用程式介面(API)互通。
物件必須可序列化,才能放在剪貼簿上。 更多關於序號的資訊請參見 System.Runtime.Serialization 。 如果你的目標應用程式需要非常特定的資料格式,序列化過程中新增的標頭可能會阻止應用程式辨識你的資料。 為了保留資料格式,將資料加入陣Byte列到 aMemoryStream,然後傳給MemoryStreamSetData方法。
建構函式
| 名稱 | Description |
|---|---|
| DataObject() |
初始化 DataObject 類別的新執行個體。 |
| DataObject(Object) |
初始化該類別的新實例 DataObject ,並將指定的物件加入其中。 |
| DataObject(String, Object) |
初始化該類別的新實例 DataObject ,並以指定格式加入指定的物件。 |