DataFormats 类

提供 static 预定义的 Clipboard 格式名称。使用它们来标识存储在 IDataObject 中的数据的格式。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Class DataFormats
用法
Dim instance As DataFormats
public class DataFormats
public ref class DataFormats
public class DataFormats
public class DataFormats

备注

IDataObjectDataObject 类也使用 static 格式列表,确定从系统 Clipboard 中检索到的数据或在拖放操作中传输的数据的类型。

GetFormat 方法使您可以:

  • 为某个格式名或 ID 号获取预定义的 DataFormats.Format 对象。

  • 将新的格式名/ID 号对添加到此类中的 static 列表,并在 Windows 注册表中将该格式注册为 Clipboard 格式(向 Windows 注册表传递格式名时)。

可以从 DataFormats.Format 实例中的相应属性中获取 Id 号或格式 Name

示例

下面的代码示例创建一个名为 myFormat 的新数据格式。然后,代码创建一个 MyNewObject ,并将它存储在 DataObject 中。将 DataObject 复制到 Clipboard

然后,将从 Clipboard 中检索 DataObject 并将恢复 MyNewObject MyNewObject 的值显示在文本框中。此代码要求已创建 textBox1 并将其放置在窗体上。

Option Explicit
Option Strict

Imports System
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 'MyClipboardMethod
End Class 'MyClass


' 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 'New
    
    ' 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 'MyNewObject
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;
       }
    }
 }

 
#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;
   }

};
import System.*;
import System.Windows.Forms.*;

public class MyClass extends 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.get_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.get_Name()));

        // Prints the value of the Object in a textBox.
        textBox1.set_Text(myDereferencedObject.get_MyObjectValue());
    } //MyClipboardMethod
} //MyClass

// Creates a new type.
/** @attribute Serializable()
 */
public class MyNewObject extends Object
{
    private String myValue;
    // Creates a default constructor for the class.
    public MyNewObject()
    {
        myValue = "This is the value of the class";
    } //MyNewObject

    // Creates a property to retrieve or set the value.
    /** @property 
     */
    public String get_MyObjectValue()
    {
        return myValue;
    } //get_MyObjectValue

    /** @property 
     */
    public void set_MyObjectValue(String value)
    {
        myValue = value;
    } //set_MyObjectValue
} //MyNewObject
import System;
import System.Windows.Forms;

public class MyClass extends Form {
    protected var textBox1 : TextBox;
    
    public function MyClipboardMethod() {
       // Create a new data format.
       var myFormat : DataFormats.Format = DataFormats.GetFormat("myFormat");
       
       /* Create a new object and store it in a DataObject import the myFormat 
        * as the type of format. */
       var myObject : MyNewObject = new MyNewObject();
       var myDataObject : DataObject = new DataObject("myFormat", myObject);
 
       // Copy myObject into the clipboard.
       Clipboard.SetDataObject(myDataObject);
 
       // Perform some processing steps.
 
       // Retrieve the data from the clipboard.
       var myRetrievedObject : IDataObject = Clipboard.GetDataObject();
 
       // Convert the IDataObject type to MyNewObject type. 
       var myDereferencedObject : MyNewObject = MyNewObject(myRetrievedObject.GetData("myFormat"));
 
       // Print the value of the Object in a textBox.
       textBox1.Text = myDereferencedObject.MyObjectValue;
    }
 }
 
 // Create a new type.
Serializable public class MyNewObject extends Object {
    private var myValue : String;
 
    // Create a default constructor for the class.
    public function MyNewObject() {
       myValue = "This is the value of the class";
    }
 
    // Create a property to retrieve or set the value.
    public function get MyObjectValue() : String {
          return myValue;
    }
       
    public function set MyObjectValue(value : String) {
          myValue = value;
    }
 }
 

继承层次结构

System.Object
  System.Windows.Forms.DataFormats

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0

请参见

参考

DataFormats 成员
System.Windows.Forms 命名空间
Clipboard 类
DataObject
DataFormats.Format
IDataObject
SetData
GetData
GetFormats
GetDataPresent
GetData
GetDataPresent
GetFormats
SetData