Click to See Complete Forum and Search --> : RichEdit colors


Notsosuperhero
September 19th, 2005, 01:05 PM
Can someone either explain or link me to a tutorial that explains how to search for a specific word to colorize in a RichEdit control? I've searched for a while but everything that I found was either MFC or another language, I need to know how to do it in Win32 API. I know how to color the text, but I don't know how to color just a specific word.

BTW its a RichEdit 2.0

Your help is greatly appreciated.

kkez
September 19th, 2005, 02:08 PM
1) Select the word you need to color -> EM_SETSEL or EM_EXSETSEL
2) Format the characters (you can make it also bold, italic, set its height, so on) -> EM_SETCHARFORMAT and CHARFORMAT
3) Reset the selection (or caret position) where it was before you did your things.

Ex:
CHARRANGE cr;
cr.cpMin = 10; //word starts at 10th column
cr.cpMax = 15; //and ends at 15th
SendMessage(HANDLE_RICHEDIT, EM_EXSETSEL, 0, (LPARAM)&cr);

CHARFORMAT cf;
cf.cbSize = sizeof(CHARFORMAT);
cf.dwMask = CFM_COLOR; //let's set the color
cf.crTextColor = RGB(0,0,255); //blue color
SendMessage(HANDLE_RICHEDIT, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cr);

Notsosuperhero
September 19th, 2005, 02:27 PM
What if you don't know the exact position of the word?

kkez
September 19th, 2005, 02:45 PM
Do you mean you have to look for it? Then use EM_FINDTEXT & FINDTEXT struct or EM_FINDTEXTEX & FINDTEXTEX struct

Notsosuperhero
September 19th, 2005, 02:56 PM
Thank you very much, yea thats what I meant.