【问题标题】:C# how to stop RichTextBox redraw? [duplicate]C#如何停止RichTextBox重绘? [复制]
【发布时间】:2021-05-21 19:47:14
【问题描述】:

我正在做一个基于 RichTextBox 的文本编辑器。它必须处理复杂的格式(BIU、彩色文本等)。问题是所有格式化工具都是基于选择的,例如我必须选择一段文本,格式化它,选择下一个,等等。

这需要时间,并且对用户可见。

有没有办法关闭 RichTextBox 重绘,然后进行格式化,然后打开重绘?

或者是否有其他快速处理复杂格式的方法?

【问题讨论】:

标签: c# winforms richtextbox


【解决方案1】:

找到了决定,它正在发挥作用。

使用 code 编写了一个包装类

类本身:

  public class RichTextBoxRedrawHandler
{
    RichTextBox rtb;

    public RichTextBoxRedrawHandler (RichTextBox _rtb)
        {
        rtb = _rtb;
        }
    [DllImport("user32.dll")]
    private static extern IntPtr SendMessage(IntPtr hWnd, int wMsg, int wParam, ref Point lParam);

    [DllImport("user32.dll")]
    private static extern IntPtr SendMessage(IntPtr hWnd, int wMsg, int wParam, IntPtr lParam);

    const int WM_USER = 1024;
    const int WM_SETREDRAW = 11;
    const int EM_GETEVENTMASK = WM_USER + 59;
    const int EM_SETEVENTMASK = WM_USER + 69;
    const int EM_GETSCROLLPOS = WM_USER + 221;
    const int EM_SETSCROLLPOS = WM_USER + 222;

    private Point _ScrollPoint;
    private bool _Painting = true;
    private IntPtr _EventMask;
    private int _SuspendIndex = 0;
    private int _SuspendLength = 0;

    public void SuspendPainting()
    {
        if (_Painting)
        {
            _SuspendIndex = rtb.SelectionStart;
            _SuspendLength = rtb.SelectionLength;
            SendMessage(rtb.Handle, EM_GETSCROLLPOS, 0,  ref _ScrollPoint);
            SendMessage(rtb.Handle, WM_SETREDRAW, 0, IntPtr.Zero);
            _EventMask = SendMessage(rtb.Handle, EM_GETEVENTMASK, 0, IntPtr.Zero);
            _Painting = false;
        }
    }

    public void ResumePainting()
    {
        if (!_Painting)
        {
            rtb.Select(_SuspendIndex, _SuspendLength);
            SendMessage(rtb.Handle, EM_SETSCROLLPOS, 0, ref _ScrollPoint);
            SendMessage(rtb.Handle, EM_SETEVENTMASK, 0, _EventMask);
            SendMessage(rtb.Handle, WM_SETREDRAW, 1, IntPtr.Zero);
            _Painting = true;
            rtb.Invalidate();
        }

    }
}

用法:

RichTextBoxRedrawHandler rh = new RichTextBoxRedrawHandler(richTextBoxActually);
rh.SuspendPainting();
// do things with richTextBox
rh.ResumePainting();

【讨论】:

    猜你喜欢
    • 2013-09-13
    • 1970-01-01
    • 2013-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2013-09-24
    相关资源
    最近更新 更多