如何检测RichTextBox中多行糊 [英] How to detect multiline paste in RichTextBox

查看:178
本文介绍了如何检测RichTextBox中多行糊的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在一个简单的语法高亮,我有几个问题。你能帮帮我吗?

At the moment I'm working on a simple syntax highlighter and I have couple of problems. Could you help me out?

我有一个类库,在它的组件类。一切都在VB.NET。它只有一个文件,这样就可以在这里 https://gist.github.com/2366507 看到它。在线92,有OnTextChanged子。我在想加入ProcessAllLines()(作为线128)子的结束,和它的工作。我用然而,当我在输入code到RichTextBox的(来源就在这里的https://gist.github .COM / 2366526 )后,每个文本改变了检查和处理的所有行。所以,我在OnTextChanged子删除ProcessAllLines()。

I have a class library with a component class in it. Everything is in VB.NET. It's only one file so you can see it here https://gist.github.com/2366507 . On line 92, there is the OnTextChanged Sub. I was thinking about adding ProcessAllLines() (as on line 128) to the end of that Sub, and it worked. However when I was typing in code to the RichTextBox (source which I used is here https://gist.github.com/2366526) after each text change it was checking and processing ALL the lines. So I deleted ProcessAllLines() in the OnTextChanged Sub.

我在想运行ProcessAllLines()当用户粘贴一些东西到SyntaksRichTextBox。不过,我不知道该怎么做。我知道,它应该是这样的:

I'm thinking about running ProcessAllLines() when the user pastes something to the SyntaksRichTextBox. However I do not know how to do that. I know that it should be something like:

If [CTRL Pressed] And [V Pressed] Then
ProcessAllLines()
End If

我也希望它成为类库,而不是应用程序(Syntaks演示)。你能帮助我在这里?谢谢你。

I also want it to be in the class library, not the application (Syntaks Demo). Could you help me out here? Thanks.

拉​​法尔Chmiel,@RafalChmiel

Rafal Chmiel, @RafalChmiel

推荐答案

首先解决方案,您可能想的是检测 WM_PASTE 信息覆盖的WndProc 可惜的控制没有按'不要发出该消息时,它本身进行粘贴操作。

Naïve detection

First solution you may think about is 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)是一个将文本粘贴你可以解决这个问题。

Detecting keyboard events (you have to override the OnKeyDown function) and checking if the key combinations (CTRL+V and SHIFT+INS) is the one to paste text you may solve this.

Protected Sub OnKeyDown(ByVal e As KeyEventArgs)
    Dim ctrlV As Boolean = e.Modifiers = Keys.Control && e.KeyCode = Keys.V
    Dim shiftIns As Boolean = e.Modifiers = Keys.Shift && e.KeyCode = Keys.Insert

    If ctrlV Or shiftIns Then
        ProcessAllLines
    End If
End Sub

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

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代理人的并不总是正确的,但它不是在这种情况下的一个问题)。

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 (this isn't always true because of Unicode surrogates but it's not a problem in this case).

它不与每一个输入法正常工作(我没有与远东语言的尝试,例如),并与统一code代理人但对于西方语言还是可以的(反正即使不工作,你会简单地重新处理所有行)。另请阅读这篇文章这帖子约统一code更多的细节和(双胞胎)的C#版本的答案。

It doesn't work well with every IME (I didn't try with far east languages, for example) and with Unicode surrogates but for western languages it's OK (anyway even when it doesn't work you'll simply reprocess all lines). Also read this post and this post for more details about Unicode and this (twin) answer for C# version.

Dim _previousLength As Int32 = 0

Protected Sub richTextBox_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
   Dim currentLength As Int32 = richTextBox.Text.Length
   If Math.Abs(currentLength - _previousLength) > 1 Then
      ProcessAllLines
   End If

   _previousLength = currentLength
End Sub

完美的解决方案

当然,完美的解决方案存在(在Windows 8或更高),本地丰富的编辑控件发送一个 EN_CLIPFORMAT 的通知消息。这是为了通知了丰富的编辑控件的一个糊发生与一个特定的剪贴板格式的父窗口。然后,您可以覆盖其母公司的的WndProc 检测 WM_NOTIFY 消息此通知。反正它不是code几行,查MSDN上这个链接的详细信息:的http://msdn.microsoft.com/en-us/library/windows/desktop/hh768385(v=vs.85).aspx.

Perfect solution

The perfect solution of course exists (on Windows 8 or higher), the native rich edit control sends a 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 link on MSDN for details: http://msdn.microsoft.com/en-us/library/windows/desktop/hh768385(v=vs.85).aspx.

这篇关于如何检测RichTextBox中多行糊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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