如何prevent的RichTextBox内粘贴的图片? [英] How to prevent richTextBox to paste images within it?
本文介绍了如何prevent的RichTextBox内粘贴的图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我编程在C#。和我有一个RichTextBox就可以了。
I am programming in c#. and I've a richTextBox on it.
在运行时,我通过编码中插入一些位图图像到RichTextBox中。
但我想这$拖我插入图像P $ pvent用户或RichTextBox中粘贴一些其他的图像。
At runtime, I insert some Bitmap images into richTextbox by coding. But I want to prevent user that drag my inserted Images or paste some other images in richTextBox.
我怎么能实现呢?
在此先感谢!
推荐答案
我用这个code和它完美的作品:
I used this code and it works perfectly:
private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
//if ((Control.ModifierKeys & Keys.Control) == Keys.Control && (e.KeyChar == 'V' || e.KeyChar == 'v'))
if (((int)e.KeyChar) == 22)
{
if(hasImage(Clipboard.GetDataObject()));
{
e.Handled = true;
richTextBox1.Undo();
MessageBox.Show("can't paste image here!!");
}
}
}
private Boolean hasImage(object obj)
{
System.Windows.Forms.RichTextBox richTextBox = new System.Windows.Forms.RichTextBox();
Object data = Clipboard.GetDataObject();
Clipboard.SetDataObject(data);
richTextBox.Paste();
Clipboard.SetDataObject(data);
int offset = richTextBox.Rtf.IndexOf(@"\f0\fs17") + 8; // offset = 118;
int len = richTextBox.Rtf.LastIndexOf(@"\par") - offset;
return richTextBox.Rtf.Substring(offset, len).Trim().Contains(@"{\pict\");
}
和RichTextBox中禁用的DragDrop。
谢谢
And disabled dragdrop in richTextBox.
thanks
这篇关于如何prevent的RichTextBox内粘贴的图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文