
Colored text in MultiLine CEdit
No, don't do this. It is a very silly thing to do, because it means you now have
responsibility for all the painting in the edit control, which makes the use of the edit
control pointless. You could have done an ordinary CWnd.
First, the error is that you get a DC and set the text color. This does nothing, since
there is nothing done with the DC to use that information. DCs are created when you need
to draw, and I see no drawing being done here (nor should there be any drawing done).
What ;you need to do is create a subclass of the CEdit, and implement the reflected
WM_CTLCOLOR handler, =WM_CTLCOLOR. In that, you will be passed the DC the control is about
to use for its painting. At that point, you set the text color. You must also return a
brush handle for the background painting, and this should be declared in your class as
CBrush bkgnd;
and initialized in your constructor as
bggnd.CreateSolidBrush(::GetSysColor(COLOR_WINDOW));
Note that you can only set ONE color for ALL the text in a CEdit. If you want to use
multiple text colors, you need to look into a rich edit control (for example, I use one to
display "selection options" and if there are errors or inconsistencies, I use a boldfaced
red font to indicate the error).
joe
Quote:
>>I couldn't found anything useful to wirte colorful text in a simple
>>Multiline CEditView .
>>I am inserting lines into CEditView with CEdit::ReplaceSel(). I tried;
>>CDC* pDC = GetDC();
>>pDC->SetTextColor(RGB(...));
>>// Finaly
>>GetEditCtrl().ReplaceSel(....);
>>But the texts are always in black.
>>I will be glad if someone tell me how to achieve colorful text in CEditView.
>AFAIK the only way to do this is write you own class derived from CEdit
>and overwrite OnPaint. That's the way I would do this.
>Joe
Joseph M. Newcomer [MVP]
Web: http://www3.pgh.net/~newcomer
MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm