C# 突出显示RichTextBox的当前行
这将通过在当前行上绘制透明颜色作为背景色来高亮显示每行的整个宽度。当线路切换时,将恢复原始背景色 因此,我们要做的是:C# 突出显示RichTextBox的当前行,c#,winforms,richtextbox,highlight,C#,Winforms,Richtextbox,Highlight,这将通过在当前行上绘制透明颜色作为背景色来高亮显示每行的整个宽度。当线路切换时,将恢复原始背景色 因此,我们要做的是: 验证上一个矩形和当前矩形不匹配,因此不要在同一区域绘制两次 使用控件背景色替换最后一行的高光 使用透明颜色高亮显示当前行 设置mlashighlight,为每个应用的行设置索引和矩形 但是,删除高光时,文本将被覆盖。应用高光时不会发生这种情况 一种解决方案是在重置背景颜色后将文本重新绘制回控件上。尽管文本格式、选择颜色、字体样式、超链接等过滤起来很繁琐。不太优雅 这将导致更简单
mlashighlight
,为每个应用的行设置索引和矩形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
}
这里的问题是,您复制的代码是为闪烁而设计的。
SCI.*
常数由闪烁体标头在内部定义,它们所指的消息仅对闪烁体控件有意义
将这些消息发送到本机Win32 rich edit控件不会起任何作用,因为它不是为处理这些消息而设计的。(更糟糕的是,一个或多个SCI.*
常量与rich edit控件识别的一个或多个消息标识符发生冲突,从而产生一些潜在的有趣行为。)
除非您在项目中实际使用了闪烁编辑控件(您说过不想这样做),否则该代码不会做任何有趣的事情。它不是为Win32富编辑控件编写的,而是为与闪烁控件的接口编写的
闪烁控件不仅仅是Win32 rich edit控件的包装器。它需要做大量的自定义绘图来创造它的魔力,而所有这些代码都很难靠你自己来完成。这就是为什么这么多人首先使用斯金特拉。如果您需要它的功能集,我强烈建议您也这样做
无论如何,我不知道用Win32富编辑控件是否可以实现这一点。我不认为是这样,但我不能对这个事实发誓。我想你可以通过设置选择颜色来解决这个问题,但这似乎不是一个很好的解决方案。差不多。我不是闪烁体专家,但在我未经训练的眼中,这看起来有点像基于闪烁体的代码的道德等价物,但是为Win32富编辑控件(通过.NET WinForms包装)编写的。感谢您的回答并澄清了这一点。经过深思熟虑,我也想到了这一点。然而,丹尼尔斯的建议并没有给整条线涂上颜色。我已经编辑了我的代码,试图实现这一点。