Clipboard.GetDataObject 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
擷取系統剪貼簿上目前的資料。
public:
static System::Windows::Forms::IDataObject ^ GetDataObject();
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
傳回
代表 IDataObject 剪貼簿上目前的資料,或 null 是夾板上沒有資料。
例外狀況
資料無法從剪貼簿中取得。 這通常發生在夾板被其他程序使用時。
目前執行緒並非處於單執行緒公寓(STA)模式,屬性 MessageLoop 值為 true。 把STAThreadAttribute它Main加入你的申請方法。
範例
以下程式碼範例使用 Clipboard 方法將資料放入系統剪貼簿並取得資料。 此代碼假設 button1、 button2、 textBox1、 textBox2 已放置在表單上。
此 button1_Click 方法呼叫 SetDataObject 從文字框中選取的文字,並將其放置到系統的剪貼簿中。
此 button2_Click 方法呼叫 GetDataObject 從系統剪貼簿擷取資料。 程式碼使用 IDataObject 和 DataFormats 來擷取回傳的資料。 資料以 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
備註
由於從剪貼簿回傳的物件資料型態可能不同,此方法會以 IDataObject. 接著你可以利用介面的方法 IDataObject ,以正確的資料型別擷取資料。
此方法嘗試以100毫秒為間隔取得十次資料,若所有嘗試皆未成功則拋出 。ExternalException
Note
Clipboard 類別只能在設定為單一執行緒 Apartment (STA) 模式的執行緒中使用。 若要使用此類別,請確定您的 Main 方法已標示為 STAThreadAttribute 屬性。