Files
envi-code/SourceCode/Code2026/Dimension/CColorButton.cpp
T

91 lines
2.5 KiB
C++
Raw Normal View History

2026-10-04 00:04:20 +08:00
#include "StdAfx.h"
2026-09-28 15:28:50 +08:00
#include "CColorButton.h"
CColorButton::CColorButton()
{
}
CColorButton::~CColorButton()
{
}
2026-10-04 00:04:20 +08:00
//从Kernal移植过来 彩色按钮
2026-09-28 15:28:50 +08:00
BEGIN_MESSAGE_MAP(CColorButton, CButton)
//{{AFX_MSG_MAP(CColorButton)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
void CColorButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC dc;
CRect rect;
rect = lpDrawItemStruct->rcItem;
2026-10-04 00:04:20 +08:00
dc.Attach(lpDrawItemStruct->hDC);//得到绘制的设备环境CDC
2026-09-28 15:28:50 +08:00
VERIFY(lpDrawItemStruct->CtlType==ODT_BUTTON);
2026-10-04 00:04:20 +08:00
// 得当Button上文字,这里的步骤是:1,先得到在资源里编辑的按钮的文字,
//然后将此文字重新绘制到按钮上,
//同时将此文字的背景色设为透明,这样,按钮上仅会显示文字
2026-09-28 15:28:50 +08:00
const int bufSize = 512;
TCHAR buffer[bufSize];
GetWindowText(buffer, bufSize);
2026-10-04 00:04:20 +08:00
int size=_tcslen(buffer); //得到长度
DrawText(lpDrawItemStruct->hDC,buffer,size,&lpDrawItemStruct->rcItem,DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_TABSTOP); //绘制文字
SetBkMode(lpDrawItemStruct->hDC,TRANSPARENT); //透明
2026-09-28 15:28:50 +08:00
2026-10-04 00:04:20 +08:00
if (lpDrawItemStruct->itemState &ODS_SELECTED) //当按下按钮时的处理
2026-09-28 15:28:50 +08:00
{
2026-10-04 00:04:20 +08:00
//重绘整个控制
2026-09-28 15:28:50 +08:00
CBrush brush(m_backgroudColor);
dc.FillRect(&(lpDrawItemStruct->rcItem),&brush);//
2026-10-04 00:04:20 +08:00
//因为这里进行了重绘,所以文字也要重绘
2026-09-28 15:28:50 +08:00
DrawText(lpDrawItemStruct->hDC,buffer,size,&lpDrawItemStruct->rcItem,DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_TABSTOP);
SetBkMode(lpDrawItemStruct->hDC,TRANSPARENT);
dc.Draw3dRect(&rect,RGB(0,0,0),RGB(255,255,255));
}
2026-10-04 00:04:20 +08:00
else //当按钮不操作或者弹起时
2026-09-28 15:28:50 +08:00
{
CBrush brush(m_backgroudColor);
dc.FillRect(&(lpDrawItemStruct->rcItem),&brush);//
2026-10-04 00:04:20 +08:00
//同上,进行重绘文字
2026-09-28 15:28:50 +08:00
DrawText(lpDrawItemStruct->hDC,buffer,size,&lpDrawItemStruct->rcItem,DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_TABSTOP);
SetBkMode(lpDrawItemStruct->hDC,TRANSPARENT);
dc.Draw3dRect(&rect,RGB(255,255,255),RGB(0,0,0));
}
2026-10-04 00:04:20 +08:00
//选中了本控件,高亮边框
2026-09-28 15:28:50 +08:00
if ((lpDrawItemStruct->itemState &ODS_SELECTED)&&(lpDrawItemStruct->itemAction &(ODA_SELECT| ODA_DRAWENTIRE)))
{
dc.Draw3dRect(&rect,RGB(0,0,0),RGB(255,255,255));
}
2026-10-04 00:04:20 +08:00
//控制的选中状态结束,去掉边框
2026-09-28 15:28:50 +08:00
if (!(lpDrawItemStruct->itemState & ODS_SELECTED) &&(lpDrawItemStruct->itemAction & ODA_SELECT))
{
dc.Draw3dRect(&rect,RGB(0,0,0),RGB(255,255,255));
}
dc.Detach();
}
void CColorButton::SetBackgroundColor(COLORREF color)
{
m_backgroudColor = color;
}
BOOL CColorButton::Attach(const UINT nID, CWnd* pParent)
{
if (!SubclassDlgItem(nID, pParent))
{
return FALSE;
}
return TRUE;
}