详细叫你怎样让MFC界面上的控件也有颜色
BOOL CMyButton::Attach(const UINT nID, CWnd* pParent)
{
//GetDlgItem(nID)->ModifyStyle(0,BS_OWNERDRAW,0);
if (!SubclassDlgItem(nID, pParent))
return FALSE;
return TRUE;
}
void CMyButton::SetDownColor(COLORREF color)
{
m_DownColor = color;
}
void CMyButton::SetUpColor(COLORREF color)
{
m_UpColor = color;
}
void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your code to draw the specified item
CDC dc;
dc.Attach(lpDrawItemStruct->hDC);//得到绘制的设备环境CDC
VERIFY(lpDrawItemStruct->CtlType==ODT_BUTTON);
// 得当Button上文字,这里的步骤是:1,先得到在资源里编辑的按钮的文字,
//然后将此文字重新绘制到按钮上,
//同时将此文字的背景色设为透明,这样,按钮上仅会显示文字
const int bufSize = 512;
TCHAR buffer[bufSize];
GetWindowText(buffer, bufSize);
int size=strlen(buffer); //得到长度
DrawText(lpDrawItemStruct->hDC,buffer,size,&lpDrawItemStruct->rcItem,DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_TABSTOP); //绘制文字
SetBkMode(lpDrawItemStruct->hDC,TRANSPARENT); //透明
if (lpDrawItemStruct->itemState &ODS_SELECTED) //当按下按钮时的处理
{//
//重绘整个控制
CBrush brush(m_DownColor);
dc.FillRect(&(lpDrawItemStruct->rcItem),&brush);//
//因为这里进行了重绘,所以文字也要重绘
DrawText(lpDrawItemStruct->hDC,buffer,size,&lpDrawItemStruct->rcItem,DT_CENTER|DT_V
搜索“diyifanwen.net”或“第一范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,第一范文网,提供最新人文社科MFC Button控件的背景颜色(2)全文阅读和word下载服务。
相关推荐: