gpt4 book ai didi

c# - RichTextBox BeginUpdate() EndUpdate() 扩展方法不起作用

转载 作者:太空狗 更新时间:2023-10-29 21:24:00 25 4
gpt4 key购买 nike

我有一个 richTextBox,我用它来执行一些语法突出显示。这是一个小型编辑工具,所以我没有编写自定义语法荧光笔 - 相反,我使用 Regex 并使用 Application.Idle 的事件处理程序检测到输入延迟时进行更新 事件:

Application.Idle += new EventHandler(Application_Idle);

在事件处理程序中,我检查文本框处于非事件状态的时间:

private void Application_Idle(object sender, EventArgs e)
{
// Get time since last syntax update.
double timeRtb1 = DateTime.Now.Subtract(_lastChangeRtb1).TotalMilliseconds;

// If required highlight syntax.
if (timeRtb1 > MINIMUM_UPDATE_DELAY)
{
HighlightSyntax(ref richTextBox1);
_lastChangeRtb1 = DateTime.MaxValue;
}
}

但即使对于相对较小的亮点,RichTextBox 也会严重闪烁,并且它没有 richTextBox.BeginUpdate()/EndUpdate() 方法。为了克服这个问题,我找到了 this answer to a similar dilemma by Hans Passant (Hans Passant 从未让我失望!):

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);
}

[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
private const int WM_SETREDRAW = 0x0b;
}

但是,这在更新时给我带来了奇怪的行为;光标消失/卡住,只显示奇怪的条纹(见下图)。

Odd Error Caused by RichTextBox Method Extension

我显然不能使用替代线程来更新 UI,所以我在这里做错了什么?

感谢您的宝贵时间。

最佳答案

尝试修改 EndUpdate 以在之后调用 Invalidate。控件不知道它需要做一些更新,所以你需要告诉它:

public void EndUpdate() 
{
SendMessage(this.Handle, WM_SETREDRAW, (IntPtr)1, IntPtr.Zero);
this.Invalidate();
}

关于c# - RichTextBox BeginUpdate() EndUpdate() 扩展方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9418024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com