Background color in CEdit Control ? 
Author Message
 Background color in CEdit Control ?

Hy,

I,m trying to change the background color of a CEdit control, but it seems
it does not work the way i try.
Only the the line of the cursor is set to the color i want, but not the
whole CEdti control.
Can anyone give me some advice.
Thx.
Martin

Here is my sample code:

BOOL CNotepadDlg::OnInitDialog()
{
 CDialog::OnInitDialog();
.
.
 CBrush Brush;
 Brush.CreateSolidBrush( RGB(255,255,0) );
 m_hYell = (HBRUSH)Brush.GetSafeHandle();
.
.

BRUSH CNotepadDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{

 // TODO: Change any attributes of the DC here
 if( nCtlColor == CTLCOLOR_EDIT )
 {
  pDC->SetBkColor(RGB(255,255,0));
  return m_hYell;
 }
 // TODO: Return a different brush if the default is not desired
 return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

Quote:
}



Fri, 02 Aug 2002 03:00:00 GMT  
 Background color in CEdit Control ?
Martin:

Isn't your CBrush Brush going out of scope, taking its handle with it? You
need to make the brush itself a member variable of the dialog. Return the
HBRUSH of this brush in OnCtlColor().

HTH,

David Wilkinson

=======================

Quote:

> Hy,

> I,m trying to change the background color of a CEdit control, but it seems
> it does not work the way i try.
> Only the the line of the cursor is set to the color i want, but not the
> whole CEdti control.
> Can anyone give me some advice.
> Thx.
> Martin

> Here is my sample code:

> BOOL CNotepadDlg::OnInitDialog()
> {
>  CDialog::OnInitDialog();
> .
> .
>  CBrush Brush;
>  Brush.CreateSolidBrush( RGB(255,255,0) );
>  m_hYell = (HBRUSH)Brush.GetSafeHandle();
> .
> .

> BRUSH CNotepadDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
> {

>  // TODO: Change any attributes of the DC here
>  if( nCtlColor == CTLCOLOR_EDIT )
>  {
>   pDC->SetBkColor(RGB(255,255,0));
>   return m_hYell;
>  }
>  // TODO: Return a different brush if the default is not desired
>  return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
> }



Fri, 02 Aug 2002 03:00:00 GMT  
 Background color in CEdit Control ?
Sorry - of course m_hYell is a menber of the dialog.


Quote:
> Martin:

> Isn't your CBrush Brush going out of scope, taking its handle with it? You
> need to make the brush itself a member variable of the dialog. Return the
> HBRUSH of this brush in OnCtlColor().

> HTH,

> David Wilkinson

> =======================


> > Hy,

> > I,m trying to change the background color of a CEdit control, but it
seems
> > it does not work the way i try.
> > Only the the line of the cursor is set to the color i want, but not the
> > whole CEdti control.
> > Can anyone give me some advice.
> > Thx.
> > Martin

> > Here is my sample code:

> > BOOL CNotepadDlg::OnInitDialog()
> > {
> >  CDialog::OnInitDialog();
> > .
> > .
> >  CBrush Brush;
> >  Brush.CreateSolidBrush( RGB(255,255,0) );
> >  m_hYell = (HBRUSH)Brush.GetSafeHandle();
> > .
> > .

> > BRUSH CNotepadDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
> > {

> >  // TODO: Change any attributes of the DC here
> >  if( nCtlColor == CTLCOLOR_EDIT )
> >  {
> >   pDC->SetBkColor(RGB(255,255,0));
> >   return m_hYell;
> >  }
> >  // TODO: Return a different brush if the default is not desired
> >  return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
> > }



Sat, 03 Aug 2002 03:00:00 GMT  
 Background color in CEdit Control ?
martin:

Yes, m_hYell is a member of the dialog, but the CBrush which owns the handle is
not. Make the CBrush a member variable of the dialog and extract its handle in
OnCtlColor().

I'm sure there is another way to do it, but since you're using MFC it makes
sense (to me)  to limit the references to HBRUSH ro the OnCtlColor function
where it is needed.

HTH,

David Wilkinson

======================

Quote:

> Sorry - of course m_hYell is a menber of the dialog.



> > Martin:

> > Isn't your CBrush Brush going out of scope, taking its handle with it? You
> > need to make the brush itself a member variable of the dialog. Return the
> > HBRUSH of this brush in OnCtlColor().

> > HTH,

> > David Wilkinson

> > =======================


> > > Hy,

> > > I,m trying to change the background color of a CEdit control, but it
> seems
> > > it does not work the way i try.
> > > Only the the line of the cursor is set to the color i want, but not the
> > > whole CEdti control.
> > > Can anyone give me some advice.
> > > Thx.
> > > Martin

> > > Here is my sample code:

> > > BOOL CNotepadDlg::OnInitDialog()
> > > {
> > >  CDialog::OnInitDialog();
> > > .
> > > .
> > >  CBrush Brush;
> > >  Brush.CreateSolidBrush( RGB(255,255,0) );
> > >  m_hYell = (HBRUSH)Brush.GetSafeHandle();
> > > .
> > > .

> > > BRUSH CNotepadDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
> > > {

> > >  // TODO: Change any attributes of the DC here
> > >  if( nCtlColor == CTLCOLOR_EDIT )
> > >  {
> > >   pDC->SetBkColor(RGB(255,255,0));
> > >   return m_hYell;
> > >  }
> > >  // TODO: Return a different brush if the default is not desired
> > >  return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
> > > }



Sat, 03 Aug 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. background color of CEdit control

2. How to change the background color of CEdit control?

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

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

5. Change background color of disabled CEdit controls

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

7. Colored background and text in a CEdit control

8. Changing color of background of a CEdit control

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

10. set the text color and the background color of a Control Cbutton

11. Win2K: static background color vs. CFormView background color

12. Background & Text color CEdit

 

 
Powered by phpBB® Forum Software