Share via


Nasıl yapılır: Panodan Veri Alma

sınıfı, Clipboard Windows işletim sistemi Pano özelliğiyle etkileşime geçmek için kullanabileceğiniz yöntemler sağlar. Birçok uygulama Pano'yu veriler için geçici bir depo olarak kullanır. Örneğin, sözcük işlemcileri kesme ve yapıştırma işlemleri sırasında Pano'yı kullanır. Pano, bilgileri bir uygulamadan diğerine aktarmak için de kullanışlıdır.

Bazı uygulamalar, verileri kullanabilecek diğer uygulamaların sayısını artırmak için panodaki verileri birden çok biçimde depolar. Pano biçimi, biçimi tanımlayan bir dizedir. Tanımlanan biçimi kullanan bir uygulama Pano'da ilişkili verileri alabilir. DataFormats sınıfı, kullanımınız için önceden tanımlanmış biçim adları sağlar. Ayrıca kendi biçim adlarınızı kullanabilir veya bir nesnenin türünü biçimi olarak kullanabilirsiniz. Panoya veri ekleme hakkında bilgi için bkz . Nasıl yapılır: Panoya Veri Ekleme.

Pano'nun belirli bir biçimde veri içerip içermediğini belirlemek için Format yöntemlerinden Containsbirini veya GetData yöntemini kullanın. Pano'dan veri almak için Format yöntemlerinden Getbirini veya GetData yöntemini kullanın. Bu yöntemler .NET Framework 2.0'da yenidir.

.NET Framework 2.0'dan önceki sürümleri kullanarak Pano'dan verilere erişmek için yöntemini kullanın Clipboard.GetDataObject ve döndürülen IDataObjectyöntemlerini çağırın. Döndürülen nesnede belirli bir biçimin kullanılabilir olup olmadığını belirlemek için yöntemini çağırın GetDataPresent .

Dekont

Tüm Windows tabanlı uygulamalar sistem Panosu'na sahip olur. Bu nedenle, başka bir uygulamaya geçiş yaptığınızda içerik değiştirilebilir.

Clipboard sınıfı yalnızca tek iş parçacığı dairesi (STA) moduna ayarlanmış iş parçacıklarında kullanılabilir. Bu sınıfı kullanmak için yönteminizin Main özniteliğiyle işaretlendiğinden STAThreadAttribute emin olun.

Pano'dan tek ve ortak bir biçimde veri almak için

  1. GetAudioStream, , GetFileDropListGetImageveya GetText yöntemini kullanın. İsteğe bağlı olarak, verilerin belirli bir biçimde kullanılabilir olup olmadığını belirlemek için önce ilgili ContainsFormat yöntemlerini kullanın. Bu yöntemler yalnızca .NET Framework 2.0'da kullanılabilir.

    // 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
    

Pano'dan özel biçimde veri almak için

  1. GetData yöntemini özel biçim adıyla kullanın. Bu yöntem yalnızca .NET Framework 2.0'da kullanılabilir.

    Yöntemiyle SetData önceden tanımlanmış biçim adları da kullanabilirsiniz. Daha fazla bilgi için bkz. 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
    

Pano'dan birden çok biçimde veri almak için

  1. GetDataObject yöntemini kullanın. .NET Framework 2.0'dan önceki sürümlerde Pano'dan veri almak için bu yöntemi kullanmanız gerekir.

    // 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
    

Ayrıca bkz.