本文介紹了如何防止richTextBox在其中粘貼圖像?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在用 C# 編程.我有一個 RichTextBox.
I am programming in c#. and I've a richTextBox on it.
在運行時,我通過編碼將一些位圖圖像插入到richTextbox中.但我想阻止用戶拖動我插入的圖像或在 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.
我該如何實現?
提前致謝!
推薦答案
我使用了這段代碼,效果很好:
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(@"f0fs17") + 8; // offset = 118;
int len = richTextBox.Rtf.LastIndexOf(@"par") - offset;
return richTextBox.Rtf.Substring(offset, len).Trim().Contains(@"{pict");
}
并在richTextBox中禁用拖放.
謝謝
And disabled dragdrop in richTextBox.
thanks
這篇關于如何防止richTextBox在其中粘貼圖像?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!