简体   繁体   中英

MFC Rich edit control 2.0 Text color

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM