Clipboard 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供將資料放入及從系統剪貼簿取回資料的方法。 此類別無法獲得繼承。
public ref class Clipboard sealed
public sealed class Clipboard
type Clipboard = class
Public NotInheritable Class Clipboard
- 繼承
-
Clipboard
範例
以下程式碼範例使用 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
備註
關於用於該 Clipboard 類別的預設格式列表,請參見該 DataFormats 類別。
呼叫 SetDataObject 將資料放入夾板,替換其目前的內容。 若要將資料的持久副本放入剪貼簿,請將參數設 copy 為 true。
Note
若要將資料以多種格式放入剪貼簿,請使用類別 DataObject 或實 IDataObject 作。 將資料以多種格式放置於剪貼簿上,以最大化目標應用程式(你可能不了解其格式需求)成功取得資料的可能性。
打電話 GetDataObject 從剪貼簿取資料。 資料會以實作 IDataObject 介面的物件形式回傳。 使用欄位IDataObjectDataFormats中指定的方法,從物件中擷取資料。 如果你不知道所取得資料的格式,請呼叫 GetFormats 介面的方法 IDataObject ,取得該資料所儲存的所有格式清單。 接著呼叫GetDataIDataObject介面的方法,並指定你的應用程式可以使用的格式。
課程 Clipboard 提供額外方法,使系統剪貼簿的操作更為簡便。 呼叫 Clear 移除剪貼簿中所有資料的方法。 若要將特定格式的資料加入剪貼簿,替換現有資料,請呼叫適當的 Set格式 方法,例如 SetText,或呼叫 SetData 該方法來指定格式。 要從剪貼簿擷取特定格式的資料,首先呼叫適當的 Contains格式 方法(例如 ContainsText)方法來判斷夾板是否包含該格式的資料,然後再呼叫適當的 Get格式 方法(例如 GetText)來擷取夾板是否包含該資料。 若要指定這些操作的格式,請呼叫 and ContainsDataGetData 方法。
Note
所有基於 Windows 的應用程式共用系統剪貼簿,因此當你切換到其他應用程式時,內容可能會有所變動。
物件必須可序列化,才能放在剪貼簿上。 如果你把一個不可序列化的物件傳給剪貼簿方法,該方法會失敗且不會拋出例外。 更多關於序號的資訊請參見 System.Runtime.Serialization 。 如果你的目標應用程式需要非常特定的資料格式,序列化過程中新增的標頭可能會阻止應用程式辨識你的資料。 為了保留資料格式,將資料加入陣Byte列到 aMemoryStream,然後傳給MemoryStreamSetData方法。
Clipboard 類別只能在設定為單一執行緒 Apartment (STA) 模式的執行緒中使用。 若要使用此類別,請確定您的 Main 方法已標示為 STAThreadAttribute 屬性。
在使用剪貼簿的元檔案格式時,可能需要特別注意。 由於目前 DataObject 類別實作的限制,.NET 框架所使用的元檔案格式可能無法被使用較舊元檔案格式的應用程式識別。 在這種情況下,你必須與 Win32 剪貼簿應用程式介面(API)互通。