Click here to Skip to main content
15,891,684 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a MFC Edit control which i use to output multiple lines of text onto. I can color the text but it only colors everything, it won't color individual lines of text.

I know this is because i treat the text as one big buffer, but is there a way around this?

My Class:
C++
CColorEdit::CColorEdit()
{
	BackColor = GetSysColor( COLOR_3DFACE );
	TextColor = RGB( 0, 0, 0 );
	BackBrush.CreateSolidBrush( BackColor );
}

CColorEdit::~CColorEdit()
{
}

void CColorEdit::SetReadOnly ()
{
	CEdit::SetReadOnly( true );
	SetBGColor( RGB( 255, 255, 255 ) );
	SetTextColor( RGB( 0, 0, 0 ) );
}

void CColorEdit::SetTextColor ( COLORREF Color )
{
	TextColor = Color;
	RedrawWindow();
}

void CColorEdit::SetBGColor ( COLORREF Color )
{
	BackColor = Color;

	BackBrush.DeleteObject();
	BackBrush.CreateSolidBrush( BackColor );
	RedrawWindow( );
}

HBRUSH CColorEdit::CtlColor(CDC* pDC, UINT nCtlColor)
{
	pDC->SetBkColor( BackColor ); 
	pDC->SetTextColor( TextColor );
	nCtlColor = CTLCOLOR_EDIT;

	return BackBrush;
}

BEGIN_MESSAGE_MAP(CColorEdit, CEdit)
	ON_WM_CTLCOLOR_REFLECT()
END_MESSAGE_MAP()


I call like:
C++
Tools.AppendWindowText( RGB( 0, 192, 0 ), "[%s] %s", CurrentService->Status, CurrentService->Name );


Here's the function:
C++
void CTools::AppendWindowText( COLORREF Color, char* AppendText, ... )
{
    static char AppendData[ 256 ];
    static char CurrentData[ 2048000 ];
    ZeroMemory( CurrentData, sizeof( CurrentData ) );
    ZeroMemory( AppendData, sizeof( AppendData ) );

    va_list valist;
    va_start( valist, AppendText );
    _vsnprintf( AppendData, sizeof( AppendData ) - strlen( AppendData ), AppendText, valist );
    va_end( valist );

    int CurrentTextLen = pClientDlg->m_ResultsWindow.GetWindowTextLengthA( );
    int AppendDataLen = strlen( AppendData );
    int DataLength = ( CurrentTextLen + AppendDataLen + 2 );

    pClientDlg->m_ResultsWindow.GetWindowTextA( CurrentData, DataLength );
    if ( CurrentTextLen > 0 ) { strcat( CurrentData, "\r\n" ); }

    strcat( CurrentData, AppendData );
    pClientDlg->m_ResultsWindow.SetTextColor( Color );
    pClientDlg->m_ResultsWindow.SetWindowTextA( CurrentData );
}


Thanks
Posted

1 solution

Hello, I believe you have 4 options:

1) Use a rich edit control
2) Use a listbox
3) Draw the text yourself (but I dont see in the code above a mechanism for storing which code gets what color)
4) Use html and a html view (if your application is using frame windows)

what your trying to do can be done, just needs a little extra code
 
Share this answer
 

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