DataFormats 類別

定義

提供 static,預先定義的 Clipboard 格式名稱。 請用它們識別儲存於 IDataObject 中的資料的格式。

public ref class DataFormats
public ref class DataFormats abstract sealed
public class DataFormats
public static class DataFormats
type DataFormats = class
Public Class DataFormats
繼承
DataFormats

範例

下列程式碼範例會建立名為 myFormat 的新資料格式。 然後,程式碼會 MyNewObject 建立儲存在 中的 DataObject 。 會 DataObject 複製到 Clipboard

接下來, DataObject 會從 Clipboard 擷取 , MyNewObject 並復原 。 的值 MyNewObject 會列印在文字方塊中。 此程式碼需要 textBox1 已建立並放置在表單上。

#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>

using namespace System;
using namespace System::Windows::Forms;

// Creates a new type.

[Serializable]
public ref class MyNewObject: public Object
{
private:
   String^ myValue;

public:

   // Creates a default constructor for the class.
   MyNewObject()
   {
      myValue = "This is the value of the class";
   }


   property String^ MyObjectValue 
   {

      // Creates a property to retrieve or set the value.
      String^ get()
      {
         return myValue;
      }

      void set( String^ value )
      {
         myValue = value;
      }

   }

};

public ref class MyClass: public Form
{
protected:
   TextBox^ textBox1;

public:
   void MyClipboardMethod()
   {
      
      // Creates a new data format.
      DataFormats::Format^ myFormat = DataFormats::GetFormat( "myFormat" );
      
      /* Creates a new object and stores it in a DataObject using myFormat 
               * as the type of format. */
      MyNewObject^ myObject = gcnew MyNewObject;
      DataObject^ myDataObject = gcnew DataObject( myFormat->Name,myObject );
      
      // Copies myObject into the clipboard.
      Clipboard::SetDataObject( myDataObject );
      
      // Performs some processing steps.
      // Retrieves the data from the clipboard.
      IDataObject^ myRetrievedObject = Clipboard::GetDataObject();
      
      // Converts the IDataObject type to MyNewObject type. 
      MyNewObject^ myDereferencedObject = dynamic_cast<MyNewObject^>(myRetrievedObject->GetData( myFormat->Name ));
      
      // Prints the value of the Object in a textBox.
      textBox1->Text = myDereferencedObject->MyObjectValue;
   }

};
using System;
using System.Windows.Forms;

public class MyClass : Form {
    protected TextBox textBox1;
    
    public void MyClipboardMethod() {
       // Creates a new data format.
       DataFormats.Format myFormat = DataFormats.GetFormat("myFormat");
       
       /* Creates a new object and stores it in a DataObject using myFormat 
        * as the type of format. */
       MyNewObject myObject = new MyNewObject();
       DataObject myDataObject = new DataObject(myFormat.Name, myObject);
 
       // Copies myObject into the clipboard.
       Clipboard.SetDataObject(myDataObject);
 
       // Performs some processing steps.
 
       // Retrieves the data from the clipboard.
       IDataObject myRetrievedObject = Clipboard.GetDataObject();
 
       // Converts the IDataObject type to MyNewObject type. 
       MyNewObject myDereferencedObject = (MyNewObject)myRetrievedObject.GetData(myFormat.Name);
 
       // Prints the value of the Object in a textBox.
       textBox1.Text = myDereferencedObject.MyObjectValue;
    }
 }
 
 // Creates a new type.
 [Serializable]
 public class MyNewObject : Object {
    private string myValue;
 
    // Creates a default constructor for the class.
    public MyNewObject() {
       myValue = "This is the value of the class";
    }
 
    // Creates a property to retrieve or set the value.
    public string MyObjectValue {
       get {
          return myValue;
       }
       set {
          myValue = value;
       }
    }
 }
Option Explicit
Option Strict

Imports System.Windows.Forms

Public Class MyClass1
    Inherits Form
    Private textBox1 As TextBox

    Public Sub MyClipboardMethod()
        ' Creates a new data format.
        Dim myFormat As DataFormats.Format = _
            DataFormats.GetFormat("myFormat")
        
        ' Creates a new object and store it in a DataObject using myFormat 
        ' as the type of format. 
        Dim myObject As New MyNewObject()
        Dim myDataObject As New DataObject(myFormat.Name, myObject)
        
        ' Copies myObject into the clipboard.
        Clipboard.SetDataObject(myDataObject)
        
        ' Performs some processing steps.
        ' Retrieves the data from the clipboard.
        Dim myRetrievedObject As IDataObject = Clipboard.GetDataObject()
        
        ' Converts the IDataObject type to MyNewObject type. 
        Dim myDereferencedObject As MyNewObject = _
            CType(myRetrievedObject.GetData(myFormat.Name), MyNewObject)
        
        ' Print the value of the Object in a textBox.
        textBox1.Text = myDereferencedObject.MyObjectValue
    End Sub
