Clipboard.GetDataObject Método

Definição

Recupera os dados que estão atualmente na área de transferência do sistema.

public:
 static System::Windows::Forms::IDataObject ^ GetDataObject();
public static System.Windows.Forms.IDataObject GetDataObject();
static member GetDataObject : unit -> System.Windows.Forms.IDataObject
Public Shared Function GetDataObject () As IDataObject

Retornos

Um IDataObject que representa os dados atualmente na Área de Transferência ou null se não há dados na Área de Transferência.

Exceções

Não foi possível recuperar dados da Área de Transferência. Isso normalmente ocorre quando a Área de Transferência está sendo usada por outro processo.

O thread atual não está no modo STA (apartamento com thread único) e o valor da MessageLoop propriedade é true. Adicione o STAThreadAttribute método do Main aplicativo.

Exemplos

O exemplo de código a seguir usa métodos Clipboard para colocar dados e recuperá-los da Área de Transferência do sistema. Esse código pressupõebutton1, button2e textBox2textBox1foi colocado no formulário.

O button1_Click método chama SetDataObject para pegar o texto selecionado na caixa de texto e colocá-lo na Área de Transferência do sistema.

O button2_Click método chama GetDataObject para recuperar dados da área de transferência do sistema. O código usa e DataFormats para extrair os dados retornadosIDataObject. Os dados são exibidos em textBox2.

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

Comentários

Como o tipo de dados do objeto retornado da Área de Transferência pode variar, esse método retorna os dados em um IDataObject. Em seguida, você pode usar métodos da IDataObject interface para extrair os dados em seu tipo de dados adequado.

Esse método tenta obter os dados dez vezes em intervalos de 100 milissegundos e lança um ExternalException se todas as tentativas não tiverem êxito.

Note

A classe Clipboard só pode ser usada em threads configurados no modo STA (Single Thread Apartment). Para usar essa classe, verifique se o método Main está marcado com o atributo STAThreadAttribute.

Aplica-se a

Confira também