Solved

changing the caption color of a Button

Posted on 2001-07-28
13
481 Views
Last Modified: 2013-11-20
is there an easy way to change the color of the caption on a button control.....
0
Comment
Question by:b_yousif
  • 6
  • 5
  • 2
13 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 50 total points
Comment Utility
sure, add the following code (by Bob Ryan)
to your app and u'll have it all:

//colorBtn.h
#ifndef __COLORBTN_H__
#define __COLORBTN_H__

class CColorButton : public CButton
{
DECLARE_DYNAMIC(CColorButton)
public:
      CColorButton();
      virtual ~CColorButton();

      BOOL Attach(const UINT nID, CWnd* pParent,
            const COLORREF BGColor = RGB(192, 192, 192),            // gray button
            const COLORREF FGColor = RGB(1, 1, 1),                        // black text
            const COLORREF DisabledColor = RGB(128, 128, 128),      // dark gray disabled text
            const UINT nBevel = 2
      );

      
protected:
      virtual void DrawItem(LPDRAWITEMSTRUCT lpDIS);
      void DrawFrame(CDC *DC, CRect R, int Inset);
      void DrawFilledRect(CDC *DC, CRect R, COLORREF color);
      void DrawLine(CDC *DC, CRect EndPoints, COLORREF color);
      void DrawLine(CDC *DC, long left, long top, long right, long bottom, COLORREF color);
      void DrawButtonText(CDC *DC, CRect R, const char *Buf, COLORREF TextColor);

      COLORREF GetFGColor() { return m_fg; }      
      COLORREF GetBGColor() { return m_bg; }
    COLORREF GetDisabledColor() { return m_disabled; }
      UINT GetBevel() { return m_bevel; }

private:
      COLORREF m_fg, m_bg, m_disabled;
      UINT m_bevel;

};
#endif


//colorBtn.cpp
#include "stdafx.h"
#include "colorbtn.h"
 
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

// no automatic class substitution for this file!
#ifdef CColorButton
#undef CColorButton      CColorButton
#endif

// CColorButton
IMPLEMENT_DYNAMIC(CColorButton, CButton)

CColorButton::CColorButton()
{  
#if (_MFC_VER < 0x0250)
  hwndOwner = NULL;  // initialize hwndOwner for GetOwner() and SetOwner() support in MFC < 2.5
#endif
}



CColorButton::~CColorButton()
{
}



BOOL CColorButton::Attach(const UINT nID, CWnd* pParent, const COLORREF BGColor, const COLORREF FGColor, const COLORREF DisabledColor, const UINT nBevel)
{
      if (!SubclassDlgItem(nID, pParent))
            return FALSE;

      m_fg = FGColor;
      m_bg = BGColor;
      m_disabled = DisabledColor;
      m_bevel = nBevel;

      return TRUE;
}


void CColorButton::DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
      CDC* pDC = CDC::FromHandle(lpDIS->hDC);

      UINT state = lpDIS->itemState;
      CRect focusRect, btnRect;
      focusRect.CopyRect(&lpDIS->rcItem);
      btnRect.CopyRect(&lpDIS->rcItem);

      //
      // Set the focus rectangle to just past the border decoration
      //
      focusRect.left += 4;
    focusRect.right -= 4;
    focusRect.top += 4;
    focusRect.bottom -= 4;
     
      //
      // Retrieve the button's caption
      //
    const int bufSize = 512;
    TCHAR buffer[bufSize];
    GetWindowText(buffer, bufSize);
      

      //
      // Draw and label the button using draw methods
      //
    DrawFilledRect(pDC, btnRect, GetBGColor());
    DrawFrame(pDC, btnRect, GetBevel());
        DrawButtonText(pDC, btnRect, buffer, GetFGColor());


      //
      // Now, depending upon the state, redraw the button (down image) if it is selected,
      // place a focus rectangle on it, or redisplay the caption if it is disabled
      //
      if (state & ODS_FOCUS) {
            DrawFocusRect(lpDIS->hDC, (LPRECT)&focusRect);
            if (state & ODS_SELECTED){
                DrawFilledRect(pDC, btnRect, GetBGColor());
                DrawFrame(pDC, btnRect, -1);
                    DrawButtonText(pDC, btnRect, buffer, GetFGColor());
                  DrawFocusRect(lpDIS->hDC, (LPRECT)&focusRect);
            }
      }
      else if (state & ODS_DISABLED) {
          //COLORREF disabledColor = bg ^ 0xFFFFFF; // contrasting color
              DrawButtonText(pDC, btnRect, buffer, GetDisabledColor());
    }
}


