Share via


方法 : クリップボードにデータを追加する

Clipboard クラスには、Windows オペレーティング システムのクリップボード機能とやり取りするためのメソッドが用意されています。 クリップボードは、データの一時的なレポジトリとして多くのアプリケーションで使用されます。 たとえば、ワード プロセッサは、切り取りと貼り付けの操作でクリップボードを使用します。 クリップボードはデータをアプリケーション間で移動させるときにも使用します。

データをクリップボードに追加する場合にはデータ形式を指定できるため、別のアプリケーションはその形式を使用できるかどうかを判断できます。 データをさまざまな形式でクリップボードに追加することで、データを使用できるアプリケーションの数を増やすこともできます。

クリップボード形式は、形式を識別する文字列です。この形式を使用するアプリケーションが、関連データを取得できるようにします。 DataFormats クラスには、使用できる形式名が定義されています。 また、独自の形式名を使用したり、オブジェクトの型を形式名として使用したりできます。

1 つまたは複数の形式でデータをクリップボードに追加するには、SetDataObject メソッドを使用します。 このメソッドには任意のオブジェクトを渡すことができますが、データを複数の形式で追加するには、最初に、複数の形式で動作するようにデザインされている個別のオブジェクトにデータを追加する必要があります。 一般には、DataObject にデータを追加しますが、IDataObject インターフェイスを実装している任意の型を使用できます。

.NET Framework Version 2.0 では、基本的なクリップボード タスクを簡単に実行できるようにデザインされている新しいメソッドを使って、クリップボードにデータを直接追加できます。 これらのメソッドは、テキストなど、単一の共通形式のデータを操作するときに使用します。

注意

Windows ベースのアプリケーションはすべて、クリップボードを共有します。 そのため、別のアプリケーションに切り替えるとクリップボードの内容は変わります。

Clipboard クラスは、シングル スレッド アパートメント (STA: Single-Threaded Apartment) モードに設定されたスレッドでのみ使用できます。 このクラスを使用するには、Main メソッドが STAThreadAttribute 属性でマークされている必要があります。

オブジェクトをクリップボードに置く場合は、オブジェクトをシリアル化する必要があります。 型をシリアル化するには、SerializableAttribute 属性でマークします。 非シリアル化オブジェクトをクリップボード メソッドに渡すと、例外をスローせずにメソッドは失敗します。 シリアル化の詳細については、「System.Runtime.Serialization」を参照してください。

