クリップボード
クリップボードとは、コピー操作やカット操作などによって選択したデータを一時的に保存する機能、または、保存する場所のことである。
クリップボードは、本来は文具である用箋挟(ようせんばさみ)のことであり、それが転じて、コンピュータにおいて簡易的にデータを保存する機能のことをクリップボードと呼んでいる。
クリップボードは、データがアプリケーションに依存しない共有メモリに保持されるため、アプリケーション間でのデータのやり取りが容易にできる。クリップボードには、テキストだけでなく修飾情報や画像も保持することができるが、すべてのデータがペーストできるかどうかは、ペースト先のアプリケーションのフォーマットに依存する。
なお、一度、保持されたデータは再度コピー操作やカット操作、シャットダウンなどを行わない限り残っており、何度でもペーストすることができる。
Clipboard クラス
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)


Clipboard クラスで使用する定義済みの形式の一覧については、DataFormats クラスのトピックを参照してください。
データをクリップボードに貼り付けるには、SetDataObject を呼び出します。クリップボードにデータのコピーを永続的に貼り付けるには、copy パラメータを true に設定します。
![]() |
---|
データを複数の形式でクリップボードに貼り付けておくと、データを取り出す側のアプリケーションで使用できるデータ形式がわからなくても、データを正常に取得できます。 |
クリップボードからデータを取得するには、GetDataObject を呼び出します。取得対象のデータは、IDataObject インターフェイスを実装したオブジェクトとして返されます。IDataObject で指定されているメソッドと、DataFormats のフィールドを使用して、返されたオブジェクトからデータを抽出します。取得したデータの形式が不明な場合は、IDataObject インターフェイスの GetFormats メソッドを呼び出し、データの格納形式すべてのリストを取得します。次に IDataObject インターフェイスの GetData メソッドを呼び出し、取り出す側のアプリケーションで使用できる形式を指定します。
Microsoft .NET Framework version 2.0 では、Clipboard クラスに、システム クリップボードを簡単に処理できるようにする追加メソッドが用意されています。クリップボードからすべてのデータを削除するには、Clear メソッドを呼び出します。特定の形式のデータをクリップボードに追加するには、SetText などの適切な SetFormat メソッドを呼び出すか、SetData メソッドを呼び出してその形式を指定します。クリップボードから特定の形式のデータを取得するには、まず適切な ContainsFormat メソッド (ContainsText など) を呼び出してクリップボードにその形式のデータが含まれているかどうかを確認します。クリップボードにそのデータが含まれている場合は、次に適切な GetFormat メソッド (GetText など) を呼び出してデータを取得します。これらの操作の際に形式を指定するには、代わりに、ContainsData メソッドおよび GetData メソッドを呼び出します。
![]() |
---|
システム クリップボードはすべての Windows ベースのアプリケーションで共有されるため、その内容は、他のアプリケーションに切り替えた場合に変更されることがあります。 クリップボードに格納するには、オブジェクトはシリアル化可能である必要があります。シリアル化可能でないオブジェクトをクリップボードのメソッドに渡すと、メソッドは例外をスローせずに失敗します。シリアル化の詳細については、「シリアル化」を参照してください。対象のアプリケーションが、非常に限定されたデータ形式を必要とする場合は、シリアル化プロセスでデータにヘッダーを追加すると、アプリケーションがデータを認識できない場合があります。データ形式を維持するには、データを Byte 配列として MemoryStream に追加し、その MemoryStream を SetData メソッドに渡します。 Clipboard クラスは、シングル スレッド アパートメント (STA: Single Thread Apartment) モードに設定されているスレッドでだけ使用できます。このクラスを使用するには、Main メソッドが確実に STAThreadAttribute 属性でマークされているようにします。 クリップボードでメタファイル形式を使用する場合には、特別な配慮が必要な場合があります。DataObject クラスの現在の実装における制限により、.NET Framework で使用されるメタファイル形式は、旧メタファイル形式を使用するアプリケーションでは認識されない場合があります。この場合は、Win32 クリップボード アプリケーション プログラミング インターフェイス (API: Application Programming Interface) で相互運用する必要があります。詳細については、http://support.microsoft.com にある Microsoft サポート技術情報の文書 323530 (「Metafiles on Clipboard Are Not Visible to All Applications」) を参照してください。 |
Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows CE プラットフォームメモ : Windows CE では、クリップボードから貼り付けられたメモリ ストリームは、クリップボードにコピーされたメモリ ストリームよりもわずかに大きなサイズを保持できます。元のメモリ ストリームの最後に追加のバイトを付加できるからです。メモリ ストリームを厳密に受け取るには、オブジェクトがメモリ ストリームを受け取る方法を判断するためのサイズをプレフィックスとしてオブジェクトに追加するか、メモリ ストリームとそのサイズの文字列値を含む DataObject をクリップボードにコピーします。

