RichTextBox的语法实时凸显 - 禁用重绘 [英] RichTextBox syntax highlighting in real time--Disabling the repaint

查看:561
本文介绍了RichTextBox的语法实时凸显 - 禁用重绘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个函数,它接受一个RichTextBox并先后获得关键字和放大器的列表; '脏话'。需要特别提出的任何关键字和放大器; BADWORDS我发现在RichTextBox中的当用户键入,这意味着该函数被调用每一个编辑按键被释放的时间。

I'm creating a function that takes a RichTextBox and has access to a list of keywords & 'badwords'. I need to highlight any keywords & badwords I find in the RichTextBox while the user is typing, which means the function is called every time an editing key is released.

我已经写了这个功能,但在框中闪烁的文字和光标太多的安慰。

I've written this function, but the words and cursor in the box flicker too much for comfort.

我发现了一个解决方案 - 禁止在RichTextBox的重绘自己,而我编辑和格式化其文本的能力。但是,我知道做到这一点的唯一方法是重写的WndProc功能和拦截(我一直对收集的)重绘信息如下:

I've discovered a solution--to disable the RichTextBox's ability to repaint itself while I'm editing and formatting its text. However, the only way I know to do this is to override the "WndProc" function and intercept (what I've been about to gather is) the repaint message as follows:

protected override void WndProc(ref System.Windows.Forms.Message m)
{
    if (m.Msg == 0x00f) {
         if (paint)
            base.WndProc(ref m);
         else
            m.Result = IntPtr.Zero;
    }
    else
         base.WndProc(ref m);
}

当布尔'油漆'设置为false,就在我开始凸显,并为真当我完成。但正如我所说,我做必须在一个RichTextBox的功能;我不能用一个子类。

Where the boolean 'paint' is set to false just before I start highlighting and to true when I finish. But as I said, the function I make must take in a RichTextBox; I cannot use a subclass.

那么,有没有一种方法来禁用一个RichTextBox的自动重画从外面?

So, is there a way to disable the automatic repainting of a RichTextBox 'from the outside?'

推荐答案

这是RichTextBox类的监督。其他控件,如列表框,支持的BeginUpdate和EndUpdate方法燮preSS画。这些方法生成WM_SETREDRAW消息。 RTB实际上支持这个消息,但他们忘了补充的方法。

It is an oversight in the RichTextBox class. Other controls, like ListBox, support the BeginUpdate and EndUpdate methods to suppress painting. Those methods generate the WM_SETREDRAW message. RTB in fact supports this message, but they forgot to add the methods.

就自行添加。项目+添加类,粘贴如下所示的code。编译并从工具箱顶部的控制拖放到窗体中。

Just add them yourself. Project + Add Class, paste the code shown below. Compile and drop the control from the top of the toolbox onto your form.

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

class MyRichTextBox : RichTextBox {
    public void BeginUpdate() {
        SendMessage(this.Handle, WM_SETREDRAW, (IntPtr)0, IntPtr.Zero);
    }
    public void EndUpdate() {
        SendMessage(this.Handle, WM_SETREDRAW, (IntPtr)1, IntPtr.Zero); 
        this.Invalidate();
    }
    [DllImport("user32.dll")]
    private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
    private const int WM_SETREDRAW = 0x0b;
}

或P / Invoke的SendMessage函数直接之前/更新之后的文字。

Or P/Invoke SendMessage directly before/after you update the text.

这篇关于RichTextBox的语法实时凸显 - 禁用重绘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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