方法 : クリップボードからデータを取得する
更新 : 2007 年 11 月
Clipboard クラスには、Windows オペレーティング システムのクリップボード機能とやり取りするためのメソッドが用意されています。クリップボードは、データの一時的なレポジトリとして多くのアプリケーションで使用されます。たとえば、ワード プロセッサは、切り取り、貼り付けの操作でクリップボードを使用します。クリップボードはデータをアプリケーション間で移動させるときにも使用します。
アプリケーションによっては、データが複数の形式でクリップボードに格納される場合もあります。そうすることで、より多くのアプリケーションがデータを使用できるからです。クリップボードの形式は、その形式を識別する文字列です。識別された形式を使用するアプリケーションでは、クリップボード上の関連データを取得できます。DataFormats クラスにはユーザーが使用できる定義済みの形式名が含まれています。また、独自の形式名を使用したり、オブジェクトの型を形式名として使用したりできます。データをクリップボードに追加する方法については、「方法 : クリップボードにデータを追加する」を参照してください。
特定の形式のデータがクリップボードに含まれているかどうかを調べるには、いずれかの ContainsFormat メソッドを使用するか、または GetData メソッドを使用します。クリップボードからデータを取得するには、いずれかの GetFormat メソッドを使用するか、GetData メソッドを使用します。これらは .NET Framework version 2.0 で新しく追加されたメソッドです。
.NET Framework 2.0 よりも前のバージョンを使用してクリップボードのデータにアクセスする場合は、GetDataObject メソッドを使用し、返された IDataObject のメソッドを呼び出します。返されたオブジェクト内で特定の形式が利用可能かどうかを調べるには、たとえば GetDataPresent メソッドを呼び出します。
メモ : |
---|
Windows ベースのアプリケーションはすべて、システムのクリップボードを共有します。そのため、別のアプリケーションに切り替えるとクリップボードの内容は変わります。 Clipboard クラスは、シングル スレッド アパートメント (STA: Single-Threaded Apartment) モードに設定されたスレッドでのみ使用できます。このクラスを使用する前に、Main メソッドが STAThreadAttribute 属性でマーク付けされていることを確認してください。 |
クリップボードからデータを単一の一般的な形式で取得するには
GetAudioStream、GetFileDropList、GetImage、または GetText メソッドを使用します。必要に応じて、最初に対応する ContainsFormat のメソッドを使用して、データが特定の形式で利用可能かどうかを判断することもできます。これらのメソッドは .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; }
クリップボードからデータをカスタム形式で取得するには
カスタム形式の名前を指定して GetData メソッドを使用します。このメソッドは .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; } } }
クリップボードからデータを複数の形式で取得するには
GetDataObject メソッドを使用します。.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; } } }