void CColorButton::DrawFrame(CDC *DC, CRect R, int Inset)
{
      COLORREF dark, light, tlColor, brColor;
      int i, m, width;
      width = (Inset < 0)? -Inset : Inset;
      
      for (i = 0; i < width; i += 1) {
            m = 255 / (i + 2);
            dark = PALETTERGB(m, m, m);
            m = 192 + (63 / (i + 1));
            light = PALETTERGB(m, m, m);
             
              if ( width == 1 ) {
                  light = RGB(255, 255, 255);
                  dark = RGB(128, 128, 128);
            }
            
            if ( Inset < 0 ) {
                  tlColor = dark;
                  brColor = light;
            }
            else {
                  tlColor = light;
                  brColor = dark;
            }
            
            DrawLine(DC, R.left, R.top, R.right, R.top, tlColor);                                          // Across top
            DrawLine(DC, R.left, R.top, R.left, R.bottom, tlColor);                                          // Down left
        
            if ( (Inset < 0) && (i == width - 1) && (width > 1) ) {
                  DrawLine(DC, R.left + 1, R.bottom - 1, R.right, R.bottom - 1, RGB(1, 1, 1));// Across bottom
                  DrawLine(DC, R.right - 1, R.top + 1, R.right - 1, R.bottom, RGB(1, 1, 1));      // Down right
            }
              else {
                  DrawLine(DC, R.left + 1, R.bottom - 1, R.right, R.bottom - 1, brColor);            // Across bottom
                  DrawLine(DC, R.right - 1, R.top + 1, R.right - 1, R.bottom, brColor);            // Down right
            }
              InflateRect(R, -1, -1);
      }
}



void CColorButton::DrawFilledRect(CDC *DC, CRect R, COLORREF color)
{
      CBrush B;
      B.CreateSolidBrush(color);
      DC->FillRect(R, &B);
}
 

void CColorButton::DrawLine(CDC *DC, CRect EndPoints, COLORREF color)
{
      CPen newPen;
      newPen.CreatePen(PS_SOLID, 1, color);
      CPen *oldPen = DC->SelectObject(&newPen);
      DC->MoveTo(EndPoints.left, EndPoints.top);
      DC->LineTo(EndPoints.right, EndPoints.bottom);
      DC->SelectObject(oldPen);
    newPen.DeleteObject();
}

void CColorButton::DrawLine(CDC *DC, long left, long top, long right, long bottom, COLORREF color)
{
      CPen newPen;
      newPen.CreatePen(PS_SOLID, 1, color);
      CPen *oldPen = DC->SelectObject(&newPen);
      DC->MoveTo(left, top);
      DC->LineTo(right, bottom);
      DC->SelectObject(oldPen);
    newPen.DeleteObject();
}


void CColorButton::DrawButtonText(CDC *DC, CRect R, const char *Buf, COLORREF TextColor)
{
    COLORREF prevColor = DC->SetTextColor(TextColor);
    DC->SetBkMode(TRANSPARENT);
      DC->DrawText(Buf, strlen(Buf), R, DT_CENTER|DT_VCENTER|DT_SINGLELINE);
      DC->SetTextColor(prevColor);
}


good luck

            







0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
one more thing: dont forget to change the type of your buttons to CColorButton.

:~)
0
 
LVL 9

Expert Comment

by:Pacman
Comment Utility
what do you mean with "caption" of a button?
A frame window can have a caption.
Do you want to change the textcolor of the button title ?

please be more specific.
0
 

Author Comment

by:b_yousif
Comment Utility
yes Pacman, what i ment that i need to change the textcolor of of the button; if u go to the properties of push button in any dialog, there is a field called caption; i need to change the color of this.
do u have more direct way than sedgwick's one ?
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
he means 2 change the text color !!!
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
b_yousif: did u try my comment???
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 9

Expert Comment

by:Pacman
Comment Utility
Yes there is a simpler solution.
Before a control is drawn in a dialog, the dialog is asked to set the color for the control.

example:

// the following function makes the static text
// with the ID IDC_FARBTEXT red
// it should also work with buttons ...
// to add the function you can use class wizard's
// "add virtual function..."


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

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

    return hbr;
}

0
 
LVL 9

Expert Comment

by:Pacman
Comment Utility
sorry, IDC_FARBTEXT is not correct.
Must be IDC_MYTEXT
0
 
LVL 9

Expert Comment

by:Pacman
Comment Utility
you may also use CTLCOLOR_BTN instead of CTLCOLOR_STATIC ...

0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
Pacman: did u try it yourself befor posting that comment?
its not working on buttons dude!
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
b_yousif: post your e-mail and i'll send u a smpale
0
 
LVL 9

Expert Comment

by:Pacman
Comment Utility
>> its not working on buttons dude!

oops, you're right.
but it was worth a try ...

Pacman
0
 

Author Comment

by:b_yousif
Comment Utility
my e-mail is b_yousif@hotmail.com
it will be nice to get a sample.
you know when i asked this i thought that there is a way to do this using one of the member functions, some one that i didn't find in CButton.
any way i'm waiting for ur sample.
0

Featured Post

Highfive Gives IT Their Time Back

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

Title # Comments Views Activity
GIF file bit manipulation for color change 5 104
iSeries FTP Exit Program 8 113
viewing source code from eclipse 13 74
haveThree challenge 22 100
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: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 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

8 Experts available now in Live!

Get 1:1 Help Now