DataFormats クラス

静的 (Visual Basic では Shared) な定義済み Clipboard 形式名を提供します。これらを使用して IDataObject に格納するデータの形式を識別します。

この型のすべてのメンバの一覧については、DataFormats メンバ を参照してください。

System.Object
   System.Windows.Forms.DataFormats

Public Class DataFormats
[C#]
public class DataFormats
[C++]
public __gc class DataFormats
[JScript]
public class DataFormats

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

IDataObject クラスと DataObject クラスでも、静的 (Visual Basic では Shared) 形式リストを使用し、システム Clipboard から取得したデータか、またはドラッグ アンド ドロップ操作で転送したデータかを判断します。

GetFormat メソッドを使用すると、次の処理を実行できます。

  • 形式名または ID 番号に対して定義済みの DataFormats.Format オブジェクトを取得します。
  • ユーザーが形式名を渡すとき、このクラスの静的 (Visual Basic では Shared) リストに形式名と ID 番号の新しいペアを追加し、Windows レジストリにこの形式を Clipboard 形式として登録します。

DataFormats.Format インスタンスの適切なフィールドから id 番号または形式の name を取得できます。

使用例

myFormat という名前の新しいデータ形式を作成する例を次に示します。このコードは MyNewObject を作成して DataObject に格納します。 DataObjectClipboard にコピーされます。

次に DataObjectClipboard から取得され、 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


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

 

[C++] 
#using <mscorlib.dll>
#using <System.dll>
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::Windows::Forms;

// Creates a new type.
[Serializable]
 public __gc class MyNewObject : public Object {
 private:
     String* myValue;

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

     // Creates a property to retrieve or set the value.
     __property String* get_MyObjectValue() {
         return myValue;
     }
     __property void set_MyObjectValue( String* value ) {
         myValue = value;
     }
 };

public __gc class MyClass : public Form {
protected:
    TextBox* textBox1;

public:
    void MyClipboardMethod() {
        // Creates a new data format.
        DataFormats::Format* myFormat = DataFormats::GetFormat(S"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 =
            dynamic_cast<MyNewObject*>(myRetrievedObject->GetData(myFormat->Name));

        // Prints the value of the Object in a textBox.
        textBox1->Text = myDereferencedObject->MyObjectValue;
    }
};



[JScript] 
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.Windows.Forms

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

アセンブリ: System.Windows.Forms (System.Windows.Forms.dll 内)

参照

DataFormats メンバ | System.Windows.Forms 名前空間 | Clipboard | DataObject | DataFormats.Format | IDataObject | SetData | GetData | GetFormats | GetDataPresent | GetData | GetDataPresent | GetFormats | SetData