按钮自绘

#pragma once


// CMyResertButton

class CMyResertButton : public CButton
{
DECLARE_DYNAMIC(CMyResertButton)

public:
CMyResertButton();
virtual ~CMyResertButton();

protected:
DECLARE_MESSAGE_MAP()

//CHN 设置按钮为自绘式
virtual void PreSubclassWindow();
//CHN 重载按钮的绘制
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);

private:
//CHN 闪铄颜色标志位   1:正常 2:切替色1
int m_nSparkColorFlag;
//CHN 按钮闪铄标志位
BOOL m_bSpark;
//CHN 按钮闪铄时间
int m_nSparkTime;
//CHN 按钮正常颜色
COLORREF m_rgbBkNormalColor;
//CHN 按钮闪铄时的切换颜色
COLORREF m_rgbBkChangeColor;
//CHN 按钮字体颜色
COLORREF m_rgbTextColor;
//CHN 按钮的矩形大小
CRect m_rctBtnRect;
//CHN 按钮文字
CString m_strBtnText;

public:
//CHN 设置按钮闪铄 TRUE:闪铄   FALSE:不闪铄
void SetButtonSpark(BOOL bSpark, int nSparkTime = 1000);
//CHN 设置按钮正常时的颜色
void SetButtonNormalColor(COLORREF rgbNormalColor);
//CHN 设置按钮切换颜色
void SetButtonChangeColor(COLORREF rgbChangeColor);
//CHN 设置按钮字体颜色
void SetButtonTextColor(COLORREF rgbTextColor);
afx_msg void OnTimer(UINT_PTR nIDEvent);
};

// MyResertButton.cpp : implementation file
//

#include "stdafx.h"
#include "ResertButton.h"
#include "MyResertButton.h"


#define ISPC_TIMER_ID 1000

IMPLEMENT_DYNAMIC(CMyResertButton, CButton)

CMyResertButton::CMyResertButton()
{
m_nSparkColorFlag = 1;
//CHN 默认设置为闪铄
m_bSpark = FALSE;
//CHN 闪铄时间为 1 秒
m_nSparkTime = 1000;
//CHN 按钮正常颜色(白色)
m_rgbBkNormalColor = RGB(255, 255, 255);
//CHN 按钮闪铄时的切换颜色(黄色)
m_rgbBkChangeColor = RGB(30, 240, 120);
//CHN 按钮的矩形大小
m_rctBtnRect = CRect(0, 0, 0, 0);
//CHN 字体颜色
m_rgbTextColor = RGB(0, 0, 0);
}

CMyResertButton::~CMyResertButton()
{
}


BEGIN_MESSAGE_MAP(CMyResertButton, CButton)
ON_WM_TIMER()
END_MESSAGE_MAP()


//CHN 设定按钮为自绘的形式
void CMyResertButton::PreSubclassWindow()
{
ModifyStyle( 0, BS_OWNERDRAW ); 
CButton::PreSubclassWindow();
}

//CHN 设置按钮正常时的颜色
void CMyResertButton::SetButtonNormalColor(COLORREF rgbNormalColor)
{
m_rgbBkNormalColor = rgbNormalColor;
Invalidate();
}
//CHN 设置按钮切换颜色
void CMyResertButton::SetButtonChangeColor(COLORREF rgbChangeColor)
{
m_rgbBkChangeColor = rgbChangeColor;
Invalidate();
}

//CHN 设置按钮字体颜色
void CMyResertButton::SetButtonTextColor(COLORREF rgbTextColor)
{
m_rgbTextColor = rgbTextColor;
Invalidate();
}

//CHN 响应定时器消息进行按钮刷新
void CMyResertButton::OnTimer(UINT_PTR nIDEvent)
{
Invalidate();
CButton::OnTimer(nIDEvent);
}

//CHN 设置按钮闪铄 TRUE 闪铄 FALSE 不闪铄
void CMyResertButton::SetButtonSpark(BOOL bSpark, int nSpakTime)
{
//CHN 设置按钮闪铄
if(bSpark)
{
//CHN 按钮已是闪铄状态
if(m_bSpark)
{
return;
}
//CHN 按钮处于不闪铄状态
else
{
::SetTimer(this->GetSafeHwnd(), ISPC_TIMER_ID, nSpakTime, NULL);
m_bSpark = bSpark;
}
}
//CHN 设置按钮不闪铄
else
{
//CHN 恢复为正常状态
m_nSparkColorFlag = 1;
//CHN 按钮状态为不闪铄
if (!m_bSpark)
{
Invalidate();
return;
}
//CHN 按钮已是闪铄状态转为不闪铄状态
else
{
KillTimer(ISPC_TIMER_ID);
m_bSpark = bSpark;
Invalidate();
}
}
}

void CMyResertButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC *pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
m_rctBtnRect = lpDrawItemStruct->rcItem;
INT32 nSaveDC = pDC->SaveDC();
VERIFY(pDC);

//CHN 按钮处于闪铄状态
if (m_bSpark)
{
if (m_nSparkColorFlag == 1)
{
m_nSparkColorFlag = 2;
}
else
{
m_nSparkColorFlag = 1;
}
}
// CHN bColor边框颜色
COLORREF bColor;
// CHN 绘制按钮背景
CBrush Brush;
switch(m_nSparkColorFlag)
{
// CHN 正常按钮
case 1:
Brush.CreateSolidBrush(m_rgbBkNormalColor);   
break; 
// CHN 切换颜色
case 2:
Brush.CreateSolidBrush(m_rgbBkChangeColor);
break;  
default:
break;
}
pDC->SelectObject( &Brush );

CPen Pen;
Pen.CreatePen(PS_SOLID, 1, m_rgbTextColor );
pDC->SelectObject( &Pen );

// CHN 画矩形
pDC->Rectangle(&m_rctBtnRect);

// CHN 绘制按钮文字
GetWindowText(m_strBtnText);
pDC->SetTextColor(m_rgbTextColor);    
pDC->SetBkMode( TRANSPARENT );
pDC->DrawText( m_strBtnText, &m_rctBtnRect, DT_SINGLELINE | DT_CENTER | DT_VCENTER | DT_END_ELLIPSIS);
Brush.DeleteObject();
}








你可能感兴趣的:(按钮自绘)