如果检测里面的一个格式文本框粘贴事件发生 [英] Detecting if paste event occurred inside a rich text box

查看:588
本文介绍了如果检测里面的一个格式文本框粘贴事件发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法通过它,我们可以发现,如果在格式文本框中剪贴板粘贴事件发生?此事件将被使用,以做某些东西,文字的粘贴块。

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.

感谢

下面是我的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);
        }
    }

修改

我希望做一些语法高亮和缩进基于粘贴事件,一些东西,这个特殊的 code编辑似乎是在做非常有效。我不知道它是怎么做的。将需要在该特定方向上的帮助。我是pretty的肯定,必须有一些本地的Win32 code或类似的东西可以被拦截。我试图追查按键,鼠标事件,这是不是pretty的。

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.

推荐答案

这是一个有点棘手检测粘贴操作的的RichTextBox

It's a little bit tricky to detect a paste operation in the RichTextBox.

首先解决方法可能检测 WM_PASTE 信息覆盖的WndProc 但遗憾的是该控件不发送消息时,它本身进行粘贴操作。

First solution may be to detect the WM_PASTE message overriding the WndProc but unfortunately the control doesn't send that message to itself when it performs a paste operation.

要检测键盘事件可能工作(你必须重写的onkeydown 函数),然后检查组合键( CTRL + <大骨节病> V 和<大骨节病> SHIFT + <大骨节病> INS )。事情是这样的:

To detect the keyboard events may work (you have to override the OnKeyDown function) then check if the key combinations (CTRL+V and SHIFT+INS). Something like this:

protected override OnKeyDown(KeyEventArgs e)
{
     bool ctrlV = e.Modifiers == Keys.Control && e.KeyCode == Keys.V;
     bool shiftIns = e.Modifiers == Keys.Shift && e.KeyCode == Keys.Insert;

     if (ctrlV || shiftIns)
         DoSomething();
}

它工作得很好,但你不能赶上使用鼠标(右键打开上下文菜单),并通过拖放放大器制成的粘贴操作所作的粘贴操作;下降。如果你不需要他们,你可以使用此解决方案(至少是简单和直接的)。

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).

假设:当用户类型在的RichTextBox 插入他每次一个字符。如何使用呢?那么,当你发现一个较大的变化,你发现粘贴操作,因为用户不能每次(好吧,你可以说,这是因为统一code代理人并非总是如此)型不止一次的性格。另请参见 VB.NET版本和的有关统一code 的东西更多的细节。

Assumption: when user types inside the RichTextBox he 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.

private int _previousLength = 0;

private void richTextBox_TextChanged(object sender, EventArgs e)
{
   int currentLength = richTextBox.Text.Length;
   if (Math.Abs(currentLength - _previousLength) > 1)
      ProcessAllLines();

   _previousLength = currentLength;
}

请注意(因为不同的输入法是如何工作的),你不能使用的onkeydown (或类似)。这非常适用仅供西方语言,但有问题的Uni code的东西(因为,​​例如, string.length减属性可以增加两个字符当用户输入一个字符。参见这个帖子有关更多详细信息这个(当然这是一个强烈建议阅读,甚至,哪怕 - 在这种情况下 - 你不关心它)。在这个职位,你还可以找到$ C $下一个更好的算法来确定字符串的长度总之你已经取代:

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.Length property may be increased by two Char when 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:

   int currentLength = richTextBox.Text.Length;

通过这样的:

   int currentLength = StringInfo.GetTextElementEnumerator(richTextBox.Text)
       .Cast<string>()
       .Count();

在这一切的努力,你可能会发现...用户甚至可以贴上一个字符,它可能不被发现。你说的没错,这就是为什么这是一个的更好的检测的而不是完美的解决方案的。

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.

完美的解决方案(如果你在Windows 8上运行),当然存在,本地丰富的编辑控件发送一个 EN_CLIPFORMAT 的通知消息。这是为了通知了丰富的编辑控件的一个糊发生与一个特定的剪贴板格式的父窗口。然后,您可以覆盖其母公司的的WndProc 检测 WM_NOTIFY 消息此通知。反正这不是code几行字,看看这个的 MSDN文章细节。

The perfect solution (if you're running on Windows 8) of course exists, the native rich edit control sends an EN_CLIPFORMAT notification 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 the WndProc of its parent to detect the WM_NOTIFY message for this notification. Anyway it's not few lines of code, check this MSDN article for details.

这篇关于如果检测里面的一个格式文本框粘贴事件发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