Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


set a color of a push button

Posted on 2001-06-10
Medium Priority
Last Modified: 2013-11-20
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);
     // TODO: Return a non-NULL brush if the parent's handler should not be called
     return NULL;

can someone help ?
Question by:irisz
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +1
LVL 42

Expert Comment

by:Meir Rivkin
ID: 6172834
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
LVL 42

Expert Comment

by:Meir Rivkin
ID: 6172835
instead of CTLCOLOR_BUTTON it should be CTLCOLOR_BTN  

Author Comment

ID: 6175830
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.

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.


Expert Comment

ID: 6175912
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 & 
    BOOL bIsFocused  = (lpDrawItemStruct->itemState & 
    BOOL bIsDisabled = (lpDrawItemStruct->itemState & 
    CRect itemRect = lpDrawItemStruct->rcItem;
    //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
        //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->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->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->MoveTo(itemRect.left+1, itemRect.bottom-2);
      pDC->LineTo(itemRect.right-2, itemRect.bottom-2);
      pDC->LineTo(itemRect.right-2, itemRect.top);

    //Get the buttons title
    CString sTitle;
    CRect captionRect = lpDrawItemStruct->rcItem;
    //Set the button text
    if(sTitle.IsEmpty() == FALSE)
          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() -
                               (centerRect.Height() -

                       captionRect.Size(), (LPCTSTR)
                       sTitle, (bIsDisabled ?
                       DSS_DISABLED : DSS_NORMAL),
                   TRUE, 0, (CBrush*)NULL);

Hopes this helps in your Button color quest...

Have fun!


Author Comment

ID: 6175989
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 ??



Expert Comment

ID: 6176110
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().

Author Comment

ID: 6176214
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.

Expert Comment

ID: 6176547
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

LVL 42

Expert Comment

by:Meir Rivkin
ID: 6176741
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....
LVL 42

Expert Comment

by:Meir Rivkin
ID: 6176744
i can send u a sample application if u post your e-mail address...


Author Comment

ID: 6176787
here is my mail:

i would appritiate if you send me a sample code.

LVL 42

Accepted Solution

Meir Rivkin earned 60 total points
ID: 6177615
irisz: the sampe was sent.

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question