【问题标题】:Highlight Current Line of RichTextBox突出显示 RichTextBox 的当前行
【发布时间】:2012-07-23 11:29:59
【问题描述】:

这通过在当前行上绘制透明颜色作为背景色来突出显示每行的整个宽度。换行时,恢复原来的背景色。

那么,我们要做的是:

  1. 验证之前和当前的矩形不匹配,所以不要在同一区域绘制两次
  2. 使用控件背景色替换最后一行的突出显示
  3. 使用透明颜色突出显示当前行
  4. 为每条应用线设置mLastHighlight 的索引和矩形

但是,当移除突出显示时,文本会被覆盖。应用高亮时不会发生这种情况。

一种解决方案是在重置背景颜色后重新绘制控件上的文本。尽管文本格式、选择颜色、字体样式、超链接等过滤起来很繁琐。不是很优雅。

这导致了一个更简单的解决方案,即刷新控件。虽然这会导致大量闪烁。也不接受。

有没有优雅的解决方案?我完全不明白为什么会这样。

编辑:已编辑以反映 Code Gray 的回应。

using System;

public class RTBHL : RichTextBox
{
    private LastHighlight mLastHighlight = new LastHighlight(0, Rectangle.Empty);

    private class LastHighlight
    {
        public int mCharIndex;
        public Rectangle mRectangle;

        public LastHighlight(int index, Rectangle r)
        {
            mCharIndex = index;
            mRectangle = r;
        }
    }

    public void PaintLineHighlight()
    {
        using (Graphics g = this.CreateGraphics)
        {
            // highlight color
            Color c = Color.Beige;
            // current pen color
            Pen cp = new Pen(Color.Beige);
            // color for removing highlight
            Pen lp = new Pen(this.BackColor);
            // brush for removing highlight
            SolidBrush lb = new SolidBrush(this.BackColor);
            // brush for applying highlight
            SolidBrush cb = new SolidBrush(Color.FromArgb(64, c.R, c.G, c.B));
            // index of the current line
            int index = this.GetFirstCharIndexOfCurrentLine;
            // rectangle to specify which region to paint too
            Rectangle r = new Rectangle();

            // specify dimensions
            r.X = 0;
            r.Y = this.GetPositionFromCharIndex(index).Y;
            r.Width = this.HorizontalScrollBarWidth;
            r.Height = Convert.ToInt32(this.Font.Height * this.ZoomFactor);

            // this will always be true unless the current line remains the same
            if (!(mLastHighlight.mCharIndex == index) && !(mLastHighlight.mRectangle == r))
            {
                // remove the last highlight. regardless of the brush specified, white is always applied, and the text is painted over
                g.DrawRectangle(lp, mLastHighlight.mRectangle);
                g.FillRectangle(lb, mLastHighlight.mRectangle);
                // apply highlight to the current line
                g.DrawRectangle(cp, r);
                g.FillRectangle(cb, r);
            }

            mLastHighlight = new LastHighlight(index, r);
        }
    }

#region RichScrollBars
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool GetScrollInfo(IntPtr hWnd, int fnBar, ref SCROLLINFO si);

    [StructLayout(LayoutKind.Sequential)]
    public class SCROLLINFO
    {
        public int cbSize;
        public int fMask;
        public int nMin;
        public int nMax;
        public int nPage;
        public int nPos;
        public int nTrackPos;
        public SCROLLINFO()
        {
            this.cbSize = Marshal.SizeOf(typeof(SCROLLINFO));
        }

        public SCROLLINFO(int mask, int min, int max, int page, int pos)
        {
            this.cbSize = Marshal.SizeOf(typeof(SCROLLINFO));
            this.fMask = mask;
            this.nMin = min;
            this.nMax = max;
            this.nPage = page;
            this.nPos = pos;
        }
    }

    private const int SIF_ALL = 0X17;
    private const int SB_HORZ = 0;
    private const int SB_VERT = 1;

    public int HorizontalScrollBarWidth()
    {
        SCROLLINFO si = new SCROLLINFO() {fMask = SIF_ALL};
        GetScrollInfo(this.Handle, SB_HORZ, si);

        return Math.Max(si.nMax, this.Width);
    }

    public int VerticalScrollBarHeight()
    {
        SCROLLINFO si = new SCROLLINFO() {fMask = SIF_ALL};
        GetScrollInfo(this.Handle, SB_VERT, si);

        return Math.Max(si.nMax, this.Height);
    }
#endregion
}

【问题讨论】:

    标签: c# winforms richtextbox highlight


    【解决方案1】:

    这里的问题是您复制的代码是为 Scintilla 设计的。 SCI_* 常量由 Scintilla 标头在内部定义,它们引用的消息仅对 Scintilla 控件有意义。

    将这些消息发送到本机 Win32 Rich Edit 控件不会做任何事情,因为它不是为处理这些消息而设计的。 (或者更糟的是,一个或多个SCI_* 常量碰巧与富编辑控件确实识别的一个或多个消息标识符发生冲突,从而产生一些潜在的有趣行为。)

    除非您在项目中实际使用了 Scintilla 编辑控件(您说过不想这样做),否则该代码不会做任何有趣的事情。它不是为 Win32 富编辑控件编写的,它是为与 Scintilla 控件交互而编写的。

    Scintilla 控件不仅仅是 Win32 富编辑控件的包装器。它必须大量进行自定义绘图才能发挥其魔力,而所有这些代码都很难靠你自己来正确完成。这就是为什么这么多人首先使用 Scintilla 的原因。如果您需要它的功能集,我强烈建议您效仿。

    无论如何,我实际上不知道这是否可以通过 Win32 富编辑控件实现。我不这么认为,但我不能对这个事实发誓。我想你可以通过设置选择颜色来破解它,但这似乎不是一个很好的解决方案。像Daniel suggests here 这样的东西。我不是 Scintilla 专家,但在未经训练的人看来,这看起来有点像基于 Scintilla 的代码的道德等价物,但为 Win32 丰富的编辑控件编写(通过其 .NET WinForms 包装器)。

    【讨论】:

    • 感谢您的回答和澄清。经过一番思考,我想了很多。然而,丹尼尔斯的建议并没有为整条线着色。我已经编辑了我的代码以包含实现这一目标的尝试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-30
    • 2011-07-21
    • 2015-12-12
    • 1970-01-01
    • 1970-01-01
    • 2016-09-25
    相关资源
    最近更新 更多