Click to See Complete Forum and Search --> : Syntax highlighting in normal textedit box?


Ali Imran
June 4th, 2005, 12:16 PM
Hi all.

First of all, I want to know

Can we highlight a word e.g. 'void' in a normal EDIT textbox ?
highlighting means giving it blue or green color.

My first preference is to use normal textbox, and I am 80% it can be done, but don't know how.

If not then, ofcourse we will be using richedit.
Please tell me simples code through which I can highlight word 'void' in a richedit. But if the word is within quotes e.g. " " or ' ', must not be highlighted.

wiating for the kind reply from gurus.

regards
Ali Imran

NoHero
June 5th, 2005, 06:12 AM
CodeGuru provides several article on this how to do syntax highlighting on a rich edit box.:


RichEditCtrl Syntax Highlighting (http://www.codeguru.com/Cpp/controls/richedit/article.php/c2419/)
Syntax coloring Editor (http://www.codeguru.com/Cpp/controls/richedit/syntaxhilighting/article.php/c2387/)
CSyntaxColorizer: Syntax Highlighting Class (http://www.codeguru.com/Cpp/controls/richedit/syntaxhilighting/article.php/c5369/)

Ali Imran
June 12th, 2005, 08:56 AM
First of all, I want to know

Can we highlight a word e.g. 'void' in a normal EDIT textbox ?
highlighting means giving it blue or green color.

My first preference is to use normal textbox, and I am 80% it can be done, but don't know how.


This one still remains unreplied. Anyone to provide more help kindly?


btw : I have seen all codes, downloaded and checked they are heavy ones I cant learn from, all of 5 to 10 files with 100s of lines of code :) so impossible for me to digout what exacting is done to colorize a particular part of text.

waiting for kind reply.

regards

kkez
June 12th, 2005, 09:38 AM
I don't think you can highlight something with a normal edit control (at least, not easily).

Instead, to highlight something in a richedit control:
Ex we have:
void something();
Then
1) (optional) Save the current selection (that means even the current position of the caret) with EM_GETSEL
2) Select the text you want to highlight with EM_SETSEL
SendMessage(hRichEditCtlr, EM_SETSEL, 0, 4);
3) Set the selection format with EM_SETCHARFORMAT
//let's set the text selected to blue and bold
CHARFORMAT cf;
cf.cbSize = sizeof(CHARFORMAT);
cf.dwMask = CFM_COLOR | CFM_BOLD;
cf.dwEffects = CFE_BOLD;
cf.crTextColor = RGB(0,0,255);

SendMessage(hRichEditCtrl, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf);
4) (optional) restore the previous selection with EM_SETSEL

it's done :)

Ali Imran
June 12th, 2005, 10:09 AM
That is great and accurate inforamtion. Thanks.

few more simple questions related to this topic:

1. does restoring the selection also mean to get cursor back to previous position ?

2. would you mind telling me how do I search all the text for word 'void' one by one to color it ? I am looking for some very efficient script.

3. What message do we receive when text is changed in the richedit box ?


your helpis highly appreciated.

regards

kkez
June 12th, 2005, 01:18 PM
1) yes, the cursor is a particular selection that start and ends at the same position :)

2) you can:




get the richedit text with GetWindowText(..) and search for all the occurrence of the string. I honestly don't know how to do that with char pointers, because i always do these things with the std::string, so if you don't mind here's my example:
//szText contains the text of the richedit control

std::string strText(szText); //char* to string
int nPos = 0;
int nOldPos = 0;

while( (nPos = strText.find("your word", nOldPos)) != std::string::npos)
{
//nPos is the position of the first char of the string
nOldPos = nPos +1;
}



Set the selection to the beginning of the richedit control, then use EM_FINDTEXT or EM_FINDTEXTEX to search the string (both messages return the index of the first character of the string), then move the selection after the index and continue to send EM_FINDTEXT messages till you reach the end (that is, SendMessage returns -1)
3) there's no specific notification sent by Windows when something is typed into a edit/richedit control. But, as these controls are windows, they receive as all the other windows (when a key is pressed when a window has the focus) the messages WM_KEYDOWN \ WM_KEYUP \ WM_CHAR, so you should subclass the richedit control and handle these messages

Another thing, if you SetWindowText to the richedit control, all the formatting (of course) disappear. This doesn't happen when the user type something inside the control.

SuperKoko
June 12th, 2005, 05:50 PM
The WM_COMMAND notification is sent when the focus is set/removed from an "EDIT" control, or a "RichEdit" control.
Any modification of the text content (keyboard modification, paste or undo operations), generate a WM_COMMAND notification for "EDIT" controls, but not for "RichEdit" controls.