Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do you change CButton color???

0 views
Skip to first unread message

Adam Rabin

unread,
Jun 27, 1997, 3:00:00 AM6/27/97
to

Environment: Win95, VC5

This is one I've never seen addressed here.

I have a CScrollView on which I dynamically create buttons using
CButton::Create.

How, though do I change the color of these buttons to let's say a green
or red
background? CtlColor doesn't seem to be the valid message for child
controls
anymore and indeed when I tried it, it it diddn't work. The help
mentions
something about a WM_CTLCOLORBTN message, but how exactly would you
process this under MFC? Also, the documentation says this message is
sent to the parent, when I would like to process this using reflection
if possible.

Lastly, I want the methodology to be portable to Windows NT.

Thanx in advance.

Cecil A. Galbraith

unread,
Jul 5, 1997, 3:00:00 AM7/5/97
to Adam Rabin

I've never been able to get this to work for a push-button. There is a
class at the URL below called CGraphicButton that sets the color of the
button to the proper 3D system color internally, but that could be
easily changed to use whatever color that you want...

This class also permits a combination of bitmap and text on the button
face, which is it's whole reason for being....

If you can get to work any other way, please let me know how you did it.

Cecil

--
Cecil Galbraith
CustomSoft mail to cgal...@concentric.net

Free programmer's utilities and MFC tips at
http://www.concentric.net/~cgalbrai

Deepa Desai

unread,
Jul 6, 1997, 3:00:00 AM7/6/97
to

Hi,

For some reason, you cannot change the background color of a button. The
solution is to subclass CButton and do your painting. Make sure you create
the button with BS_OWNERDRAW style.

Here's the sample code:

declare these as members of your subclassed button (header file).

CBrush m_brHollow;
CBrush m_brColor;
COLORREF m_color;

in the consturctor
m_color = color' //what ever color you want
VERIFY (m_brColor.CreateSolidBrush(m_color));
VERIFY (m_brHollow.CreateStockObject (NULL_BRUSH));

BOOL CAAColoredButton::OnChildNotify(UINT message, WPARAM wParam, LPARAM
lParam, LRESULT* pLResult)
{
if (message != WM_DRAWITEM)
{
return CButton::OnChildNotify(message, wParam, lParam, pLResult);
}
TRACE ("OnChildNotify msg is WM_DRAWITEM\n");

LPDRAWITEMSTRUCT lpDrawItemStruct = (LPDRAWITEMSTRUCT)lParam;

CRect rc;
GetWindowRect(&rc); //window rectangle..
ScreenToClient (&rc); // ..client rectangle

if (lpDrawItemStruct->itemState & ODS_SELECTED) //button is down
rc += CPoint (1, 1); //shift sourtheast

CString text;
GetWindowText (text);
TRACE ("Window text is %s\n", text);

CDC *pDC = CDC::FromHandle (lpDrawItemStruct->hDC);

//paint color background
CBrush *pOldBrush = pDC->SelectObject (&m_brColor);
pDC->PatBlt(0, 0, rc.Width (), rc.Height (), PATCOPY);
pOldBrush = pDC->SelectObject (pOldBrush);

//draw btn border using COLOR_BTNTEXT
pOldBrush = pDC->SelectObject (&m_brHollow);
CPen pen (PS_SOLID, 2, GetSysColor (COLOR_BTNTEXT));
CPen *pOldPen = pDC->SelectObject (&pen);
pDC->Rectangle (&rc);

//draw button text
pDC->SetTextColor (GetSysColor (COLOR_BTNTEXT));
pDC->SetBkColor (m_color); //colored back ground
pDC->DrawText (text, &rc, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
pDC->SelectObject (pOldBrush);
pDC->SelectObject (pOldPen);

// Return TRUE to indicate that the message was handled:
return TRUE;


}

This should do the trick!

Deepa


Cecil A. Galbraith <cgal...@concentric.net> wrote in article
<33BE1F...@concentric.net>...

0 new messages