RichTextBox..::.CanPaste 方法

引自msdn

确定是否可以粘贴指定数据格式的剪贴板信息。

如果可以粘贴指定数据格式的剪贴板数据,则为 true;否则为 false

可以使用此方法在允许用户将剪贴板的当前内容粘贴到 RichTextBox 控件中之前,确定此信息是否为指定的剪贴板数据格式。

下面的代码示例演示如何使用 Paste 方法将位图粘贴到 RichTextBox 控件中。从文件打开位图后,此示例使用 SetDataObject 方法将位图复制到 Windows 剪贴板。最后,此示例检索 Bitmap 对象的格式,使用 CanPaste 方法来验证是否可以将该格式粘贴到 RichTextBox 控件中,然后使用 Paste 方法来粘贴数据。

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;
 }
}

posted @ 2011-06-12 00:31  __陆杨  阅读(350)  评论(0编辑  收藏  举报