Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change selection color in RichEdit control?

Basically, I want to change the standard RichEdit selection color in places where it is above colored text.

I'm implementing a "highlight" function in a RichEdit control (like a yellow marker). Now when I apply highlight on the selected text, nothing changes visually because selection stays in place and it overrides character color. This is bad, because the user should get some visual feedback that the highlight has been applied.

Now I've settled for a compromise - after executing the "highlight" command, I just clear the selection altogether. But it would be ideal to have selection color different when it is above colored text.

How can I do that? Thanks in advance!

like image 929
Alex Jenter Avatar asked Feb 24 '23 07:02

Alex Jenter


1 Answers

The color setting for text selection is hardwired to the system selection color in RichEdit 2.0 and higher. In RichEdit 1.0, the selection color is generated by inversing the background color, so you sort of have some control, but you will lose a lot of features if you downgrade to 1.0.

A walkaround is to installing a process-wide Detour hook on the GetSysColor API.

There is a SelectionBrush Property in the WPF4 version of RichTextBox that can be used to change the selection appearence. Not sure if your project requirement allows you to host a WPF control on your dialog.

like image 168
Sheng Jiang 蒋晟 Avatar answered Feb 26 '23 20:02

Sheng Jiang 蒋晟