Colored text in MultiLine CEdit 
Author Message
 Colored text in MultiLine CEdit

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.



Fri, 04 Jun 2004 23:38:39 GMT  
 Colored text in MultiLine CEdit


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

--
Effizienz ist die Faulheit der Intelligenten.

http://huntcase.musicpage.de



Fri, 04 Jun 2004 23:38:54 GMT  
 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


Sat, 05 Jun 2004 00:04:22 GMT  
 Colored text in MultiLine CEdit
Hi,

You should call SetTextColor in the WM_CTLCOLOR  handler.

Please refer to following code.

HBRUSH CAboutDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
        HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

   if (pWnd->GetDlgCtrlID() == IDC_EDIT1)
   {
      // Set the text color to red
      pDC->SetTextColor(RGB(255, 0, 0));

      // Set the background mode for text to transparent
      // so background will show thru.
   //   pDC->SetBkMode(TRANSPARENT);

      // Return handle to our CBrush object
  //    hbr = m_brush;
   }

        // TODO: Return a different brush if the default is not desired
        return hbr;

Quote:
}

Best regards,
Billy Zhang
Microsoft

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. ? 2001 Microsoft Corporation. All rights
reserved.



Sun, 06 Jun 2004 14:24:03 GMT  
 Colored text in MultiLine CEdit
I understand the corner you have pointed out, but still I have a question,
how about multi lines with different colors? What should I do write texts
with different colors ?

Thank you very much.


Quote:
> Hi,

> You should call SetTextColor in the WM_CTLCOLOR  handler.

> Please refer to following code.

> HBRUSH CAboutDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
> {
> HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

>    if (pWnd->GetDlgCtrlID() == IDC_EDIT1)
>    {
>       // Set the text color to red
>       pDC->SetTextColor(RGB(255, 0, 0));

>       // Set the background mode for text to transparent
>       // so background will show thru.
>    //   pDC->SetBkMode(TRANSPARENT);

>       // Return handle to our CBrush object
>   //    hbr = m_brush;
>    }

> // TODO: Return a different brush if the default is not desired
> return hbr;
> }

> Best regards,
> Billy Zhang
> Microsoft

> This posting is provided "AS IS" with no warranties, and confers no
rights.
> You assume all risk for your use. ? 2001 Microsoft Corporation. All rights
> reserved.



Sun, 06 Jun 2004 15:58:56 GMT  
 Colored text in MultiLine CEdit
Dont use an edit control if you want multiple colored texts. Use a Rich Edit
Control instead.

--
Ajay Kalra [MVP - VC++]

Note: Please post all replies to newsgroup only.


Quote:
> I understand the corner you have pointed out, but still I have a question,
> how about multi lines with different colors? What should I do write texts
> with different colors ?

> Thank you very much.



> > Hi,

> > You should call SetTextColor in the WM_CTLCOLOR  handler.

> > Please refer to following code.

> > HBRUSH CAboutDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
> > {
> > HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

> >    if (pWnd->GetDlgCtrlID() == IDC_EDIT1)
> >    {
> >       // Set the text color to red
> >       pDC->SetTextColor(RGB(255, 0, 0));

> >       // Set the background mode for text to transparent
> >       // so background will show thru.
> >    //   pDC->SetBkMode(TRANSPARENT);

> >       // Return handle to our CBrush object
> >   //    hbr = m_brush;
> >    }

> > // TODO: Return a different brush if the default is not desired
> > return hbr;
> > }

> > Best regards,
> > Billy Zhang
> > Microsoft

> > This posting is provided "AS IS" with no warranties, and confers no
> rights.
> > You assume all risk for your use. ? 2001 Microsoft Corporation. All
rights
> > reserved.



Sun, 06 Jun 2004 16:03:04 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Set text color for 1 line in a CEdit control (MultiLine)

2. multiline CEdit background color

3. Multiline CEdit custom background color problem

4. How to set text into CEdit (Multiline)

5. Text erases itself in my multiline CEdit ! Help

6. How to set multiline text in a CEdit?

7. again, CEdit multiline text

8. Background & Text color CEdit

9. CEdit text color

10. Changing text color in a CEdit box

11. coloring text in CEdit or....?

12. Changing Background Color of CEdit - Problem With Double Clicking On Texts

 

 
Powered by phpBB® Forum Software