共用方式為


如何:將資料加入至剪貼簿

類別 Clipboard 提供可用來與 Windows 作業系統剪貼簿功能互動的方法。 許多應用程式會使用剪貼簿作為資料的暫存存放庫。 例如,文字處理器會在剪下和貼上作業期間使用剪貼簿。 剪貼簿也適用于將資料從一個應用程式傳輸到另一個應用程式。

當您將資料新增至剪貼簿時,您可以指出資料格式,讓其他應用程式可以在使用該格式時辨識資料。 您也可以以多種不同格式將資料新增至剪貼簿,以增加可能使用資料的其他應用程式數目。

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

若要以一或多個格式將資料新增至剪貼簿,請使用 SetDataObject 方法。 您可以將任何物件傳遞至此方法,但若要以多種格式新增資料,您必須先將資料新增至設計成使用多種格式的個別物件。 一般而言,您會將資料新增至 DataObject ,但您可以使用任何實作 IDataObject 介面的類型。

若要以單一通用格式將資料新增至剪貼簿,請使用該格式的特定方法,例如 SetText 文字。

注意

所有以 Windows 為基礎的應用程式都會共用剪貼簿。 因此,當您切換到另一個應用程式時,內容可能會變更。

類別 Clipboard 只能在設定為單一執行緒 Apartment (STA) 模式的執行緒中使用。 若要使用此類別,請確定您的 Main 方法已標示為 STAThreadAttribute 屬性。

物件必須可序列化,才能放在剪貼簿上。 若要將類型設為可序列化,請使用 屬性標記它 SerializableAttribute 。 如果您將不可序列化的物件傳遞至剪貼簿方法,此方法將會失敗,而不會擲回例外狀況。 如需序列化的詳細資訊,請參閱 System.Runtime.Serialization

若要以單一通用格式將資料新增至剪貼簿

  1. SetAudio使用 、 SetFileDropListSetImageSetText 方法。

    // 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;
    }
    
    ' 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
    

若要以自訂格式將資料新增至剪貼簿

  1. 使用 SetData 方法搭配自訂格式名稱。

    您也可以搭配 方法使用預先定義的格式名稱 SetData 。 如需詳細資訊,請參閱DataFormats

    // 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;
        }
    }
    
    ' 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 string nameValue = string.Empty;
        public Customer(String name)
        {
            nameValue = name;
        }
        public string Name
        {
            get { return nameValue; }
            set { nameValue = value; }
        }
    }
    
    <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
    

若要以多種格式將資料新增至剪貼簿

  1. Clipboard.SetDataObject使用 方法並傳入 DataObject 包含您資料的 。

    // 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);
            }
        }
    }
    
    ' 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 string nameValue = string.Empty;
        public Customer(String name)
        {
            nameValue = name;
        }
        public string Name
        {
            get { return nameValue; }
            set { nameValue = value; }
        }
    }
    
    <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
    

另請參閱