Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding coloured line to rich edit control

Status
Not open for further replies.

Skute

Programmer
Jul 21, 2003
272
0
0
GB
Hi,

I need some example code / suggestions which will easily allow me to add a new line to a rich edit control and colour it accordingly.


Skute

"There are 10 types of people in this World, those that understand binary, and those that don't!"
 
As text in rich edit controls are in RTF, so there should be color definition part somewhere in the header:
{\colortbl \red0 \green0 \blue0; \red255 \green0 \blue0; }
and then color switch commands later in the text:
{\cf0 black text}{\cf1 red text}
New line (paragraph end) switch in RTF is \par
 
this is a bit different, but you should be able to pick out what you want from it:
Code:
ColourEdit.h
////////////////////////////////////////////////////////////
//
// Class      : CColourEdit
// Base Class : CEdit
// Author     : Robert Cumming
// Date       : August 2004
//
// Comment    : Class used to create colour edit control dialog's
//
// version 1  : Initial Release Version
//
//
////////////////////////////////////////////////////////////
#pragma once
#include "afxwin.h"

class CColourEdit : public CEdit
{
public:
  CColourEdit( void );
  ~CColourEdit( void );

  void SetTextColour( COLORREF refRGB );  // set the colour of the text in the edit control
  void SetBgColour( COLORREF refRGB );    // set the background colour of the edit control
protected:
  DECLARE_MESSAGE_MAP()
private:
  CBrush brush;     // brush used for painting in the control
  COLORREF rgb;     // colour used for background
  COLORREF text;    // colour used for text

  afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor); // paint the control
//  afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
};
Code:
ColourEdit.cpp
////////////////////////////////////////////////////////////
//
// Class      : CColourEdit
// Base Class : CEdit
// Author     : Robert Cumming
// Date       : August 2004
//
// Comment    : Class used to create colour edit control dialog's
//
// version 1  : Initial Release Version
//
//
////////////////////////////////////////////////////////////
#include "StdAfx.h"
#include ".\colouredit.h"

////////////////////////////////////////////////////////////
// Standard Constructor
////////////////////////////////////////////////////////////
CColourEdit::CColourEdit( void )
{
  rgb = RGB(255,0,0);
  text = RGB(0,0,0);
  brush.CreateSolidBrush( rgb );
}
////////////////////////////////////////////////////////////
// Standard Destructor
////////////////////////////////////////////////////////////
CColourEdit::~CColourEdit( void )
{
}
BEGIN_MESSAGE_MAP(CColourEdit, CEdit)
  ON_WM_CTLCOLOR_REFLECT()
END_MESSAGE_MAP()
////////////////////////////////////////////////////////////
// Set the colour of the text in the control
////////////////////////////////////////////////////////////
void CColourEdit::SetTextColour( COLORREF refRGB )
{
  text = refRGB;
}
////////////////////////////////////////////////////////////
// Set the background colour of the edit control
////////////////////////////////////////////////////////////
void CColourEdit::SetBgColour( COLORREF refRGB )
{
  rgb = refRGB;
}
////////////////////////////////////////////////////////////
// Routine to draw the colours of the control
////////////////////////////////////////////////////////////
HBRUSH CColourEdit::CtlColor(CDC* pDC, UINT nCtlColor)
{
  pDC->SetTextColor ( text );
  pDC->SetBkColor ( rgb );

  brush.DeleteObject();
  brush.CreateSolidBrush( rgb );

  return brush;
}

I imagine the same approach should work for other control types?

Robert Cumming
 
Yeah, but i want different coloured lines after each other, i.e.

white line
yellow line
red line

etc

thanks

Skute

"There are 10 types of people in this World, those that understand binary, and those that don't!"
 
Havent tried it yet ;)
(But i was looking for a simpler way than having to write RTF out)

Skute

"There are 10 types of people in this World, those that understand binary, and those that don't!"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top