Click here to Skip to main content
15,896,502 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, in my dialog based application i use edit box control. I want to make it color some specific words in some color, not the whole thing written into the edit box. Thank you for help in advance!
Posted

1 solution

That's hard to do with a regular edit box. I see the following options:

- Override the OnPaint handler and do the entire painting by custom code. That appears to me as really difficult if you want to support the full range of capabilities of an edit control.

- Use a rich text edit control instead of an edit control.

You might also want to search this website for syntax coloring controls used in text editors. There you might get some more ideas.
 
Share this answer
 
Comments
Jijesh Balakrishnan b 15-Mar-12 6:48am    
how can i implement this with rich edit text box?
enhzflep 15-Mar-12 9:39am    
Use the following code to selectively colour text contained within a RichEdit control.


void changetextcolor(HWND hwnd,char *str,COLORREF col)
{
int lResult;
FINDTEXTEX fte; // This structure contains information about text to search for
CHARFORMAT cf; // This structure contains information about character formatting

// Set the range where to search
fte.chrg.cpMin=0; // Character position index immediately preceding the first character in the range
fte.chrg.cpMax=-1; // Character position immediately following the last character in the range

do
{
fte.lpstrText=str; // Pointer to the null-terminated string to find
// lResult=SendMessage(hwnd,(UINT)EM_FINDTEXTEX,(WPARAM)FR_DOWN,(LPARAM)&fte); // Find it
lResult=SendMessage(hwnd,(UINT)EM_FINDTEXTEX,(WPARAM)1,(LPARAM)&fte); // Find it
if(lResult!=-1) // There are more matches
{
// Set selection
if(SendMessage(hwnd,(UINT)EM_EXSETSEL,(WPARAM)0,(LPARAM)&fte.chrgText)) // Success
{
cf.cbSize=sizeof(cf);
cf.dwMask=CFM_COLOR;
cf.crTextColor=col;
SendMessage(hwnd,EM_SETCHARFORMAT,SCF_SELECTION,(LPARAM)&cf);
}
fte.chrg.cpMin=fte.chrgText.cpMax; // Set start to find more matches
}
}
while(lResult!=-1); // There are more matches
}
Sergey Alexandrovich Kryukov 15-Mar-12 23:19pm    
That's correct, a 5.
--SA
Jijesh Balakrishnan b 16-Mar-12 1:12am    
okay,thank you
let me try this
Jijesh Balakrishnan b 16-Mar-12 1:25am    
i have one text file to read and insert the content of that file to a richtextbox. in that richtextbox, i want to highlight all the words with different colors.
where should i use the above sample program that you have sent to me?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900