Setting Background Colour of Specific CEdit Controls 
Author Message
 Setting Background Colour of Specific CEdit Controls

Hi,

I've been using OnEraseBackground and ONCTLColour to customise my
application.

But i would now like to set some of my CEdit controls to have a default
background.

Say RED - for all controls that must be filled out.

I've tried to do this in ONCTLColour but it only changes the background when
the user
types in some text.

I can't get it to completely paint the background of the CEdit control.

How can i do this ???

Duncan



Sat, 30 Aug 2003 07:00:18 GMT  
 Setting Background Colour of Specific CEdit Controls
You have to return a brush of desired color in response to OnCtlColor. This
brush must not be local to the method.

--
Ajay Kalra [MVP]

Note: Please post all replies to Newsgroup only.


Quote:
> Hi,

> I've been using OnEraseBackground and ONCTLColour to customise my
> application.

> But i would now like to set some of my CEdit controls to have a default
> background.

> Say RED - for all controls that must be filled out.

> I've tried to do this in ONCTLColour but it only changes the background
when
> the user
> types in some text.

> I can't get it to completely paint the background of the CEdit control.

> How can i do this ???

> Duncan



Sat, 30 Aug 2003 07:15:30 GMT  
 Setting Background Colour of Specific CEdit Controls
Hi Duncan...

        Search for
            Article ID: Q74043 - HOWTO: Change the Color of an Edit Control

            HOWTO: Change the Background Color of an MFC Edit Control

    in msdn.

Cheers
Check Abdoul
------------------


Quote:
> Hi,

> I've been using OnEraseBackground and ONCTLColour to customise my
> application.

> But i would now like to set some of my CEdit controls to have a default
> background.

> Say RED - for all controls that must be filled out.

> I've tried to do this in ONCTLColour but it only changes the background
when
> the user
> types in some text.

> I can't get it to completely paint the background of the CEdit control.

> How can i do this ???

> Duncan



Sat, 30 Aug 2003 07:18:12 GMT  
 Setting Background Colour of Specific CEdit Controls
Ajay,

Here is the code i've got for OnCtrlColor:

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

 // TODO: Change any attributes of the DC here

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

 switch (pWnd->GetDlgCtrlID())
 {
  case IDC_JCCLIENT:
  case IDC_JCCONTACT:
   pDC->SetBkColor(RGB(255,0,0));
   pDC->SetTextColor(RGB(255,255,255));
   hbr = (HBRUSH) GetStockObject(NULL_BRUSH);
   break;
  default:
   hbr=CDialog::OnCtlColor(pDC,pWnd,nCtlColor);
   break;
 }

 return hbr;

Quote:
}

So what is the modification i need to make to set the background of the Edit
control to red and text to white ?

Many thanks for your help.

Duncan



Sat, 30 Aug 2003 16:25:07 GMT  
 Setting Background Colour of Specific CEdit Controls
Thanks for the help.

I've now written a new OnCtlColor handler - which works !

I create a brush (not static to the handler)

 COLORREF backcol = RGB(255,0,0);
 RedEdit.CreateSolidBrush(backcol);

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

 switch (nCtlColor)
 {
 case CTLCOLOR_EDIT:
 case CTLCOLOR_MSGBOX:
  switch (pWnd->GetDlgCtrlID())
  {
   case IDC_JCCLIENT:
   case IDC_JCCONTACT:
   case IDC_JCCLIENTQUOTE:
   case IDC_JCCLIENTREF:
   case IDC_JCCLIENTORDER:
    pDC->SetBkColor(RGB(255,0,0));
    pDC->SetTextColor(RGB(255,255,255));
    hbr = (HBRUSH) RedEdit; //GetStockObject(NULL_BRUSH);
    break;
   default:
    hbr=CDialog::OnCtlColor(pDC,pWnd,nCtlColor);
    break;
  }
  break;
 default:
  hbr=CDialog::OnCtlColor(pDC,pWnd,nCtlColor);
 }

 return hbr;

Quote:
}

Works like a charm.

Thanks for the help - it pointed me in the right direction.

Duncan


Quote:
> Ajay,

> Here is the code i've got for OnCtrlColor:

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

>  // TODO: Change any attributes of the DC here

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

>  switch (pWnd->GetDlgCtrlID())
>  {
>   case IDC_JCCLIENT:
>   case IDC_JCCONTACT:
>    pDC->SetBkColor(RGB(255,0,0));
>    pDC->SetTextColor(RGB(255,255,255));
>    hbr = (HBRUSH) GetStockObject(NULL_BRUSH);
>    break;
>   default:
>    hbr=CDialog::OnCtlColor(pDC,pWnd,nCtlColor);
>    break;
>  }

>  return hbr;
> }

> So what is the modification i need to make to set the background of the
Edit
> control to red and text to white ?

> Many thanks for your help.

> Duncan



Sat, 30 Aug 2003 21:19:19 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Setting the background colour in a CEdit control.

2. Background colour in a CEdit control

3. CEdit control background colour

4. Set cursor to a specific CEdit control

5. How do you set the background color of a CEdit control

6. CEdit background colour.

7. Setting window background colour from a DLL

8. Setting background colour of CEditview.

9. Setting default background colour.

10. CFont, CEdit, setting font colour question

11. CEdit: set cursor to specific position

12. Changing text background colour in a Edit control

 

 
Powered by phpBB® Forum Software