- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我一直在寻找在用户选择文本时更改文本框突出显示颜色的方法。 Windows 使用蓝色作为默认颜色。例如,在 Microsoft Outlook 上,当您编写邮件并选择(突出显示)文本时,背景颜色为灰色。
每个人都说我需要覆盖 onPaint 方法,但我不知道该怎么做。 RichTextbox selectedbackground 颜色不是解决方案,因为它会更改文本的颜色,而不是在用户选择它时。
最佳答案
作为一种选择,您可以依赖 ElementHost
承载 WPF TextBox
控件的 Windows 窗体控件。然后为 WPF TextBox
控件设置 SelectionBrush
和 SelectionOpacity
。
示例
在下面的示例中,我创建了一个 Windows 窗体 UserControl
,其中包含一个 ElementHost
以托管 WPF TextBox
控件。然后为 WPF TextBox
控件设置 SelectionBrush
和 SelectionOpacity
。
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms.Integration;
using System.Windows.Media;
public class MyWPFTextBox : System.Windows.Forms.UserControl
{
private ElementHost elementHost = new ElementHost();
private TextBox textBox = new TextBox();
public MyWPFTextBox()
{
textBox.SelectionBrush = new SolidColorBrush(Colors.Gray);
textBox.SelectionOpacity = 0.5;
textBox.TextAlignment = TextAlignment.Left;
textBox.VerticalContentAlignment = VerticalAlignment.Center;
elementHost.Dock = System.Windows.Forms.DockStyle.Fill;
elementHost.Name = "elementHost";
elementHost.Child = textBox;
textBox.TextChanged += (s, e) => OnTextChanged(EventArgs.Empty);
Controls.Add(elementHost);
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override string Text
{
get { return textBox.Text; }
set { textBox.Text = value; }
}
}
引用程序集
这里是需要引用的程序集:PresentationCore
、PresentationFramework
、WindowsBase
、WindowsFormsIntegration
。
关于c# - 当用户选择文本时更改 TextBox 突出显示颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34666064/
有没有一种方法可以“标记”对象的属性,使它们在反射中“突出”? 例如: class A { int aa, b; string s1, s2; public int AA
我是一名优秀的程序员,十分优秀!