Using a selection
-
Hi. What MFC control should I use in order to put a text in such a way that some text should have different background color, and most important, there will be several selection in this text, all of them with the same background color ... I have tried with CRichEditView, and I could set a text color for several selection, but not color for background, only for text:
SetWindowText(\_T("Havecreatedonecomboclasstogetthecomboboxhandleandlistboxhandlevalue")); CHARFORMAT cf; cf.cbSize = sizeof(cf); cf.dwMask = CFM\_BOLD | CFM\_COLOR; cf.dwEffects = CFE\_BOLD | ~CFE\_AUTOCOLOR; cf.crTextColor = RGB(0, 255, 0); GetRichEditCtrl().SetSel(2, 5); GetRichEditCtrl().SetSelectionCharFormat(cf); cf.crTextColor = RGB(250, 55, 33); GetRichEditCtrl().SetSel(10, 18); GetRichEditCtrl().SetSelectionCharFormat(cf); cf.crTextColor = RGB(250, 55, 33); GetRichEditCtrl().SetSel(10, 18); GetRichEditCtrl().SetSelectionCharFormat(cf); GetRichEditCtrl().SetSel(-1, -1); GetRichEditCtrl().SetReadOnly();
-
Hi. What MFC control should I use in order to put a text in such a way that some text should have different background color, and most important, there will be several selection in this text, all of them with the same background color ... I have tried with CRichEditView, and I could set a text color for several selection, but not color for background, only for text:
SetWindowText(\_T("Havecreatedonecomboclasstogetthecomboboxhandleandlistboxhandlevalue")); CHARFORMAT cf; cf.cbSize = sizeof(cf); cf.dwMask = CFM\_BOLD | CFM\_COLOR; cf.dwEffects = CFE\_BOLD | ~CFE\_AUTOCOLOR; cf.crTextColor = RGB(0, 255, 0); GetRichEditCtrl().SetSel(2, 5); GetRichEditCtrl().SetSelectionCharFormat(cf); cf.crTextColor = RGB(250, 55, 33); GetRichEditCtrl().SetSel(10, 18); GetRichEditCtrl().SetSelectionCharFormat(cf); cf.crTextColor = RGB(250, 55, 33); GetRichEditCtrl().SetSel(10, 18); GetRichEditCtrl().SetSelectionCharFormat(cf); GetRichEditCtrl().SetSel(-1, -1); GetRichEditCtrl().SetReadOnly();
Did you try using [CHARFORMAT2W | Microsoft Docs](https://docs.microsoft.com/en-us/windows/desktop/api/richedit/ns-richedit-charformat2w)?
-
Did you try using [CHARFORMAT2W | Microsoft Docs](https://docs.microsoft.com/en-us/windows/desktop/api/richedit/ns-richedit-charformat2w)?
Yes, it goes. Here is the code, in case that everyone need it:
CHARFORMAT2 cf; cf.cbSize = sizeof(cf); cf.dwMask = CFM\_BACKCOLOR | CFE\_AUTOCOLOR; cf.dwEffects = CFE\_PROTECTED; cf.crTextColor = RGB(64, 255, 33); cf.crBackColor = RGB(240, 64, 64); GetRichEditCtrl().SetSel(18, 28); GetRichEditCtrl().SetSelectionCharFormat(cf); GetRichEditCtrl().SetSel(-1, -1);