如何加快基本语法荧光笔的显示速度? [英] How to Speed Up A Basic Syntax Highlighter?

查看:83
本文介绍了如何加快基本语法荧光笔的显示速度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

因此,就像很多其他人一样,我认为我会尝试为关键字着色.并不是说我需要任何严肃的东西...只是觉得尝试会很有趣...到目前为止,它仍然很有趣!

无论如何,我的作品相当不错.但是,我想知道是否有任何方法可以加快速度.

请记住...我是这个的初学者,所以我可能在做些古怪的事情!如果我这样做了,请告诉我.

因此,我所拥有的是带有以下代码的继承的richtextbox:

Hello All!

So, just like a lot of others, I thought I''d try my hand at colorizing keywords. Not that I need it for anything serious... just figured it would be fun to try... and so far it has been fun!

Anyways, what I have works pretty good. However, I''m wondering if there is any way to speed things up a bit.

Keep in mind... I am a beginner at this so I may be doing something wacko! If I did, please let me know.

So, what I have is an inherited richtextbox with the following code:

Private Sub ColorizePastedText()
        Dim CursorPosition As Integer = Me.SelectionStart
        Dim RegXMatches As MatchCollection
        Dim RegXMatch As Match
        Dim tmpRTB As New RichTextBox
        ''LastRTBLength is the Textlength before the paste. RTB TextLength stored on key down event
        Dim PasteLength As Integer = Me.TextLength - LastRTBLength
        ''Get rid of the RTB flickering
        LockWindowUpdate(Me.Handle.ToInt32)
        tmpRTB.Rtf = Me.Rtf
        tmpRTB.SelectAll()
        tmpRTB.SelectionFont = Me.Font
        ''Find keywords then colorize them
        For Each KeyWord In KeywordList
            RegXMatches = System.Text.RegularExpressions.Regex.Matches(tmpRTB.Text, "\b" & KWord & "\b")
            For Each RegXMatch In RegXMatches
                If RegXMatch.Index >= LastCurPos AndAlso RegXMatch.Index < LastCurPos + PasteLength Then ''Colorize only what was just pasted
                    tmpRTB.Select(RegXMatch.Index, RegXMatch.Length)
                    tmpRTB.SelectionColor = KeywordColor
                End If
            Next
        Next
        Me.Rtf = tmpRTB.Rtf

        ''Put the cursor back where it started from
        Me.SelectionStart = CursorPosition
        ''Set the RichTextBox''s color back to normal
        Me.SelectionColor = Me.ForeColor
        ''Unlock RichTextBox
        LockWindowUpdate(0)
    End Sub



这个想法是,它将为新粘贴的关键字着色,并忽略实时出价工具中已经存在的任何内容.
如果我复制一百行左右的文本,然后将其粘贴到RTB中,它将在合理的时间内处理文本.但是,如果我再次输入相同的文本,则处理时间会更长一些.每次添加其他糊剂,处理时间都会继续增加.
我知道我在传输rtf数据时会浪费一点时间,因为它更大,但是真正的损失是在"For"语句中.如果我扔
"tmpRTB.SelectionColor = KeywordColor"之后的"tmpRTB.Select()"它将选择粘贴的内容...告诉我它仅在粘贴的区域内起作用.

我确定这很简单...我只是没有看到它. :)

任何想法将不胜感激!

谢谢!



The idea is that it will colorize keywords that are newly pasted and ignore anything that was already in the RTB.
If I copy a hundred or so lines of text then paste it into the RTB it processes the text within a reasonable amount of time. However, if I past the same text again it takes a bit longer to process. The process time continues to increase with each additional paste.
I know I lose a tiny bit of time in transferring the rtf data as it is larger, but the real loss is in the "For" statement. If I throw in
"tmpRTB.Select()" right after "tmpRTB.SelectionColor = KeywordColor" It will select what was pasted... telling me that it is working within only the pasted area.

I''m sure it''s something simple... I''m just not seeing it.! :)

Any ideas will be greatly appreciated!

Thanks!

推荐答案

好吧,由于所需时间的增加,我怀疑以下内容.

您正在表单上的RTF组件中处理实际的RTF.因此,每次将其粘贴到其中时,RTF都会变得越来越大且难以处理.这是因为您应用于文本的每种样式都会添加新数据,并且有必要在当前分配的内存用尽的情况下进行复制.因为您已经将其串联为完整的文本,所以这意味着所有先前的文本也必须被复制到新分配的内存中.

一个避免此过程变得越来越慢的简单方法是在实际粘贴到rtf控件之前简单地获取粘贴的文本:
http://www.vbforums.com/archive/index.php/t-374169.html [^ ]

然后是您创建的文本(或使用已经备用的)RichTextBox组件来处理该文本并在那里处理突出显示.完成后,将所有突出显示的rtf粘贴到总rtf中.

您也可以使用此组件向文本添加样式,而不是使用可视组件.但是,由于您是初学者,所以我不知道该如何轻松地使用它,但您始终可以看一下.

NRTFTree-在C#中用于RTF处理的类库 [
Well, because of the increasing amount of time needed I suspect the following.

You are working on the actual RTF in the RTF component on your form. So each time you paste it into it, the RTF is becoming larger and harder to process. This is because each style you apply to the text will add new data and makes it necessary to copy it in case the current allocated memory runs out. Because you already concatenated it to the complete text this means that all of the previous text has to be copied as well to the new allocated memory.

A simple option to avoid this process of getting slower and slower is to simply get the pasted text before it is actually pasted into the rtf control:
http://www.vbforums.com/archive/index.php/t-374169.html[^]

Then is it is text you create (or use an already standby) RichTextBox component to handle that text and process the highlighting in there. When you''re done you paste the rtf with all highlighting into the total rtf.

You could also use this component for adding the styles to the text instead of using a visual component. But since you are a beginner I don''t know how easy you could use it, but you can always have a look.

NRTFTree - A class library for RTF processing in C#[^]

Good luck!


这篇关于如何加快基本语法荧光笔的显示速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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