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

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

问题描述

我正在创建一个函数,它接受一个 RichTextBox 并可以访问关键字列表 &'脏话'.我需要突出显示任何关键字 &我在 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);
}

在我开始突出显示之前将布尔paint"设置为 false 并在我完成时设置为 true.但是正如我所说,我制作的函数必须包含在 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 类中的一个疏忽.其他控件,如 ListBox,支持 BeginUpdate 和 EndUpdate 方法来抑制绘制.这些方法生成 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.

自己添加即可.Project + Add Class,粘贴如下所示的代码.编译并将控件从工具箱顶部拖放到您的表单上.

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天全站免登陆