RichTextBox.Paste(DataFormats+Format) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
以指定的剪貼簿格式貼上剪貼簿內容。
public:
void Paste(System::Windows::Forms::DataFormats::Format ^ clipFormat);
public void Paste(System.Windows.Forms.DataFormats.Format clipFormat);
override this.Paste : System.Windows.Forms.DataFormats.Format -> unit
Public Sub Paste (clipFormat As DataFormats.Format)
參數
- clipFormat
- DataFormats.Format
剪貼簿格式,資料應從剪貼簿取得。
範例
以下程式碼範例示範如何使用此 Paste 方法將點陣圖貼入控制項 RichTextBox 。 在從檔案開啟位圖後,範例使用 SetDataObject 方法將點陣圖複製到 Windows 剪貼簿。 最後,範例會取得物件 Bitmap 的格式,驗證該格式是否可以貼上到 RichTextBox 控制項中,並使用該 Paste 方法來貼上資料。
private:
bool pasteMyBitmap( String^ fileName )
{
// Open an bitmap from file and copy it to the clipboard.
Bitmap^ myBitmap = gcnew Bitmap( fileName );
// Copy the bitmap to the clipboard.
Clipboard::SetDataObject( myBitmap );
// Get the format for the object type.
DataFormats::Format^ myFormat = DataFormats::GetFormat( DataFormats::Bitmap );
// After verifying that the data can be pasted, paste it.
if ( richTextBox1->CanPaste( myFormat ) )
{
richTextBox1->Paste( myFormat );
return true;
}
else
{
MessageBox::Show( "The data format that you attempted to paste is not supported by this control." );
return false;
}
}
private bool pasteMyBitmap(string fileName)
{
// Open an bitmap from file and copy it to the clipboard.
Bitmap myBitmap = new Bitmap(fileName);
// Copy the bitmap to the clipboard.
Clipboard.SetDataObject(myBitmap);
// Get the format for the object type.
DataFormats.Format myFormat = DataFormats.GetFormat(DataFormats.Bitmap);
// After verifying that the data can be pasted, paste it.
if(richTextBox1.CanPaste(myFormat))
{
richTextBox1.Paste(myFormat);
return true;
}
else
{
MessageBox.Show("The data format that you attempted to paste is not supported by this control.");
return false;
}
}
Private Function PasteMyBitmap(ByVal Filename As String) As Boolean
'Open an bitmap from file and copy it to the clipboard.
Dim MyBitmap As Bitmap
MyBitmap = Bitmap.FromFile(Filename)
' Copy the bitmap to the clipboard.
Clipboard.SetDataObject(MyBitmap)
' Get the format for the object type.
Dim MyFormat As DataFormats.Format = DataFormats.GetFormat(DataFormats.Bitmap)
' After verifying that the data can be pasted, paste it.
If RichTextBox1.CanPaste(MyFormat) Then
RichTextBox1.Paste(MyFormat)
PasteMyBitmap = True
Else
MessageBox.Show("The data format that you attempted to paste is not supported by this control.")
PasteMyBitmap = False
End If
End Function
備註
你可以用這個方法把剪貼簿的資料貼到控制鍵裡。 此版本 Paste 與原方法 TextBoxBase.Paste 不同,因為它允許您僅貼上指定的剪貼簿格式文字。 你可以用這個 CanPaste 方法判斷夾板內的資料是否符合指定的格式。 接著你可以呼叫這個版本 Paste 的方法,確保貼上操作的檔案格式正確。