2

The idea here is pretty simple I thought, but i can't seem to figure it out. Essentially what i have is a MFC single view application with just one rich edit control 2.0 and some text in it. Let just say this control says "Please click here to fast forward."

CHARFORMAT cf = { sizeof(cf) };
cf.dwEffects = CFM_BOLD;
cf.dwMask = CFM_BOLD;
m_pMessageTextBox.SetSel(13, 17);
m_pMessageTextBox.SetSelectionCharFormat(cf);

Now this snippet of code i have just bolds the word "here" which is what i want. But in addition to bolding it, i want to change the color of the entire text to red.

I only need to see how you would turn text in a rich edit control 2.0 to a different color. I have browsed stackoverflow and google and a lot of the documents regarding this is from 10 years ago+.

Here is a recent question asked that asks the same question i do except his post is a gigantic snippet of code that i really don't care for. I've already tried using

cf.crTextColor = RGB(255,0,0); 

before using

m_pMessageTextBox.SetSelectionCharFormat(cf);

nothing changes, i thought in this case the bold word would be bold AND red at the same time but it just stays black.

Community
  • 1
  • 1
Steven V
  • 65
  • 5

1 Answers1

3

Okay so I managed to figure it out, one of my conditional statement's was broken so part of my code never actually got read. For anyone who runs into this issue this is what i used:

CHARFORMAT cf = { sizeof(cf) };
cf.dwEffects = CFM_BOLD;
cf.dwMask = CFM_BOLD;
m_pMessageTextBox.SetSel(13, 17);
m_pMessageTextBox.SetSelectionCharFormat(cf);
cf.cbSize = sizeof(cf);
cf.dwMask = CFM_COLOR;
cf.dwEffects = 0;
m_pMessageTextBox.SetSel(0, -1);
m_pMessageTextBox.SetSelectionCharFormat(cf); 

For this, and i haven't had a chance to play around with it just yet to see if i can shorten it but it's working the way i want it now. First it does the bold properties and applies it to the text, THEN it does the text color properties and applies it from start to the end of the text.

Steven V
  • 65
  • 5
  • Might make more sense to swap the operations - color the entire text first, then bold the individual section. BTW, what is the point of using "click here" text if you don't actually make it clickable? Use the `CFM_LINK` mask to allow the `CFE_LINK` effect to be enabled, then the clickable text will generate an `EN_LINK` notification when clicked. – Remy Lebeau Jun 08 '16 at 18:54
  • Your answer does not make any sense if it does not have the `cf.crTextColor = RGB(255,0,0); ` line! – sergiol Sep 30 '16 at 19:19