Is there a way by which we can find out if a clip board paste event occurred in a rich text box? This event would be used in order to do certain stuff, with the pasted block of text.
thanks
Here is my code
protected override void WndProc(ref System.Windows.Forms.Message m)
{
if (m.Msg == WM_PASTE)
{
OnPasteOccurred();
MessageBox.Show("Pas");
}
if (m.Msg == 0x000F)
{
if (PaintControl)
{
base.WndProc(ref m);
}
else
{
m.Result = IntPtr.Zero;
}
}
else
{
base.WndProc(ref m);
}
}
Edit
I wish to do some syntax highlighting or indentation based on paste events, something which this particular code editor seems to be doing very efficiently. I don’t know how it is doing it. Would require help in this particular direction. I am pretty sure that there must some native Win32 code or something like that can be intercepted. I have tried tracking down keys, mouse events and it is not pretty.
It’s a little bit tricky to detect a paste operation in the
RichTextBox.First solution may be to detect the
WM_PASTEmessage overriding theWndProcbut unfortunately the control doesn’t send that message to itself when it performs a paste operation.Naïve detection
To detect the keyboard events may work (you have to override the
OnKeyDownfunction) then check if the key combinations (CTRL+V and SHIFT+INS). Something like this:It works well but you can’t catch the paste operation made using the mouse (right click to open the context menu) and the paste operations made via drag & drop. If you do not need them you can use this solution (at least it’s simply and straightforward).
Better detection
Assumption: when user types inside the
RichTextBoxhe inserts one character per time. How can you use this? Well, when you detect a bigger change you detected a paste operation because user can’t type more than once character per time (ok, you can argue that it’s not always true because of Unicode surrogates). See also VB.NET version and more details about Unicode stuff.Please note that you can’t (because of how different IMEs work) use
OnKeyDown(or similar). This works well only for western languages but it has problems with Unicode stuff (because, for example,String.Lengthproperty may be increased by twoCharwhen user typed a single character. See also this post for much more details about this (well it’s a strongly suggested reading even, even if – in this case – you don’t care about it). In that post you’ll also find code for a better algorithm to determine string length. In short you have to replace:With this:
After all this effort you may realize that…user can even paste a single character and it may go undetected. You’re right, that’s why this is a better detection instead of a perfect solution.
Perfect solution
The perfect solution (if you’re running on Windows 8) of course exists, the native rich edit control sends an
EN_CLIPFORMATnotification message. It’s intended to notify a rich edit control’s parent window that a paste occurred with a particular clipboard format. You can then override theWndProcof its parent to detect theWM_NOTIFYmessage for this notification. Anyway it’s not few lines of code, check this MSDN article for details.