Changing Backround Color and ReadOnly status in CEdit control 
Author Message
 Changing Backround Color and ReadOnly status in CEdit control

    Hi!

    How can I change the background color and read-only status of CEdit
control in run-time?

    Best regards,
        Martin



Fri, 12 Nov 2004 21:39:31 GMT  
 Changing Backround Color and ReadOnly status in CEdit control
Take a look at following article for changing color:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q117778

--
Ajay Kalra [MVP - VC++]


Quote:

>     Hi!

>     How can I change the background color and read-only status of CEdit
> control in run-time?

>     Best regards,
>         Martin



Fri, 12 Nov 2004 23:27:32 GMT  
 Changing Backround Color and ReadOnly status in CEdit control
Hi Martin,

You can remove the read-only status by calling CEdit::SetReadOnly(FALSE).

To change the background color, you need to handle WM_CTLCOLOREDIT (or
WM_CTLCOLORSTATIC when it's read-only) in the edit controls's parent (ie
your dialog or view). Rather than messing around with the parent, I've found
it easier just to derive a new class from CEdit, let's say CMyEdit, and
handle WM_CTLCOLOR right inside of the derived edit class via MFC's
'reflection' mechanism. In class wizard for this particular case, you'll be
looking to map < =WM_CTLCOLOR >, the equal siginifying that this is a
reflected message. Then you just return the handle to one of your own
brushes and windows will paint the edit's background with this brush. For
example:

class CMyEdit : public CEdit
{
public:
    CMyEdit();
    virtual ~CMyEdit();

    CBrush m_brush; // Brush to paint background of edit
    afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
    DECLARE_MESSAGE_MAP()

Quote:
}

CMyEdit::CMyEdit()
{
    m_brush.CreateSolidBrush(RGB(255,0,0)); // {*filter*} red brush just for fun

Quote:
}

CMyEdit::~CMyEdit()
{
    m_brush.DeleteObject();

Quote:
}

HBRUSH CMyEdit::CtlColor(CDC* pDC, UINT nCtlColor)
{
    return m_brush;

Quote:
}

Note that the above never checks for nCtlColor == CTL_COLOREDIT; I've never
found a need to in these reflected handlers because you are only getting the
'reflection' for your particular control anyway. Indeed, if you did check
it, you'd see that nCtlColor had a value of CTL_COLOREDIT when the control
was 'normal', and CTL_COLORSTATIC when the control was read-only.

Regards,
Mike


Quote:

>     Hi!

>     How can I change the background color and read-only status of CEdit
> control in run-time?

>     Best regards,
>         Martin



Fri, 12 Nov 2004 23:54:33 GMT  
 Changing Backround Color and ReadOnly status in CEdit control

    Thank you for your help Michael and Ajay Karla.

    Best regards,
        Martin



Quote:
> Hi Martin,

> You can remove the read-only status by calling CEdit::SetReadOnly(FALSE).

> To change the background color, you need to handle WM_CTLCOLOREDIT (or
> WM_CTLCOLORSTATIC when it's read-only) in the edit controls's parent (ie
> your dialog or view). Rather than messing around with the parent, I've
found
> it easier just to derive a new class from CEdit, let's say CMyEdit, and
> handle WM_CTLCOLOR right inside of the derived edit class via MFC's
> 'reflection' mechanism. In class wizard for this particular case, you'll
be
> looking to map < =WM_CTLCOLOR >, the equal siginifying that this is a
> reflected message. Then you just return the handle to one of your own
> brushes and windows will paint the edit's background with this brush. For
> example:

> class CMyEdit : public CEdit
> {
> public:
>     CMyEdit();
>     virtual ~CMyEdit();

>     CBrush m_brush; // Brush to paint background of edit
>     afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
>     DECLARE_MESSAGE_MAP()
> }

> CMyEdit::CMyEdit()
> {
>     m_brush.CreateSolidBrush(RGB(255,0,0)); // {*filter*} red brush just for
fun
> }

> CMyEdit::~CMyEdit()
> {
>     m_brush.DeleteObject();
> }

> HBRUSH CMyEdit::CtlColor(CDC* pDC, UINT nCtlColor)
> {
>     return m_brush;
> }

> Note that the above never checks for nCtlColor == CTL_COLOREDIT; I've
never
> found a need to in these reflected handlers because you are only getting
the
> 'reflection' for your particular control anyway. Indeed, if you did check
> it, you'd see that nCtlColor had a value of CTL_COLOREDIT when the control
> was 'normal', and CTL_COLORSTATIC when the control was read-only.

> Regards,
> Mike



> >     Hi!

> >     How can I change the background color and read-only status of CEdit
> > control in run-time?

> >     Best regards,
> >         Martin



Sat, 13 Nov 2004 01:01:28 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Cedit Readonly Falg Color Change?

2. Changin the color of a ReadOnly CEdit control

3. Changing Backround Color

4. background color in readonly CEdit

5. Question on changing text color in CEdit Control

6. Changing the default background color of disabled control (CEdit)

7. Changing the color of a CEdit control

8. How to change background color of a dynamically created CEdit control

9. CEdit control (changing back color)

10. Change background color of disabled CEdit controls

11. Changing the Background Color of Individual CEdit controls in a CDialogBar

12. CEdit controls and changing the font color

 

 
Powered by phpBB® Forum Software