• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1537
  • Last Modified:

set a color of a push button

Hi,
I tried to use CtlColor event on a class derived from CButton, but it didnt help:

HBRUSH CMyButton::CtlColor(CDC* pDC, UINT nCtlColor)
{
     COLORREF aColor = RGB(255,0,0);
     pDC->SetTextColor(aColor);
     
     // TODO: Return a non-NULL brush if the parent's handler should not be called
     return NULL;
}

can someone help ?
Iris.
0
irisz
Asked:
irisz
  • 5
  • 4
  • 2
  • +1
1 Solution
 
Meir RivkinFull stack Software EngineerCommented:
override OnCtlColor not CtlColor....->


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

     if ( CTLCOLOR_BUTTON == nCtlColor )    
     {
          if ( GetDlgItem( IDC_MYBUTTON)->GetSafeHwnd() == pWnd->GetSafeHwnd() )
               pDC->SetTextColor( RGB( 255, 0, 0 ) );    

}

good luck mate
0
 
Meir RivkinFull stack Software EngineerCommented:
instead of CTLCOLOR_BUTTON it should be CTLCOLOR_BTN  
0
 
iriszAuthor Commented:
I tried your way but I still get black text...
Besides, I would prefer to color the text in the responsibility of the button and not the dialog.

Iris.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
newton1Commented:
Hi irisz,

This may be a little overkill for what you are wanting to achieve. The benefits of this method are that you can at anytime totally change the appearance of your buttons by modifying a few lines of code. What I'm talking about is using an ownerdrawn button. Please try the code I've provided below. Note the areas that I have commented that will change the text color, and the background color of your CButton derived buttons. First you must override "DrawItem" from within your derived button class.
Make certain to choose from within your resource editor that the button<s> you wish to use this method will have the OwnerDraw property selected.
 
Paste the following code into your "DrawItem" method.

void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
    CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
      
    CPen *pOldPen;
    BOOL bIsPressed  = (lpDrawItemStruct->itemState & 
                        ODS_SELECTED);
    BOOL bIsFocused  = (lpDrawItemStruct->itemState & 
                        ODS_FOCUS);
    BOOL bIsDisabled = (lpDrawItemStruct->itemState & 
                        ODS_DISABLED);
      
    CRect itemRect = lpDrawItemStruct->rcItem;
      
    pDC->SetBkMode(TRANSPARENT);
      
    //Prepare the button to be drawn upon. (This is  
    //required to get the proper effect)
    CBrush br(RGB(255, 255, 255));
      
    pDC->FillRect(&itemRect, &br);
      
    //Draw pressed button
    if(bIsPressed)
    {
        //draw the 3D shadow around the button frame
        CBrush brBtnShadow(GetSysColor(COLOR_BTNSHADOW));
      pDC->FrameRect(&itemRect, &brBtnShadow);      
    }
    else //draw non pressed button
    {
      CPen penBtnHiLight(PS_SOLID, 0, GetSysColor
                          (COLOR_BTNHILIGHT)); //White
      CPen pen3DLight(PS_SOLID, 0, GetSysColor
                       (COLOR_3DDKSHADOW));    //Light gray
      CPen penBtnShadow(PS_SOLID, 0, GetSysColor
                         (COLOR_BTNSHADOW));   //Dark gray
      CPen pen3DDKShadow(PS_SOLID, 0, GetSysColor
                          (COLOR_3DDKSHADOW)); //Black
            
      //White line
      pOldPen = pDC->SelectObject(&penBtnHiLight);
      pDC->MoveTo(itemRect.left, itemRect.bottom-1);
      pDC->LineTo(itemRect.left, itemRect.top);
      pDC->LineTo(itemRect.right, itemRect.top);
            
      //Light gray line
      pDC->SelectObject(pen3DLight);
      pDC->MoveTo(itemRect.left+1, itemRect.bottom-1);
      pDC->LineTo(itemRect.left+1, itemRect.top+1);
      pDC->LineTo(itemRect.right, itemRect.top+1);
            
      //Black line
      pDC->SelectObject(pen3DDKShadow);
      pDC->MoveTo(itemRect.left, itemRect.bottom-1);
      pDC->LineTo(itemRect.right-1, itemRect.bottom-1);
      pDC->LineTo(itemRect.right-1, itemRect.top-1);
            
      //Dark gray line
      pDC->SelectObject(penBtnShadow);
      pDC->MoveTo(itemRect.left+1, itemRect.bottom-2);
      pDC->LineTo(itemRect.right-2, itemRect.bottom-2);
      pDC->LineTo(itemRect.right-2, itemRect.top);
            
      pDC->SelectObject(pOldPen);
    }

    //Get the buttons title
    CString sTitle;
    GetWindowText(sTitle);
      
    CRect captionRect = lpDrawItemStruct->rcItem;
      
    //Set the button text
    if(sTitle.IsEmpty() == FALSE)
    {
      if(bIsPressed)
          captionRect.OffsetRect(1, 1);

      //Set text color to BLUE and Background to WHITE
      pDC->SetTextColor(RGB(0, 0, 255));
      pDC->SetBkColor(RGB(255, 255, 255));
      
      //Center text on button
      CRect centerRect = captionRect;
      pDC->DrawText(sTitle, -1, captionRect,
                      DT_SINGLELINE | DT_CALCRECT);
      captionRect.OffsetRect((centerRect.Width() -
                               captionRect.Width())/2,
                               (centerRect.Height() -
                                captionRect.Height())/2);

        pDC->SetBkMode(TRANSPARENT);
      pDC->DrawState(captionRect.TopLeft(),
                       captionRect.Size(), (LPCTSTR)
                       sTitle, (bIsDisabled ?
                       DSS_DISABLED : DSS_NORMAL),
                   TRUE, 0, (CBrush*)NULL);
    }
}

