Change the highlight color of a TextBox when the user selects text?

I was looking for a way to change the highlight color of a text field when a user selects text. Windows uses blue by default. For example, in Microsoft Outlook, when you write mail and select (select) text, the background color is gray.

Selected text in Outlook

TextBox selected text by user

Everyone said that I need to override the onPaint method, but I don’t know how to do this. The reverse color selected by RichTextbox is not a solution because it changes the color of the text, not when the user selects it.

+12
source share
2 answers

Alternatively, you can rely on the ElementHost Windows Forms control to host the WPF TextBox TextBox control. Then, for the WPF TextBox control TextBox set SelectionBrush and SelectionOpacity .

example

In the following example, I created a Windows Forms UserControl containing an ElementHost to host the WPF TextBox control. Then, for the WPF TextBox control TextBox set SelectionBrush and 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; } } } 

Reference Assemblies

Mandatory assemblies are required here: PresentationCore , PresentationFramework , WindowsBase , WindowsFormsIntegration .

+1
source

Hi, here is the code for changing the highlight color, just keep in mind that you will need to save the current color, and then as soon as you change the color and your application closes, you will need to restore it, because it does not change the color of the whole computer only for the current process.

  [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()); } 
0
source

All Articles