Clipboard クラスとは? わかりやすく解説

Clipboard クラス

システム クリップボードデータ貼り付けるメソッド、またはシステム クリップボードデータ取得するメソッド提供します。このクラス継承できません。

名前空間: System.Windows.Forms
アセンブリ: 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 PCWindows Mobile for SmartphoneWindows CE プラットフォームメモ : Windows CE では、クリップボードから貼り付けられメモリ ストリームは、クリップボードコピーされメモリ ストリームよりもわずかに大きなサイズ保持できます。元のメモリ ストリーム最後に追加バイト付加できるからです。メモリ ストリーム厳密に受け取るには、オブジェクトメモリ ストリーム受け取方法判断するためのサイズプレフィックスとしてオブジェクト追加するか、メモリ ストリームとそのサイズ文字列値を含む DataObjectクリップボードコピーします

使用例使用例

Clipboardメソッド使用してシステム クリップボードデータ貼り付けたりシステム クリップボードからデータ取得したりするコード例次に示します。このコードは、button1button2textBox1、および textBox2 が既に作成されフォーム配置されていることを前提にしています。

button1_Click メソッドは、SetDataObject呼び出してテキスト ボックス内で選択されているテキスト取り出しシステム クリップボード貼り付けます。

button2_Click メソッドは、GetDataObject呼び出してシステム クリップボードからデータ取得しますコードIDataObjectDataFormats使用して返されデータ抽出して 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.Object
  System.Windows.Forms.Clipboard
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「Clipboard クラス」の関連用語

Clipboard クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Clipboard クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS