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: