【问题标题】:Change the TextBox highlight color when a user selects text?用户选择文本时更改文本框突出显示颜色?
【发布时间】:2016-01-07 22:08:03
【问题描述】:

我一直在寻找在用户选择文本时更改文本框突出显示颜色的方法。 Windows 使用蓝色作为默认颜色。例如,在 Microsoft Outlook 上,当您编写邮件并选择(突出显示)文本时,背景颜色为灰色。

每个人都说我需要重写 onPaint 方法,但我不知道该怎么做。 RichTextbox selectedbackground 颜色不是解决方案,因为它会更改文本的颜色,而不是在用户选择文本时。

【问题讨论】:

  • 您需要了解以下内容,例如int length = richTextBox.TextLength richTextBox.SelectionStart = length; richTextBox.SelectionLength = yourstring.Length; richTextBox.SelectionColor = Color.Blue;类似的东西
  • 这会在文本上生成背景颜色,但是当用户选择它时,它仍然是蓝色的。我希望更改用户使用鼠标或 Shift 键选择文本时绘制的矩形颜色。
  • google 搜索一下我在网上看到了几个关于高亮选择文本的例子
  • @MethodMan 我用谷歌搜索,没有找到。能给个链接吗?
  • 我找到了一些相关的文章some examples。还有OnPaint也许有帮助。

标签: c# winforms


【解决方案1】:

作为一种选择,您可以依靠ElementHost Windows 窗体控件来承载 WPF TextBox 控件。然后对于 WPF TextBox 控件,设置 SelectionBrushSelectionOpacity

示例

在以下示例中,我创建了一个 Windows 窗体 UserControl,其中包含一个 ElementHost 来托管 WPF TextBox 控件。然后对于 WPF TextBox 控件,设置 SelectionBrushSelectionOpacity

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; }
    }
}

引用的程序集

这里是必需的引用程序集:PresentationCorePresentationFrameworkWindowsBaseWindowsFormsIntegration

【讨论】:

  • +1 这正是我想到的唯一解决方案。但我也可能是错的。请注意使用此解决方案的任何人,它仅适用于框架> = 4.0。仍在寻找其他可能提供更好的原生 Winforms 解决方案的答案。此外,WPF 中的SelectionBrush 不是太先进(即没有指定前/后颜色。只有不透明度。但仍然是一个很好的解决方案)。虽然感谢您的回答!
  • @XStylish 不客气。据我所知,没有原生支持更改独立于 Windows 主题的原生文本框的选择突出显示颜色。
  • 请注意System.Windows.Forms.TextBox 没有属性SelectionBrush。那只是在System.Windows.Controls.TextBox
  • @HaraldCoppoolse 答案中的textBoxSystem.Windows.Controls.TextBox
  • 这似乎是一个不错的解决方案。更多框架原生且负担得起,也许这将适用于 Mono。
【解决方案2】:

您好,这里是更改选择颜色的代码,请记住,您必须存储当前颜色,然后在更改颜色并且应用程序关闭后,您需要恢复它,因为这会更改颜色整个计算机的,而不仅仅是当前进程。

    [DllImport("user32.dll")]
    static extern bool SetSysColors(int cElements, int[] lpaElements, uint[] lpaRgbValues);


    void ChangeSelectColour(Color color)
    {
        const int COLOR_HIGHLIGHT = 13;
        const int COLOR_HIGHLIGHTTEXT = 14;
        // You will have to set the HighlightText colour if you want to change that as well.


        //array of elements to change
        int[] elements = { COLOR_HIGHLIGHT };


        List<uint> colours = new List<uint>();
        colours.Add((uint)ColorTranslator.ToWin32(color));

        //set the desktop color using p/invoke
        SetSysColors(elements.Length, elements, colours.ToArray());
    }

【讨论】:

  • 这似乎是一种方法,但使用操作系统有点冒险。
  • 没有风险。但它会更改所有正在运行的应用程序的设置,包括 Windows 资源管理器。这肯定不是用户所期望的。可惜微软没有实现和SetSysColors一样的功能,只针对当前应用。
猜你喜欢
  • 1970-01-01
  • 2023-04-04
  • 2019-07-06
  • 1970-01-01
  • 1970-01-01
  • 2010-09-29
  • 2016-03-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多