DataFormats 类

定义

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

C#
public class DataFormats
C#
public static class DataFormats
继承
DataFormats

示例

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

接下来, DataObjectClipboard 中检索 ,并 MyNewObject 恢复 。 的值 MyNewObject 在文本框中打印出来。 此代码要求 textBox1 已创建并放置在窗体上。

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

注解

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

方法 GetFormat 允许你:

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

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

可以从 实例中的IdDataFormats.Format相应属性获取数字或格式Name

字段

Bitmap

指定 Windows 位图格式。 此 static 字段是只读的。

CommaSeparatedValue

指定以逗号分隔值 (CSV) 的格式,这是电子表格常用的交换格式。 Windows 窗体不直接使用此格式。 此 static 字段是只读的。

Dib

指定 Windows 与设备无关的位图 (DIB) 格式。 此 static 字段是只读的。

Dif

指定 Windows 数据交换格式 (DIF),Windows 窗体不直接使用此格式。 此 static 字段是只读的。

EnhancedMetafile

指定 Windows 增强型图元文件格式。 此 static 字段是只读的。

FileDrop

指定 Windows 文件放置格式,Windows 窗体不直接使用此格式。 此 static 字段是只读的。

Html

指定 HTML 剪贴板格式中的文本。 此 static 字段是只读的。

Locale

指定 Windows 区域性格式,Windows 窗体不直接使用此格式。 此 static 字段是只读的。

MetafilePict

指定 Windows 图元文件格式,Windows 窗体不直接使用此格式。 此 static 字段是只读的。

OemText

指定标准 Windows 原始设备制造商 (OEM) 文本格式。 此 static 字段是只读的。

Palette

指定 Windows 调色板格式。 此 static 字段是只读的。

PenData

指定 Windows 钢笔数据格式,它由书写软件所使用的笔画组成,Windows 窗体不使用此格式。 此 static 字段是只读的。

Riff

指定资源交换文件格式 (RIFF) 音频格式,Windows 窗体不直接使用此格式。 此 static 字段是只读的。

Rtf

指定由 RTF 数据组成的文本。 此 static 字段是只读的。

Serializable

指定封装任何类型的 Windows 窗体对象的格式。 此 static 字段是只读的。

StringFormat

指定 Windows 窗体字符串类格式,Windows 窗体使用此格式存储字符串对象。 此 static 字段是只读的。

SymbolicLink

指定 Windows 符号链接格式,Windows 窗体不直接使用此格式。 此 static 字段是只读的。

Text

指定标准 ANSI 文本格式。 此 static 字段是只读的。

Tiff

指定标记图像文件格式 (TIFF),Windows 窗体不直接使用此格式。 此 static 字段是只读的。

UnicodeText

指定标准 Windows Unicode 文本格式。 此 static 字段是只读的。

WaveAudio

指定 wave 音频格式,Windows 窗体不直接使用此格式。 此 static 字段是只读的。

方法

GetFormat(Int32)

为指定的 ID 返回带有 Windows 剪贴板数字 ID 和名称的 DataFormats.Format

GetFormat(String)

为指定的格式返回带有 Windows 剪贴板数字 ID 和名称的 DataFormats.Format

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另请参阅