Hopes this helps in your Button color quest...

Have fun!

Newton1
0
 
iriszAuthor Commented:
Dear newton1,
All I wanted is to change the color of the text of a push button. Without making it owner-drawn and draw the text myself.
Is this to much to ask ??

thank,
Iris.



0
 
MadYugoslavCommented:
First CTLCOLOR_BTN is Type of message that You need to catch. Second thing is that You need to return HBRUSH of Your background for Button. You may get HBRUSH value with function GetSysColor().
0
 
iriszAuthor Commented:
Obviously, I caught the message...
I dont think the returning of the brush should effect the color of the text...NeverTheLess, I tried to return a brush and it did not help.
0
 
MadYugoslavCommented:
In Control constructor call:
m_clrText = RGB( 0xFF, 0, 0 );
m_clrBkgnd = GetSysColor(COLOR_BTNFACE);
m_brBkgnd.CreateSolidBrush( GetSysColor(COLOR_BTNFACE) );
(add m_clrText, m_clrBkgnd and m_brBkgnd as class members).


In OnCtlColor call:
if( pWnd->GetDlgCtrlID() == IDC_MY_BUTTON )
{
   pDC->SetTextColor( m_clrText );    // text
   pDC->SetBkColor( m_clrBkgnd );    // text bkgnd
   return m_brBkgnd;                // ctl bkgnd
}
return(NULL);

0
 
Meir RivkinFull stack Software EngineerCommented:
irisz, did u try my suggestion?
i'm using this code every day and it works fine.
i needed to change the text color of a static in order to simulate link to some web site, here's the code:

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

     if ( CTLCOLOR_STATIC == nCtlColor )    
     {
          if ( GetDlgItem( IDC_Z3OFFICIALWEBSITE )->GetSafeHwnd() == pWnd->GetSafeHwnd() )
               pDC->SetTextColor( RGB( 0, 0, 255 ) );    
     }
     
                   
     return hbr;
}

the only things u need 2 change are:
1. CTLCOLOR_STATIC should be CTLCOLOR_BTN //for button ctrl
2. IDC_Z3OFFICIALWEBSITE should be your button resource ID

if it doesn't work something wrong with your program or something....
0
 
Meir RivkinFull stack Software EngineerCommented:
i can send u a sample application if u post your e-mail address...

g'day
0
 
iriszAuthor Commented:
sedgwick,
here is my mail:
irisz@cimatron.co.il

i would appritiate if you send me a sample code.

Iris.
0
 
Meir RivkinFull stack Software EngineerCommented:
irisz: the sampe was sent.
g'day
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

  • 5
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now