MFC CButton is pretty basic. Cannot even change background color, by default. Well, if you need a subclass with a fixed background color, read on.
This is a rough and working implementation that relies on WM_CTLCOLOR message handling. Comments are in Italian but code is so stupid that I think even a spider could understand it.
License is LGPL
Header file
/** \file
\date $Date: 2007/01/10 17:09:43 $
\version $Revision: 1.1 $
*/
#ifndef __IPAQ_CLIENT_COLOR_BUTTON_H
#define __IPAQ_CLIENT_COLOR_BUTTON_H
#include <afxwin.h>
/** Subclass del pulsante standard che
permette una colorazione personalizzabile */
class CColorButton : public CButton
{
public:
CColorButton();
/** Specifica il colore in fase di creazione */
CColorButton(COLORREF color);
virtual ~CColorButton();
/** Ignoro il secondo parametro. Spero che arrivi
sempre il CTLCOLOR_BTN */
afx_msg HBRUSH CtlColor (CDC* pDC, UINT nCtlColor);
protected:
COLORREF m_Color;
CBrush m_BackBrush;
DECLARE_MESSAGE_MAP()
DECLARE_DYNAMIC(CColorButton)
};
#endif //__IPAQ_CLIENT_COLOR_BUTTON_H
Source file
/** \file
\date $Date: 2007/01/10 17:09:43 $
\version $Revision: 1.1 $
*/
#include "stdafx.h"
#include "ColorButton.h"
IMPLEMENT_DYNAMIC(CColorButton, CButton)
CColorButton::CColorButton()
{
m_Color = ::GetSysColor(COLOR_WINDOW);
m_BackBrush.CreateSolidBrush(m_Color);
}
CColorButton::CColorButton(COLORREF color) :
m_Color(color),
m_BackBrush(color)
{}
CColorButton::~CColorButton()
{}
BEGIN_MESSAGE_MAP(CColorButton, CButton)
ON_WM_CTLCOLOR_REFLECT()
END_MESSAGE_MAP()
HBRUSH CColorButton::CtlColor(CDC* pDC, UINT nCtlColor)
{
return (HBRUSH)m_BackBrush;
}