|
class CButtonZP : public CButton
{
// Construction
public:
CButtonZP();
// Attributes
public:
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CButtonZP)
public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDIS);
//}}AFX_VIRTUAL
// Implementation
public:
void SetBGColor(COLORREF crNew);
virtual ~CButtonZP();
COLORREF m_crBg;
// Generated message map functions
protected:
//{{AFX_MSG(CButtonZP)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
#include "stdafx.h"
#include "2410.h"
#include "ButtonZP.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CButtonZP
CButtonZP::CButtonZP()
{
}
CButtonZP::~CButtonZP()
{
}
BEGIN_MESSAGE_MAP(CButtonZP, CButton)
//{{AFX_MSG_MAP(CButtonZP)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CButtonZP message handlers
void CButtonZP::SetBGColor(COLORREF crNew)
{
m_crBg = crNew;
Invalidate();
}
void CButtonZP::DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
CDC* pDC = CDC::FromHandle(lpDIS->hDC);
TRACE(L"DRAWITEM\r\n");
CRect itemRect = lpDIS->rcItem;
//准备绘画,用背景颜色填充背景区域
COLORREF bgColor;
bgColor = m_crBg;
CBrush br(bgColor);
pDC->FillRect(&itemRect, &br);
//读取按钮字符
CString sTitle;
GetWindowText(sTitle);
pDC->SetTextColor(RGB(255,0,0));
pDC->SetBkColor(bgColor);
int nOld=pDC->SetBkMode(TRANSPARENT);
pDC->DrawText(sTitle, sTitle.GetLength(), itemRect, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
pDC->SetBkMode(nOld);
}
调用
CButtonZP m_BtnAuto;
m_BtnAuto.SetBGColor(RGB(255,0,0));
为什么没效果呢? |
|