Q: Change bkcolor fo control : strange behavior??? 
Author Message
 Q: Change bkcolor fo control : strange behavior???

Hello all,

In a formview based application, I've created a handle for the WM_CTCOLOR
message. Hoping that this will do the trick to change the color of the used
controls.

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

 // TODO: Change any attributes of the DC here
 pDC->SetBkColor(RGB(0,180,0));
 pDC->SetTextColor(RGB(255,0,0));

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

Quote:
}

It works, but the controls are filled with strings shorter than the
controlbox width. The result is that only the part wich is filled by the
string has changed color (bkcolor).
The rest of the control box is still gray.
What goes wrong?

What should be done if I want to change the color of individual control
boxes (bk or text).

When and by whom is the WM_CTCOLOR message generatered?

Thanks in advance,

Kimball



Sat, 07 Dec 2002 03:00:00 GMT  
 Q: Change bkcolor fo control : strange behavior???
FDB:

You have to change the brush.

HTH,

David Wilkinson

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

Quote:

> Hello all,

> In a formview based application, I've created a handle for the WM_CTCOLOR
> message. Hoping that this will do the trick to change the color of the used
> controls.

> HBRUSH CWShieldView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
> {
>  HBRUSH hbr = CFormView::OnCtlColor(pDC, pWnd, nCtlColor);

>  // TODO: Change any attributes of the DC here
>  pDC->SetBkColor(RGB(0,180,0));
>  pDC->SetTextColor(RGB(255,0,0));

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

> It works, but the controls are filled with strings shorter than the
> controlbox width. The result is that only the part wich is filled by the
> string has changed color (bkcolor).
> The rest of the control box is still gray.
> What goes wrong?

> What should be done if I want to change the color of individual control
> boxes (bk or text).

> When and by whom is the WM_CTCOLOR message generatered?

> Thanks in advance,

> Kimball



Sat, 07 Dec 2002 03:00:00 GMT  
 Q: Change bkcolor fo control : strange behavior???
Hi Kimball,

As an answer, here is an exemple:

/*  Oops... sorry for the define's :-) */
#define TEXT_COLOR RGB( 0, 0, 0 ); // Black
#define BK_COLOR RGB( 0, 0, 255 ); // Blue

HBRUSH CYourClass::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
UINT ControlD = pWnd->GetDlgCtrlID();

switch (nCtlColor){        
        case CTLCOLOR_LISTBOX: //ListBox color
          // you can select each of your control separately
           if( ControlID == IDC_LIST_A_SPECIFIC_LIST ) ...

        case CTLCOLOR_MSGBOX: // MsgBox color
        case CTLCOLOR_SCROLLBAR: // scrollbar color
        case CTLCOLOR_BTN:
                case CTLCOLOR_STATIC:      
        case CTLCOLOR_EDIT:
        case CTLCOLOR_DLG:
        default:
           m_Brush.DeleteObject(); //Deleting previously created brush
           pDC->SetBkMode( TRANSPARENT );
           pDC->SetTextColor( TEXT_COLOR );
           m_Brush.CreateSolidBrush( BK_COLOR );
           return( HBRUSH )( m_Brush.m_hObject );  
        }

Quote:
}

As you can see, i use only one color for all controls, but to used
different colors, just create and return a brush of the needed color
in each "case".

Notes:
1. m_Brush is a CBrush member of CYourClass
2. the CTLCOLOR_BTN only represents the BACKGROUND color of buttons,
ie the portion of the windows that is  the back on the rear of the
button. To change the color of the button itself, you have to  "custom
draw" it.

JD



Sun, 08 Dec 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Change BKcolor of a control.

2. changing bkcolor of a static control

3. Change the bkColor of a disabled control ...

4. Changing bkcolor of edit-control

5. Strange ATL Button Control Behavior

6. Strange behavior when subclassing a static control in modal dialog

7. Strange, very strange OnGetMinMaxInfo() behavior

8. Strange behavior of Access as control host

9. Strange, very strange OnGetMinMaxInfo() behavior

10. Changing BkColor of CFormView???

11. To Change FgColor and BkColor of a readonly CEdit field

 

 
Powered by phpBB® Forum Software