How to change highlight color in RichEdit control?

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

I implement a “highlight” function in a RichEdit control (for example, a yellow marker). Now, when I apply the selection of the selected text, nothing changes visually, because the choice remains in place and it redefines the color of the characters. This is bad because the user needs to get some visual feedback that has been applied.

Now I decided to compromise - after executing the “highlight” command, I just completely canceled the choice. But it would be ideal if the highlight color were different if it was above the colored text.

How can i do this? Thanks in advance!

+4
source share
5 answers

The color setting for text selection is tightly tied to the system selection color in RichEdit 2.0 and higher . In RichEdit 1.0, the highlight color is generated by the inverse of the background color , so you have some control, but you will lose a lot of functions if you lower it to 1.0.

The workaround is to set up the Merge hook workaround in the GetSysColor API.

The version of RichTextBox WPF4 has a SelectionBrush property that can be used to change the visibility of a selection. Not sure if your project requirement allows you to place a WPF control in your dialog box.

+4
source

I do not think that this can be done automatically, because the rich editing editor from Windows does not provide such functionality. You have 2 options:

  • Ignore the problem. What you want is impossible, so do not wish it.
  • Make your own rich editing control (just a joke, but it can be done). Or use some others. Perhaps scintilla has this feature?
+2
source

A quick test application, and I was able to change these colors, however it requires special attention to make it work the way you expect.

First, you may have to modify your dialog resource to identify the control as the old RICHEDIT. In visual studio 2010, the .rc file generated in my dialog box had a control specified as "RichEditCtrl20A , and when the control was identified as such, I could not make significant changes to the highlight color. Changing the control type to "RICHEDIT" allowed me to make changes to the color of the selection without noticing a loss of functionality.You need to change the .rc file in a text editor, find an instance of the RichEdit control and make changes.

After that, you can change the color of the selection (in fact, significantly more selection attributes) using the CRichEditCtrl::SetSelectionCharFormat .

The simple test I did was to create a new Dialog-based MFC application, add a rich editing control to it, modify the RC file as described above, and add the following OnOK () handler:

 void CTestMFCDlg::OnBnClickedOk() { m_rec.SetWindowText("This is a test of stuff"); m_rec.SetFocus(); CHARRANGE cr; cr.cpMin = 0; cr.cpMax = 16; m_rec.SetSel(cr); CHARFORMAT2A cf; m_rec.GetSelectionCharFormat(cf); cf.dwEffects = 0; cf.dwMask = CFM_BACKCOLOR | CFM_COLOR | CFM_FACE; cf.crBackColor = 0; cf.crTextColor = RGB(15, 15, 255); strcpy(cf.szFaceName, "Times New Roman"); m_rec.SetSelectionCharFormat(cf); } 

Once this handler was in place, clicking the "OK" button in the dialog box would fill in the advanced editing control, select the first 17 characters and change the highlight color.

You can find the documentation for this feature on MSDN:

+1
source

Now I decided to compromise - after executing the "highlight" command, I just cleared the selection altogether

I just wanted to add that even Microsoft Word deselects text when highlighting a specific color / color. From the game it seems the most intuitive, because usually you don’t want to do anything, and you just cancel the text right away.

+1
source

Although I could not correctly understand your question, I assume that you want to change the color of the selected text. The following links are in MFC, so you need to change it to the Windows SDK code, but it will help you get started.

http://social.msdn.microsoft.com/Forums/en-US/vcmfcatl/thread/860b0295-9144-4af6-9ffc-42c2b39a3f50/

http://www.go4expert.com/forums/showthread.php?t=320

Please let me know if my answer helped you.

EDIT

Change color in Rich Edit Control

0
source

All Articles