RichTextBox 中的实时语法突出显示

发布于 2024-12-04 20:57:34 字数 376 浏览 5 评论 0原文

我目前正在开发一个非常简单的应用程序,在 RichTextBox 中具有非常基本的语法突出显示。

它工作得几乎没问题。我正在做的是:

  • 如果一个或多个匹配,则查找预定义正则表达式的列表。
  • 选择匹配项,将正确的样式应用于所选内容。
  • 然后将光标放回到原来的位置。

每个 KeyUp 事件都会触发此方法。它会产生很多闪烁。

所以我的问题是:如何巧妙地突出显示我输入的文本而不出现任何闪烁?这个编辑器永远不会包含数千行文本,最多可能大约一百行,所以我还不需要任何非常优化的解决方案。

我尝试了其他帖子中提出的一些解决方案,但没有任何有趣的效果。我不想使用另一个库中的另一个组件 - 我想出于学习目的自己做。

I'm currently developing a very simple app with very basic syntax highlighting in a RichTextBox.

It works almost fine. What I'm doing is:

  • Lookup a list of predefined regex if one or more matches.
  • Select the match, applying the right style to the selection.
  • Then replace the cursor where it initially was.

This method gets triggered on every KeyUp event. And it makes a LOT of flickering.

So my question is: how could I subtly highlight the text that I type without any flickering? This editor will never contain thousands lines of text, maybe about a hundred max so I don't need any very optimized solution yet.

I tried some of the solutions proposed on other posts, but nothing interesting worked. And I don't want to use another component from another library -- I wanted to do it myself for learning purposes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鸵鸟症 2024-12-11 20:57:34

我这样做的方法是等到打字停止,然后突出显示一次。
这是基本逻辑:

  • 对于每个 keyup(或 testchange 等),设置“最后更改”时间,然后

  • 在后台线程的 WaitCallback 中,等待750ms 通过System.Threading.Thread.Sleep()。睡眠结束后,检查“最后一次更改”时间。是否小于 750 毫秒前?如果是这样,用户仍在输入,所以,...无事可做。只需退出该方法即可。

  • 如果最后一次更改时间超过 750 毫秒前,则用户已停止输入。也就是说,过去750ms的实时时间内没有发生任何变化。所以,突出显示。请记住,您需要在 UI 线程上进行 UI 更新。这意味着在应用格式之前,请在 WinForms 应用程序中检查 this.InvokeRequired ,或在 WPF 中检查 this.Dispatcher.CheckAccess()

The way I did it was to wait til typing stopped, and then do the highlighting once.
This was the basic logic:

  • with each keyup (or testchange, etc), set the "last change" time, and, queue a background task (QueueUserWorkItem)

  • In the WaitCallback for the background thread, wait 750ms via System.Threading.Thread.Sleep(). When the sleep finishes, check the "last change" time. Is it less than 750ms ago? If so, the user is still typing, so,... nothing to do. Just exit the method.

  • if the last change time is more than 750ms ago, then the user has stopped typing. In other words, no changes have occurred in the past 750ms of real time. So, do the highlighting. Remember that you need to do UI updates on the UI thread. That means checking this.InvokeRequired in a WinForms app, or this.Dispatcher.CheckAccess() in WPF, before applying formatting.

笑红尘 2024-12-11 20:57:34

我决定尝试一些东西,效果惊人!

我一次突出显示一行。因此,当 keyUp 被触发时,我只解析所选行。所以没有闪烁!

在启动时,我创建了一个HighlightLines() 方法,该方法循环遍历各行并调用我的HighlightLine(lineIndex) 方法。

我会尝试将我的解决方案与 Cheeso 的解决方案混合,我想它会做出很棒的东西!

谢谢

I decided to try something and it worked amazingly!

I'm highlighting one line at a time. So when the keyUp gets triggered I only parse the selected line. So NO flickering!

And at the startup I made a HighlightLines() method that loop through the lines and call my HighlightLine(lineIndex) method.

I'll try to mix my solution with Cheeso's and I guess it'll make something awesome!

Thanks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文