4

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!

Alex Jenter
  • 4,324
  • 4
  • 36
  • 61
  • I'm afraid code won't help here, I want to know if this is possible to do it at all, and how. – Alex Jenter May 19 '11 at 06:59
  • loops like similar to the question Change highlight color of selected text in RichEdit http://stackoverflow.com/questions/1139234/change-highlight-color-of-selected-text-in-richedit – Sheng Jiang 蒋晟 May 21 '11 at 00:22

5 Answers5

4

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.

Sheng Jiang 蒋晟
  • 15,125
  • 2
  • 28
  • 46
2

I don't think this can be done automatically, because rich edit control from Windows does not provide such functionality. You have 2 options:

  • Ignore the problem. What you want is impossible, so do not wish for it.
  • Make your own rich edit control (just a joke, but it can be done). Or use some other. Maybe scintilla control has this functionality?
Dialecticus
  • 16,400
  • 7
  • 43
  • 103
  • Having created my own color-coded rich text edit box a while back, I can vouch that it is doable... but may not really be worth the effort. Now, if you were using C#... :) – Gustavo Mori May 17 '11 at 00:56
  • Could it be possible to do that via sending EM_HIDESELECTION and emulating selection manually with character formatting? – Alex Jenter May 19 '11 at 06:49
  • 1
    AFAIK EM_HIDESELECTION will hide selection only if control is out of focus. Which means that you must hack things around if you want to follow this path. End result might collide with existing user's experience of rich edit controls. – Dialecticus May 19 '11 at 09:34
1

A quick test application and I was able to modify these colors, however it takes some careful consideration to make it work as you expect.

First off, you likely have to change your dialog resource to identify the control as the older RICHEDIT. In visual studio 2010, the .rc file generated on my dialog had the control identified as a "RichEditCtrl20A, and when the control was identified as such, I couldn't make any significant changes to the selection color. Changing the control type to "RICHEDIT" allowed me to make the changes to selection color, with no perceived loss of functionality. You have to modify the .rc file in a text editor, find your RichEdit control instance and make the modification.

Once that is done, you can modify the selection color (actually, significantly more selection attributes) with the CRichEditCtrl::SetSelectionCharFormat function.

The simple test that I did was create a new Dialog Based MFC application, add a rich edit 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, pressing OK in the dialog would populate the rich edit control, select the first 17 characters and change the selection highlight color.

You can find the documentation on this function on MSDN:

Chad
  • 18,706
  • 4
  • 46
  • 63
1

Now I've settled for a compromise - after executing the "highlight" command, I just clear the selection altogether

I just wanted to add that even Microsoft Word deselects the text when you highlight a certain colour/color. From playing around, this feels the most intuitive because normally you don't want to do anything else anyway, and you'll just unselect the text straight away.

LukeFitz
  • 156
  • 3
0

Though I could not understand your question properly, I am guessing you want to change the colour of the selected text. The following links are in MFC, so you need to change it to Windows SDK code but it shall 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

Color change in Rich Edit Control

Community
  • 1
  • 1
Sujay Ghosh
  • 2,828
  • 8
  • 30
  • 47
  • No, this is not really what I want. Sorry for being unclear. I want to change the standard selection highlight color in places where it is above colored text. – Alex Jenter May 19 '11 at 06:47
  • Alex , are you saying that you want to change the blue colour ( the colour when text is selected ) to a colour of your choice.I have edited my answer with a link , maybe you can go through that. – Sujay Ghosh May 19 '11 at 11:32