DataFormats 类 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
提供 static 预定义的 Clipboard 格式名称。 使用它们来标识存储在 IDataObject 中的数据的格式。
public ref class DataFormatspublic ref class DataFormats abstract sealedpublic class DataFormatspublic static class DataFormatstype DataFormats = classPublic 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
注解
              IDataObject和 DataObject 类还使用static格式列表来确定从系统 Clipboard检索或在拖放操作中传输的数据的类型。
方法 GetFormat 允许你:
- 获取格式名称或 ID 号的预定义 DataFormats.Format 对象。 
- 向此类中的列表添加新的格式名称/ID 号对 - static,并在向 Windows 注册表传递格式名称时将格式注册为 Clipboard 格式。
可以从 实例中的IdDataFormats.Format相应属性获取数字或格式Name。
字段
| Bitmap | 指定 Windows 位图格式。 此  | 
| CommaSeparatedValue | 指定以逗号分隔值 (CSV) 的格式,这是电子表格常用的交换格式。 Windows 窗体不直接使用此格式。 此  | 
| Dib | 指定 Windows 与设备无关的位图 (DIB) 格式。 此  | 
| Dif | 指定 Windows 数据交换格式 (DIF),Windows 窗体不直接使用此格式。 此  | 
| EnhancedMetafile | 指定 Windows 增强型图元文件格式。 此  | 
| FileDrop | 指定 Windows 文件放置格式,Windows 窗体不直接使用此格式。 此  | 
| Html | 指定 HTML 剪贴板格式中的文本。 此  | 
| Locale | 指定 Windows 区域性格式,Windows 窗体不直接使用此格式。 此  | 
| MetafilePict | 指定 Windows 图元文件格式,Windows 窗体不直接使用此格式。 此  | 
| OemText | 指定标准 Windows 原始设备制造商 (OEM) 文本格式。 此  | 
| Palette | 指定 Windows 调色板格式。 此  | 
| PenData | 指定 Windows 钢笔数据格式,它由书写软件所使用的笔画组成,Windows 窗体不使用此格式。 此  | 
| Riff | 指定资源交换文件格式 (RIFF) 音频格式,Windows 窗体不直接使用此格式。 此  | 
| Rtf | 指定由 RTF 数据组成的文本。 此  | 
| Serializable | 指定封装任何类型的 Windows 窗体对象的格式。 此  | 
| StringFormat | 指定 Windows 窗体字符串类格式,Windows 窗体使用此格式存储字符串对象。 此  | 
| SymbolicLink | 指定 Windows 符号链接格式,Windows 窗体不直接使用此格式。 此  | 
| Text | 指定标准 ANSI 文本格式。 此  | 
| Tiff | 指定标记图像文件格式 (TIFF),Windows 窗体不直接使用此格式。 此  | 
| UnicodeText | 指定标准 Windows Unicode 文本格式。 此  | 
| WaveAudio | 指定 wave 音频格式,Windows 窗体不直接使用此格式。 此  | 
方法
| GetFormat(Int32) | 为指定的 ID 返回带有 Windows 剪贴板数字 ID 和名称的 DataFormats.Format。 | 
| GetFormat(String) | 为指定的格式返回带有 Windows 剪贴板数字 ID 和名称的 DataFormats.Format。 |