RichTextBox 中所选文本的颜色 [英] Color of selected text in RichTextBox
问题描述
在 CSS 中,我们可以使用 ::selection
伪标签来编辑文本选择的颜色.是否可以只为一个控件覆盖选择的默认背景颜色,例如富文本框?我知道,无法通过 CSS 之类的方式更改默认行为,但至少可以为此控件覆盖它.
In CSS we're able to edit the text selection's colors by using the ::selection
pseudo-tag. Is it possible, to overwrite the default background color of the selection just for one control, e.g. a RichTextBox? I know, that there is no way to change the default behaviour by something like CSS, but at least overwrite it for this control might be possible.
我已经用谷歌搜索了大约一个小时,但我只找到了语法高亮的片段.我希望文本是例如黄色而不是典型的 Windows 蓝色.
I already googled for about an hour now, but I only found snippets of syntax highlighting. I want the text to be e.g. yellow instead the typical Windows blue.
编辑
就像这个小提琴:http://jsfiddle.net/W99Gt/
推荐答案
在 WPF 中,您可以按如下方式完成:
In WPF you can accomplish this as follows:
myRichTextBox.SelectionBrush = System.Windows.Media.Brushes.Yellow; // WPF
myRichTextBox.IsInactiveSelectionHighlightEnabled = true;
不幸的是,在 Windows 窗体中无法实现所需的行为(详细信息 此处).解决方法是通过 ElementHost.
Unfortunately, the desired behavior is not possible in Windows Forms (details here). The workaround would be to use a WPF RichTextBox in the Windows Form through ElementHost.
参考资料:
TextBoxBase.SelectionBrush 属性 (WPF)
TextBoxBase.IsInactiveSelectionHighlightEnabled 属性 (WPF)
删除了 WinForms 解决方案,因为 SelectionBackColor
没有提供所需的行为.
References:
TextBoxBase.SelectionBrush Property (WPF)
TextBoxBase.IsInactiveSelectionHighlightEnabled Property (WPF)
Removed the WinForms solution, because SelectionBackColor
does not provide the desired behavior.
这篇关于RichTextBox 中所选文本的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!