共用方式為


HOW TO:將資料加入至剪貼簿

更新:2007 年 11 月

Clipboard 類別提供了一些方法,可以用來與 Windows 作業系統的剪貼簿功能互動。許多應用程式都將剪貼簿當做資料的暫時儲存機制。例如,文書處理器在進行剪下和貼上作業時就會使用剪貼簿。在應用程式之間傳送資料時,剪貼簿也很有用。

當您將資料加入至剪貼簿時,可以指明資料格式,以便其他可以使用該格式的應用程式能夠辨識資料。您也可以將資料以多種不同的格式加入剪貼簿,以增加其他可能使用這些資料的應用程式數目。

剪貼簿檔案格式是一個用來辨識格式的字串,讓使用該格式的應用程式能夠擷取相關聯的資料。DataFormats 類別會提供預先定義的格式名稱,以供您使用。您也可以使用自己的格式名稱,或是使用物件型別做為格式。

若要將資料以一或多種格式加入剪貼簿,請使用 SetDataObject 方法。您可以將任何物件傳遞給這個方法,但是如果要以多種格式加入資料,則必須先將資料加入至設計成可使用多種格式的單獨物件中。一般而言,您會將資料加入 DataObject,但是您可以使用實作 IDataObject 介面的任何型別。

在 .NET Framework 2.0 版中,有一些針對簡化基本剪貼簿工作而設計的新方法,您可以利用這些方法將資料直接加入至剪貼簿中。當您使用單一常用格式 (例如文字) 時,請使用這些方法。

注意事項:

所有 Windows 應用程式都會共用剪貼簿。因此,剪貼簿的內容會根據您切換至不同的應用程式而改變。

Clipboard 類別只能用在設定為單一執行緒 Apartment Model (STA) 的執行緒中。若要使用這個類別,請確定您的 Main 方法是以 STAThreadAttribute 屬性做為標記。

物件必須可以序列化,才能放在剪貼簿上。若要讓型別可以序列化,請將它以 SerializableAttribute 屬性標記。如果您將不可序列化的物件傳入剪貼簿方法,方法將會失敗,而且不會擲回例外狀況。如需序列化的詳細資訊,請參閱序列化

若要以單一常用格式將資料加入至剪貼簿

  • 請使用 SetAudioSetFileDropListSetImageSetText 方法。這些方法只能在 .NET Framework 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 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; }
        }
    }
    

請參閱

工作

HOW TO:從剪貼簿擷取資料

其他資源

拖放作業和剪貼簿支援