📄 colorbutton.cpp
字号:
// ColorButton.cpp : implementation file
//
#include "stdafx.h"
#include "FoodMenu.h"
#include "ColorButton.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CColorButton
CColorButton::CColorButton()
{
HadSelected=false;
}
CColorButton::~CColorButton()
{
}
BEGIN_MESSAGE_MAP(CColorButton, CButton)
//{{AFX_MSG_MAP(CColorButton)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CColorButton message handlers
void CColorButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your code to draw the specified item
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
UINT state = lpDrawItemStruct->itemState;//获得按钮状态
CRect focusRect, btnRect;
focusRect.CopyRect(&lpDrawItemStruct->rcItem);
btnRect.CopyRect(&lpDrawItemStruct->rcItem);//矩阵拷贝
//取得按钮标题
TCHAR buffer[MAX_PATH + 1];
GetWindowText(buffer,MAX_PATH);
strlong=0;
while(buffer[strlong]!=NULL)
strlong++;
//绘制按钮
if(!HadSelected)//还没按
{
DrawFilledRect(pDC,btnRect,GetBGColor()); //填充颜色(白)
DrawButtonText(pDC,btnRect,buffer,GetFGColor());//填充文字
}
else//按了
{
DrawFilledRect(pDC,btnRect,GetDisabledColor());//填充颜色
DrawButtonText(pDC,btnRect,buffer,RGB(255,255,255));//填充文字
}
//根据按钮的状态,变化。
if(state & ODS_FOCUS )//鼠标在focus地范围内
{
if(state & ODS_SELECTED)//按下还没放手
{
DrawFilledRect(pDC,btnRect,GetDisabledColor());
DrawButtonText(pDC,btnRect,buffer,RGB(255,255,255));
HadSelected=true;
}
}
else//按了其他建恢复原来的颜色
{
DrawFilledRect(pDC,btnRect,GetBGColor());
DrawButtonText(pDC,btnRect,buffer,GetFGColor());
}
}
BOOL CColorButton::Attach(const UINT nID, CWnd *pParent, const COLORREF BGColor,
const COLORREF FGColor, const COLORREF DisabledColor)
{
if(!SubclassDlgItem(nID,pParent))
return false;
m_fg = FGColor;
m_bg = BGColor;
m_disabled = DisabledColor;
return true;
}
COLORREF CColorButton::GetFGColor()
{
m_fg=RGB(32,25,78);
return m_fg;
}
COLORREF CColorButton::GetBGColor()
{
m_bg=RGB(255,255,255);//()175,234,241
return m_bg;
}
COLORREF CColorButton::GetDisabledColor()
{
m_disabled=RGB(128,191,255);//110,180,255
return m_disabled;
}
void CColorButton::DrawFilledRect(CDC *DC, CRect R, COLORREF color)
{
CBrush B;
B.CreateSolidBrush(color);
DC->FillRect(R,&B);
}
void CColorButton::DrawButtonText(CDC *DC, CRect R,unsigned short Buf[], COLORREF TextColor)
{
COLORREF prevColor = DC->SetTextColor(TextColor);
DC->SetBkMode(TRANSPARENT);
DC->DrawText((LPCTSTR)Buf,strlong/*(strlen(Buf))*/,R, DT_VCENTER |DT_SINGLELINE);
DC->SetTextColor(prevColor);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -