How to add a line with words in different colors to TRichEdit?

How to add a line with words in different colors to TRichEdit?

Post by Igor Urden » Tue, 13 Jul 2004 19:07:47


Hi all!
I am working on the instant messenger project. I want to add a message from
the user in format "<username>: <message>" to rich edit. The <username> part
should be in one color and <message> in another. The problem is I cannot add
this in one line.
Could anybody help me with this?
Thank you
 
 
 

How to add a line with words in different colors to TRichEdit?

Post by Antonio Fe » Tue, 13 Jul 2004 19:51:05


Just a Sample:

( RE is a RichText )

void __fastcall TFrmLogMsg::AddLine( AnsiString Text, TColor Color = clBlack, bool Line, bool Bold, bool TimeStamp )
{
if ( Bold )
RE->SelAttributes->Style = TFontStyles()<< fsBold;
else
RE->SelAttributes->Style = TFontStyles();

if ( TimeStamp ) {
RE->SelAttributes->Color = clRed;
RE->SelText = Now().FormatString( "hh:nn:ss' - '" );
}

RE->SelAttributes->Color = Color;
RE->SelText = Text;
if ( Line )
RE->SelText = "\r\n";
}

HTH
Antonio

 
 
 

How to add a line with words in different colors to TRichEdit?

Post by Team » Wed, 14 Jul 2004 02:03:18


Use the SelStart and SelAttributes properties, ie:

void __fastcall TForm1::AddUserMessage(const AnsiString &User, const
AnsiString &Message)
{
RichEdit1->SelStart = RichEdit1->GetTextLen();
RichEdit1->SelAttributes->Color = clRed;
RichEdit1->SelText = User + ":";

RichEdit1->SelStart = RichEdit1->GetTextLen();
RichEdit1->SelAttributes->Color = clBlack;
RichEdit1->SelText = " " + Message;
}


Gambit
 
 
 

How to add a line with words in different colors to TRichEdit?

Post by Team » Wed, 14 Jul 2004 02:03:40


Small correction:

RichEdit1->SelText = " " + Message + "\r\n";


Gambit