データを単一の共通形式でクリップボードに追加するには

  • SetAudioSetFileDropListSetImage、または SetText メソッドを使用します。 これらのメソッドは .NET Framework Version 2.0 以外では使用できません。

    ' Demonstrates SetAudio, ContainsAudio, and GetAudioStream.
    Public Function SwapClipboardAudio( _
        ByVal replacementAudioStream As System.IO.Stream) _
        As System.IO.Stream
    
        Dim returnAudioStream As System.IO.Stream = Nothing
    
        If (Clipboard.ContainsAudio()) Then
            returnAudioStream = Clipboard.GetAudioStream()
            Clipboard.SetAudio(replacementAudioStream)
        End If
    
        Return returnAudioStream
    
    End Function
    
    ' Demonstrates SetFileDropList, ContainsFileDroList, and GetFileDropList
    Public Function SwapClipboardFileDropList(ByVal replacementList _
        As System.Collections.Specialized.StringCollection) _
        As System.Collections.Specialized.StringCollection
    
        Dim returnList As System.Collections.Specialized.StringCollection _
            = Nothing
    
        If Clipboard.ContainsFileDropList() Then
    
            returnList = Clipboard.GetFileDropList()
            Clipboard.SetFileDropList(replacementList)
        End If
    
        Return returnList
    
    End Function
    
    ' Demonstrates SetImage, ContainsImage, and GetImage.
    Public Function SwapClipboardImage( _
        ByVal replacementImage As System.Drawing.Image) _
        As System.Drawing.Image
    
        Dim returnImage As System.Drawing.Image = Nothing
    
        If Clipboard.ContainsImage() Then
            returnImage = Clipboard.GetImage()
            Clipboard.SetImage(replacementImage)
        End If
    
        Return returnImage
    End Function
    
    ' Demonstrates SetText, ContainsText, and GetText.
    Public Function SwapClipboardHtmlText( _
        ByVal replacementHtmlText As String) As String
    
        Dim returnHtmlText As String = Nothing
    
        If (Clipboard.ContainsText(TextDataFormat.Html)) Then
            returnHtmlText = Clipboard.GetText(TextDataFormat.Html)
            Clipboard.SetText(replacementHtmlText, TextDataFormat.Html)
        End If
    
        Return returnHtmlText
    
    End Function
    
    // Demonstrates SetAudio, ContainsAudio, and GetAudioStream.
    public System.IO.Stream SwapClipboardAudio(
        System.IO.Stream replacementAudioStream)
    {
        System.IO.Stream returnAudioStream = null;
        if (Clipboard.ContainsAudio())
        {
            returnAudioStream = Clipboard.GetAudioStream();
            Clipboard.SetAudio(replacementAudioStream);
        }
        return returnAudioStream;
    }
    
    // Demonstrates SetFileDropList, ContainsFileDroList, and GetFileDropList
    public System.Collections.Specialized.StringCollection
        SwapClipboardFileDropList(
        System.Collections.Specialized.StringCollection replacementList)
    {
        System.Collections.Specialized.StringCollection returnList = null;
        if (Clipboard.ContainsFileDropList())
        {
            returnList = Clipboard.GetFileDropList();
            Clipboard.SetFileDropList(replacementList);
        }
        return returnList;
    }
    
    // Demonstrates SetImage, ContainsImage, and GetImage.
    public System.Drawing.Image SwapClipboardImage(
        System.Drawing.Image replacementImage)
    {
        System.Drawing.Image returnImage = null;
        if (Clipboard.ContainsImage())
        {
            returnImage = Clipboard.GetImage();
            Clipboard.SetImage(replacementImage);
        }
        return returnImage;
    }
    
    // Demonstrates SetText, ContainsText, and GetText.
    public String SwapClipboardHtmlText(String replacementHtmlText)
    {
        String returnHtmlText = null;
        if (Clipboard.ContainsText(TextDataFormat.Html))
        {
            returnHtmlText = Clipboard.GetText(TextDataFormat.Html);
            Clipboard.SetText(replacementHtmlText, TextDataFormat.Html);
        }
        return returnHtmlText;
    }
    

データをカスタム形式でクリップボードに追加するには

  • SetData メソッドとカスタム形式名を使用します。 このメソッドは .NET Framework Version 2.0 以外では使用できません。

    また、定義済みの形式名を指定して SetData メソッドを使用することもできます。 詳細については、「DataFormats」を参照してください。

    ' Demonstrates SetData, ContainsData, and GetData
    ' using a custom format name and a business object.
    Public ReadOnly Property TestCustomFormat() As Customer
        Get
            Clipboard.SetData("CustomerFormat", New Customer("Customer Name"))
    
            If Clipboard.ContainsData("CustomerFormat") Then
                Return CType(Clipboard.GetData("CustomerFormat"), Customer)
            End If
    
            Return Nothing
        End Get
    End Property
    
    
    ...
    
    
    <Serializable()> Public Class Customer
    
        Private nameValue As String = String.Empty
    
        Public Sub New(ByVal name As String)
            nameValue = name
        End Sub
    
        Public Property Name() As String
            Get
                Return nameValue
            End Get
            Set(ByVal value As String)
                nameValue = value
            End Set
        End Property
    
    End Class
    
    // Demonstrates SetData, ContainsData, and GetData
    // using a custom format name and a business object.
    public Customer TestCustomFormat
    {
        get
        {
            Clipboard.SetData("CustomerFormat", new Customer("Customer Name"));
            if (Clipboard.ContainsData("CustomerFormat")) 
            {
                return Clipboard.GetData("CustomerFormat") as Customer;
            }
            return null;
        }
    }
    
    
    ...
    
    
    [Serializable]
    public class Customer
    {
        private string nameValue = string.Empty;
        public Customer(String name)
        {
            nameValue = name;
        }
        public string Name
        {
            get { return nameValue; }
            set { nameValue = value; }
        }
    }
    