End Class


' Creates a new type.
<Serializable()> Public Class MyNewObject
    Inherits Object
    Private myValue As String
    
    
    ' Creates a default constructor for the class.
    Public Sub New()
        myValue = "This is the value of the class"
    End Sub
    
    ' Creates a property to retrieve or set the value.
    
    Public Property MyObjectValue() As String
        Get
            Return myValue
        End Get
        Set
            myValue = value
        End Set
    End Property
End Class

備註

IDataObjectDataObject 類別也會使用 static 格式清單來判斷從系統 Clipboard 擷取的資料類型,或在拖放作業中傳輸的資料類型。

方法 GetFormat 可讓您:

  • 取得格式名稱或識別碼的預先定義 DataFormats.Format 物件。

  • 將新的格式名稱/識別碼組新增至 static 此類別中的清單,並在傳遞格式名稱時,以Windows登錄 Clipboard 註冊格式。

您可以從 實例中的 DataFormats.Format 適當屬性取得 Id 數位或格式 Name

欄位

Bitmap

指定 Windows 點陣圖的格式。 這個 static 欄位是唯讀的。

CommaSeparatedValue

指定逗號分隔值 (CSV) 格式,這是試算表常用的一種交換格式。 Windows Form 不會直接使用這種格式。 這個 static 欄位是唯讀的。

Dib

指定 Windows 與裝置無關的點陣圖 (DIB) 格式。 這個 static 欄位是唯讀的。

Dif

指定 Windows 資料交換格式 (DIF),Windows Form 不會直接使用這種格式。 這個 static 欄位是唯讀的。

EnhancedMetafile

指定 Windows 加強型中繼檔 (Metafile) 格式。 這個 static 欄位是唯讀的。

FileDrop

指定 Windows 的檔案置放格式,Windows Form 不會直接使用這種格式。 這個 static 欄位是唯讀的。

Html

指定 HTML 剪貼簿格式的文字。 這個 static 欄位是唯讀的。

Locale

指定 Windows 的文化特性 (Culture) 格式,Windows Form 不會直接使用這種格式。 這個 static 欄位是唯讀的。

MetafilePict

指定 Windows 中繼檔格式,Windows Form 不會直接使用這種格式。 這個 static 欄位是唯讀的。

OemText

指定標準的 Windows 原始設備廠商 (OEM) 文字格式。 這個 static 欄位是唯讀的。

Palette

指定 Windows 調色盤格式。 這個 static 欄位是唯讀的。

PenData

指定 Windows 的畫筆資料格式,這個格式由手寫軟體的筆畫構成,Windows Form 不會使用這個格式。 這個 static 欄位是唯讀的。

Riff

指定資源交換檔案格式 (RIFF) 音效格式,Windows Form 不會直接使用這種格式。 這個 static 欄位是唯讀的。

Rtf

指定由 Rich Text 格式 (RTF) 資料構成的文字。 這個 static 欄位是唯讀的。

Serializable

指定用來封裝任何類型的 Windows Form 物件的格式。 這個 static 欄位是唯讀的。

StringFormat

指定 Windows Form 字串類別格式,Windows Form 會用這個格式儲存字串物件。 這個 static 欄位是唯讀的。

SymbolicLink

指定 Windows 符號連結格式,Windows Form 不會直接使用這種格式。 這個 static 欄位是唯讀的。

Text

指定標準的 ANSI 文字格式。 這個 static 欄位是唯讀的。

Tiff

指定 Tagged Image File Format (TIFF),Windows Form 不會直接使用這種格式。 這個 static 欄位是唯讀的。

UnicodeText

指定標準的 Windows Unicode 文字格式。 這個 static 欄位是唯讀的。

WaveAudio

指定 Wave Audio 格式,Windows Form 不會直接使用這種格式。 這個 static 欄位是唯讀的。

方法

GetFormat(Int32)

傳回有 Windows 剪貼簿的數字 ID 和指定 ID 名稱的 DataFormats.Format

GetFormat(String)

傳回指定格式的 Windows 剪貼簿數字 ID 和名稱的 DataFormats.Format

適用於

另請參閱