Clipboard のメソッドを使用して、システム クリップボードにデータを貼り付けたり、システム クリップボードからデータを取得したりするコード例を次に示します。このコードは、button1、button2、textBox1、および textBox2 が既に作成され、フォームに配置されていることを前提にしています。
button1_Click メソッドは、SetDataObject を呼び出して、テキスト ボックス内で選択されているテキストを取り出し、システム クリップボードに貼り付けます。
button2_Click メソッドは、GetDataObject を呼び出して、システム クリップボードからデータを取得します。コードは IDataObject と DataFormats を使用して、返されたデータを抽出して textBox2 に表示します。
Private Sub button1_Click(sender As Object, e As System.EventArgs) ' Takes the selected text from a text box and puts it on the clipboard. If textBox1.SelectedText <> "" Then Clipboard.SetDataObject(textBox1.SelectedText) Else textBox2.Text = "No text selected in textBox1" End If End Sub 'button1_Click Private Sub button2_Click(sender As Object, e As System.EventArgs) ' Declares an IDataObject to hold the data returned from the clipboard. ' Retrieves the data from the clipboard. Dim iData As IDataObject = Clipboard.GetDataObject() ' Determines whether the data is in a format you can use. If iData.GetDataPresent(DataFormats.Text) Then ' Yes it is, so display it in a text box. textBox2.Text = CType(iData.GetData(DataFormats.Text), String) Else ' No it is not. textBox2.Text = "Could not retrieve data off the clipboard." End If End Sub 'button2_Click
private void button1_Click(object sender, System.EventArgs e) { // Takes the selected text from a text box and puts it on the clipboard. if(textBox1.SelectedText != "") Clipboard.SetDataObject(textBox1.SelectedText); else textBox2.Text = "No text selected in textBox1"; } private void button2_Click(object sender, System.EventArgs e) { // Declares an IDataObject to hold the data returned from the clipboard. // Retrieves the data from the clipboard. IDataObject iData = Clipboard.GetDataObject(); // Determines whether the data is in a format you can use. if(iData.GetDataPresent(DataFormats.Text)) { // Yes it is, so display it in a text box. textBox2.Text = (String)iData.GetData(DataFormats.Text); } else { // No it is not. textBox2.Text = "Could not retrieve data off the clipboard."; } }
private: void button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ ) { // Takes the selected text from a text box and puts it on the clipboard. if ( !textBox1->SelectedText->Equals( "" ) ) { Clipboard::SetDataObject( textBox1->SelectedText ); } else { textBox2->Text = "No text selected in textBox1"; } } void button2_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ ) { // Declares an IDataObject to hold the data returned from the clipboard. // Retrieves the data from the clipboard. IDataObject^ iData = Clipboard::GetDataObject(); // Determines whether the data is in a format you can use. if ( iData->GetDataPresent( DataFormats::Text ) ) { // Yes it is, so display it in a text box. textBox2->Text = (String^)(iData->GetData( DataFormats::Text )); } else { // No it is not. textBox2->Text = "Could not retrieve data off the clipboard."; } }
private void button1_Click(Object sender, System.EventArgs e) { // Takes the selected text from a text box and puts it on the clipboard. if (!textBox1.get_SelectedText().Equals("")) { Clipboard.SetDataObject(textBox1.get_SelectedText()); } else { textBox2.set_Text("No text selected in textBox1"); } } //button1_Click private void button2_Click(Object sender, System.EventArgs e) { // Declares an IDataObject to hold the data returned from the clipboard. // Retrieves the data from the clipboard. IDataObject iData = Clipboard.GetDataObject(); // Determines whether the data is in a format you can use. if (iData.GetDataPresent(DataFormats.Text)) { // Yes it is, so display it in a text box. textBox2.set_Text((String)(iData.GetData(DataFormats.Text))); } else { // No it is not. textBox2.set_Text("Could not retrieve data off the clipboard."); } } //button2_Click
private function button1_Click(sender : Object, e : System.EventArgs) { //Take the selected text from a text box and put it on the clipboard. if(textBox1.SelectedText != "") Clipboard.SetDataObject(textBox1.SelectedText); else textBox2.Text = "No text selected in textBox1"; } private function button2_Click(sender : Object, e : System.EventArgs) { //Declare an IDataObject to hold the data returned from the clipboard. //Then retrieve the data from the clipboard. var iData : IDataObject = Clipboard.GetDataObject(); //Determine whether the data is in a format you can use. if(iData.GetDataPresent(DataFormats.Text)) { //Yes it is, so display it in a text box. textBox2.Text = String(iData.GetData(DataFormats.Text)); } else { //No it is not. textBox2.Text = "Could not retrieve data off the clipboard."; } }

System.Windows.Forms.Clipboard


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Clipboard メンバ
System.Windows.Forms 名前空間
DataObject
DataFormats
IDataObject
その他の技術情報
Windows フォームのセキュリティに関するその他の考慮事項
Clipboard メソッド


名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 ( Object から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 ( Object から継承されます。) |

関連項目
Clipboard クラスSystem.Windows.Forms 名前空間
DataObject
DataFormats
IDataObject
その他の技術情報
Windows フォームのセキュリティに関するその他の考慮事項Clipboard メンバ
システム クリップボードにデータを貼り付けるメソッド、またはシステム クリップボードのデータを取得するメソッドを提供します。このクラスは継承できません。
Clipboard データ型で公開されるメンバを以下の表に示します。


名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 (Object から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 (Object から継承されます。) |

関連項目
Clipboard クラスSystem.Windows.Forms 名前空間
DataObject
DataFormats
IDataObject
その他の技術情報
Windows フォームのセキュリティに関するその他の考慮事項- Clipboardのページへのリンク