データを複数の形式でクリップボードに追加するには

  • SetDataObject メソッドを使用し、データを格納している DataObject を渡します。 .NET Framework 2.0 よりも前のバージョンでクリップボードにデータを追加する場合は、このメソッドを使う必要があります。

    ' Demonstrates how to use a DataObject to add
    ' data to the Clipboard in multiple formats.
    Public Sub TestClipboardMultipleFormats()
    
        Dim data As New DataObject()
    
        ' Add a Customer object using the type as the format.
        data.SetData(New Customer("Customer as Customer object"))
    
        ' Add a ListViewItem object using a custom format name.
        data.SetData("CustomFormat", _
            New ListViewItem("Customer as ListViewItem"))
    
        Clipboard.SetDataObject(data)
        Dim retrievedData As DataObject = _
            CType(Clipboard.GetDataObject(), DataObject)
    
        If (retrievedData.GetDataPresent("CustomFormat")) Then
    
            Dim item As ListViewItem = _
                TryCast(retrievedData.GetData("CustomFormat"), ListViewItem)
    
            If item IsNot Nothing Then
                MessageBox.Show(item.Text)
            End If
    
        End If
    
        If retrievedData.GetDataPresent(GetType(Customer)) Then
    
            Dim customer As Customer = _
                CType(retrievedData.GetData(GetType(Customer)), Customer)
    
            If customer IsNot Nothing Then
    
                MessageBox.Show(customer.Name)
            End If
    
        End If
    
    End Sub
    
    
    ...
    
    
    <Serializable()> Public Class Customer
    
        Private nameValue As String = String.Empty
    
        Public Sub New(ByVal name As String)
            nameValue = name
        End Sub
    
        Public Property Name() As String
            Get
                Return nameValue
            End Get
            Set(ByVal value As String)
                nameValue = value
            End Set
        End Property
    
    End Class
    
    // Demonstrates how to use a DataObject to add
    // data to the Clipboard in multiple formats.
    public void TestClipboardMultipleFormats()
    {
        DataObject data = new DataObject();
    
        // Add a Customer object using the type as the format.
        data.SetData(new Customer("Customer as Customer object"));
    
        // Add a ListViewItem object using a custom format name.
        data.SetData("CustomFormat", 
            new ListViewItem("Customer as ListViewItem"));
    
        Clipboard.SetDataObject(data);
        DataObject retrievedData = (DataObject)Clipboard.GetDataObject();
    
        if (retrievedData.GetDataPresent("CustomFormat"))
        {
            ListViewItem item = 
                retrievedData.GetData("CustomFormat") as ListViewItem;
            if (item != null)
            {
                MessageBox.Show(item.Text);
            }
        }
    
        if (retrievedData.GetDataPresent(typeof(Customer)))
        {
            Customer customer = 
                retrievedData.GetData(typeof(Customer)) as Customer;
            if (customer != null)
            {
                MessageBox.Show(customer.Name);
            }
        }
    }
    
    
    ...
    
    
    [Serializable]
    public class Customer
    {
        private string nameValue = string.Empty;
        public Customer(String name)
        {
            nameValue = name;
        }
        public string Name
        {
            get { return nameValue; }
            set { nameValue = value; }
        }
    }
    

参照

処理手順

方法 : クリップボードからデータを取得する

その他の技術情報

ドラッグ アンド ドロップ操作とクリップボードのサポート