jeudi 2 avril 2015

Disable image paste in RichTextBox in Winforms

We have an UserControls which herits from the RichTextBox. We would like to forbid the user to enter any image(with copy paste) in this user control.


I found several places where speaking of this:



Currently I've this solution:



protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys)Shortcut.CtrlV || keyData == (Keys)Shortcut.ShiftIns)
{
if (Clipboard.ContainsImage())
{
return false;
}
}
return base.ProcessCmdKey(ref msg, keyData);
}


Which works for copy paste with CTRL+C-CTRL+V, but not with the contextual menu.


EDIT


I tried the given proposition:



public class CustomRichBox : RichTextBox
{
private const int WM_PASTE = 0x0302;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_PASTE )
{
if (Clipboard.ContainsImage())
{
return;
}
}
base.WndProc(ref m);
}
}


But when I do the CTRL+V, I don't receive this message


Aucun commentaire:

Enregistrer un commentaire