Solved

set a color of a push button

Posted on 2001-06-10
12
1,438 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
  • 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now