Solved

set a color of a push button

Posted on 2001-06-10
12
1,493 Views
Last Modified: 2013-11-20
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
Comment
Question by:irisz
[X]
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
12 Comments
 
LVL 42

Expert Comment

by:sedgwick
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
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 6172835
instead of CTLCOLOR_BUTTON it should be CTLCOLOR_BTN  
0
 

Author Comment

by:irisz
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.

Iris.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:newton1
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 & 
                        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
 

Author Comment

by:irisz
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 ??

thank,
Iris.



0
 
LVL 2

Expert Comment

by:MadYugoslav
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().
0
 

Author Comment

by:irisz
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.
0
 
LVL 2

Expert Comment

by:MadYugoslav
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
}
return(NULL);

0
 
LVL 42

Expert Comment

by:sedgwick
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....
0
 
LVL 42

Expert Comment

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

g'day
0
 

Author Comment

by:irisz
ID: 6176787
sedgwick,
here is my mail:
irisz@cimatron.co.il

i would appritiate if you send me a sample code.

Iris.
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 20 total points
ID: 6177615
irisz: the sampe was sent.
g'day
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

717 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