- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
这通过在当前行上绘制透明颜色作为背景色来突出显示每行的整个宽度。线路切换时,恢复原来的背景颜色。
那么,我们要做的是:
mLastHighlight
的索引和矩形但是,当移除高亮时,文本会被覆盖。应用高亮时不会发生这种情况。
一种解决方案是在重置背景颜色后重新绘制控件上的文本。尽管文本格式、选择颜色、字体样式、超链接等过滤起来很乏味。不是很优雅。
这导致更简单的解决方案,即刷新控件。尽管那会导致大量闪烁。也不接受。
有优雅的解决方案吗?我完全不明白为什么会这样。
编辑:编辑以反射(reflect) 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
}
最佳答案
这里的问题是您复制的代码是为 Scintilla 设计的。 SCI_*
常量由 Scintilla header 在内部定义,它们引用的消息仅对 Scintilla 控件有意义。
将这些消息发送到 native Win32 Rich Edit 控件不会执行任何操作,因为它不是为处理这些消息而设计的。 (或者更糟的是,一个或多个 SCI_*
常量碰巧与富编辑控件确实识别的一个或多个消息标识符发生冲突,从而产生一些可能有趣的行为.)
除非您实际上在您的项目中使用 Scintilla 编辑控件(您说过您不想这样做),否则该代码不会做任何有趣的事情。它不是为 Win32 rich edit 控件编写的,它是为与 Scintilla 控件交互而编写的。
Scintilla 控件不仅仅是 Win32 rich edit 控件的包装器。它必须执行很多自定义绘图才能发挥其魔力,而所有这些代码都很难靠您自己正确处理。这就是为什么这么多人首先使用 Scintilla 的原因。如果您需要它的功能集,我强烈建议您效仿。
无论如何,我实际上并不知道 Win32 rich edit 控件是否可以做到这一点。我不认为是,但我不能对这个事实发誓。我猜你可以通过设置选择颜色来破解它,但这似乎不是一个很好的解决方案。类似于 Daniel suggests here .我不是 Scintilla 专家,但在我未经训练的眼中,这看起来有点像基于 Scintilla 的代码的道德等价物,但是是为 Win32 rich edit 控件编写的(通过其 .NET WinForms 包装器)。
关于c# - 突出显示 RichTextBox 的当前行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11642861/
我对 RichTextBox 控件感到困惑。 我的 WinForm 有一个 RichTextBox 控件,宽度为 100px。我想动态创建一个然后引用实例。 因此,假设在我的 Form 上我有一个名为
MouseDown 事件处理程序的 KeyDown 事件处理程序中 e.Handled = true 的等价物是什么?我不希望鼠标事件对 RichTextBox 有任何影响(完全禁用鼠标与 RichT
我有一个 richtextbox,它的文本是特定表格中一些单词的串联。 (表格列是'word','translate'和'id') 我需要当用户将鼠标悬停在每个单词上时,相关的翻译会显示在单词的工具提
您好,我在将 richtextbox 中的数据显示或传输到其他 richtextbox 时遇到问题... richtextbox1.Document = richtextbox2.Document;
我正尝试在我的 Windows Phone 8 应用程序中使用 RichTextBox。用户需要能够选择文本并将其复制到剪贴板。用户不需要编辑文本,只需选择它。但是,我似乎无法在 Windows Ph
我有一个 Winforms 项目,我可以在其中将文本写入 RichTextBox,还有一些控件可以设置所写文本的字体格式。我能够将文本保存并附加到 RTF 文件,但我在保留每个 RichTextBox
假设我有以下内容:
我需要一些具有富文本编辑功能的基本 CMS 功能。在 stack overflow 上有一个常规的文本区域编辑控件,支持 Markdown 样式语法格式。那将超出我的用户范围,所以我想要一个类似丰富的
显示图像: 复制代码代码如下: Image img = Image.FromFi
我遇到过 RichTextBox 的一些奇怪行为。我希望它是readonly,但是当我使用时它不显示图像richTextBox.LoadFile(path) 方法加载.rtf 文件。当它不是reado
我需要将 ScrollBars 设置为 ForcedBoth 但我还想知道 ScrollBars 的句柄何时可见并且用户可以滚动。 两个滚动条的 bool 值都可以 这里用户不能滚动: 在这里他们可以
我正在开发根据正则表达式模式在 RichTextBox 中突出显示文本的应用程序。 它工作正常,除了性能,即使对于小文本(大约 500 个字符),它也会挂起一段时间,这对用户是可见的。 我在 Flow
我在我的 WPF richtextbox 上启用了拼写,我想在显示带有拼写建议的上下文菜单之前在当前插入符号位置获取拼写错误的单词。 最佳答案 新方式 void richTextBox1_Pr
我在 .NET Windows 窗体应用程序中使用 RichTextBox 控件。我允许用户在文本框本身内按 TAB 键。但是,当我将 .Text 值保存在文本框中时,它将显示如下: "This[]i
我在 MS Word 中创建了带有超链接的 rtf 文件并将其加载到 RichTextBox 中。 RichTextBox 中的超链接不起作用。 RichTextBox 和 MS Word 使用不同的
有没有办法改变 RichTextBox 中下划线的颜色? ? 我试过 this但它似乎不起作用。有人知道这些枚举值是从哪里来的吗?没有看到它的任何文档。 谢谢。 最佳答案 枚举值是将消息传递给控件时的
这是交易:我有一个 RichTextBox 控件,它工作正常。问题是有一个“插入当前日期时间”按钮,它将当前日期时间添加/注入(inject)到 RichTextBox 中。用户可以在插入符号指向的任
我正在编写一个小应用程序,我可以在其中加载文本文件以匹配正则表达式: 我根据匹配行的长度更改前景色文本, 问题是,如果一行是多行,那么它不会按预期工作并且只选择该行的一半,例如以“title”开头的第
我有一个 RichTextBox在我的应用程序中,它正在获取有关某些事件的新内容。 添加新内容时,我想滚动到底部,仅当 卷轴在底部 之前 . 我该怎么做呢? 更具体地说,给我带来麻烦的部分是确定滚动位
当加载 50KB 的文本文档时,WPF 的 RichTextBox 控件的性能确实很慢。滚动滞后并按 Ctrl-A 选择所有文本需要 10 多秒钟。 (这在记事本上是瞬时的)。 我没有做任何花哨的位图
我是一名优秀的程序员,十分优秀!